Hexo+nginx部署博客到服务器

之前使用hexo在github上搭建了博客,但是由于加载速度过慢,有时候不稳定,就开始考虑将博客部署到自己的服务器上面。

前提条件

  1. 服务器
  2. 域名(也可以使用IP,请在下文中将域名替换为IP地址)
  3. nginx安装
  4. hexo安装
    hexo安装教程

本地配置

在站点根目录下的package.js中的dependencies中加入如下代码:

"hexo-deployer-rsync": "git+https://github.com/dolonfly/hexo-deployer-rsync.git"

执行

npm install

安装依赖

_config.yml配置如下:

1
2
3
4
5
6
7
8
9
10
deploy:
type: rsync
#服务器IP地址
host: 121.40.253.109
#服务器用户(默认的为root)
user: root
#public下所有资源存放的路径(服务器上要有此路径)
root: /data/www/aaa.cn/
port: 22
delete: false

本配置为本人服务器的配置,替换为自己的即可。

测试:

1
hexo d -g

生成静态文件并部署到远程服务器

运行此命令即进行将文档渲染为静态文件,并发送到121.40.253.109的/data/www/dullong.com/目录下。登陆主机,对应文件夹内存在public文件夹内内容即表名配置手动部署成功。

服务器端配置

在/etc/nginx/nginx.conf或者/etc/nginx/conf.d/default.conf文件中找到server配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
server {
listen 80;

#虚拟主机名称(浏览器上要输入的地址)
server_name www.aaa.cn;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

#过滤该目录下的所有静态资源
location / {
root /data/www/aaa.cn;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {

#显示错误信息的静态资源
root /data/www/aaa.cn;
}

更新nginx配置

nginx -s reload

最后将域名解析到IP地址,输入www.liuyanhao.cn可访问到本人静态博客