VPS部署静态Hexo博客

文章目录
  1. 1. 1 配置WebServer
    1. 1.1. 1.1 安装nginx
    2. 1.2. 1.2 设置根路径
    3. 1.3. 1.3 配置nginx
  2. 2. 2 配置VPS登录
  3. 3. 3 配置Hexo
    1. 3.1. 3.1 VPS上的Hexo
    2. 3.2. 3.2 本地Hexo
  4. 4. 4 过程总结

背景:本地已经搭建好了Hexo静态博客,托管在了Git Page上,但是GitHub的访问速度不太理想,现在需要将其部署在自己购买的一个VPS上。

在本地通过Hexo建立静态博客的方法可以参考:使用GitHub和Hexo搭建免费静态Blog

1 配置WebServer

使用 Nginx 实现一个简单的 WebServer(用以静态博客真是大材小用了)。

1.1 安装nginx

执行 yum install nginx,结果如下

1
2
3
4
5
6
7
8
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.yun-idc.com
* extras: mirrors.tuna.tsinghua.edu.cn
* rpmforge: mirrors.neusoft.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
No package nginx available.
Error: Nothing to do

没有找到nginx package,那就手动配置nginx源吧,执行下面命令即可。

vim /etc/yum.repos.d/nginx.repo

修改内容如下

1
2
3
4
5
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

然后 yum install nginx 即可。

1.2 设置根路径

然后设置 WebServer 工作的根路径。

1
2
3
mkdir /var/www/blog
chmod 0755 blog
chown root:root -R /var/www/blog

1.3 配置nginx

下面是HTTP的配置方案:

1
2
cd /etc/nginx/conf.d
vim default.conf

default.conf 修改为下面的配置:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server {
listen 80;
listen [::]:80;

# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

# ====== 2 ======
root /var/www/blog;
# ====== 2 ======

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

# ====== 1 ======
server_name mydomain.com;
# ====== 1 ======

location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
# ====== 2 ======
root /var/www/blog;
# ====== 2 ======
access_log off;
expires 1d;
}
location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
# ====== 2 ======
root /var/www/blog;
# ====== 2 ======
access_log off;
expires 10m;
}
location / {
# ====== 2 ======
root /var/www/blog;
# ====== 2 ======
if (-f $request_filename) {
rewrite ^/(.*)$ /$1 break;
}
}
}

注意上面配置中的 /var/www/blog 就是1.2中自定义的根路径。

然后启动 nginx

1
/etc/init.d/nginx start

2 配置VPS登录

这里需要配置ssh登录,以免hexo在部署博客的时候还需要输入密码。步骤如下:

在本地MAC生成博客专用的公私钥

1
ssh-keygen -t blog

会在 ~/.ssh/ 目录下生成 blogblog.pub 两个文件

然后在本地MAC创建配置文件

1
2
3
4
cd ~
mkdir .ssh
chmod 0755 .ssh
vim .ssh/config

内容如下:

1
2
3
4
5
Host 97.64.**.**             # VPS 的 IP
HostName 97.64.**.** # VPS 的 IP
User root # 用户
Port 1270 # SSH 端口
IdentityFile ~/.ssh/blog # SSH Key

登录VPS,执行下面的命令:

1
2
3
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys
vim ~/.ssh/authorized_keys

然后把本地MACblog.pub 中的内容拷贝进去。

输入 ssh git@97.64.**.** 验证一下,上面配置正确的话,就可以免密 ssh 登陆了。

3 配置Hexo

3.1 VPS上的Hexo

root用户登录VPS,执行:

1
2
3
4
cd ~
mkdir hexo
cd hexo
git init --bare

这里建立了一个 git 仓库,MAC上的博客内容可以通过 push 到这个仓库,然后执行一些脚本将博客内容同步到 WebServer 根目录。

执行:

1
2
3
cd ~/hexo/hooks
touch post-receive
vim post-receive

然后写入下面的内容:

1
2
3
4
5
6
7
8
9
!/bin/bash

GIT_REPO=~/hexo # 空 Git 仓库的文件夹,触发 hook 时已经存入了内容
TMP_GIT_CLONE=/tmp/hexo # 缓存文件夹,存在 /tmp 下可以随意读写
PUBLIC_WWW=/var/www/blog # 之前创建的 blog 文件夹,用作网站主目录
rm -rf ${TMP_GIT_CLONE} # 删除缓存的全部内容
git clone $GIT_REPO $TMP_GIT_CLONE # 将 Git 仓库被上传的内容写入缓存
rm -rf ${PUBLIC_WWW}/* # 删除网站主目录全部内容
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW} # 将缓存目录所有内容复制到主目录

别忘记给这个脚本加上权限:

1
chmod +x post-receive

3.2 本地Hexo

首先安装一个用于 Git 上传的模块: hexo-deployer-git

1
npm install hexo-deployer-git --save

然后配置本地的Hexo的部署信息。很简单,进入hexo的根目录,修改 _config.yml 文件,找到 deploy 字段,修改如下:

1
2
3
4
5
6
7
deploy: 
- type: git
repo: root@97.64.**.**:hexo ## VPS部署的位置
branch: master
- type: git
repo: https://github.com/your_repo/****.github.io.git
branch: master

这样就可以实现 github 和VPS的双部署,可以执行 hexo d -g 体验一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
INFO  Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
On branch master
nothing to commit, working tree clean
Everything up-to-date
Branch master set up to track remote branch master from root@97.64.**.**:hexo.
INFO Deploy done: git
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
On branch master
nothing to commit, working tree clean
To https://github.com/your_repo/****.github.io.git
8307156..1b22d1f HEAD -> master
Branch master set up to track remote branch master from https://github.com/your_repo/****.github.io.git.
INFO Deploy done: git

快速实现了双部署,输入 VPS 的 IP,即可看到博客内容,剩下的绑定域名就不再赘述了。

4 过程总结

注意事项:

  • 最好 为 Hexo 生成不同的密钥对;
  • 钩子脚本 post-receive 需可执行权限;
  • MWEB这个工具貌似有更简洁的部署静态博客的方案,也可以考虑下。

参考文章:

  1. 在搬瓦工 VPS 上搭建 Hexo
  2. 通过 Git Hooks 自动部署 Hexo 到 VPS