Hexo部署到私人服务器解决方案

博客部署在Gitpage上,国内加载速度很慢,恰好手头有个空闲的阿里云服务器,于是决定部署到自己的服务器上,用此文记录一下折腾过程。

基本流程

流程结构

  1. 本地创建hexo项目,部署到gitpage上,同时本地hexo项目代码上传到GitHub仓库中,做好备份。
  2. gitpage依赖的仓库设置webhook,每当本地hexo deploy时触发post请求到目标服务器上。
  3. 目标服务器处理post请求,执行deploy脚本来更新静态页面内容。

具体操作

准备

  • 两个GitHub仓库,
    • 一个用于存放hexo代码
    • 一个用于发布页面(命名规则:github用户名.github.io)
  • 备案过的域名
  • 云服务器

服务器搭建Webhook服务

安装 webhook for Ubuntu

sudo apt-get install webhook

安装 Docker

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

新建配置文件 webhook.json

[
    {
        "id": "blog",
        "execute-command": "/root/blog/hexo-deploy.sh",
        "response-message": "deploy done!",
        "trigger-rule": {
            "and": [
                {
                    "match": {
                        "type": "payload-hash-sha1",
                        "secret": "your-secret",
                        "parameter": {
                            "source": "header",
                            "name": "X-Hub-Signature"
                        }
                    }
                }
            ]
        }
    }
]

运行webhook服务(默认监听9000端口)

nohup webhook -hooks webhook.json -verbose &

Github上配置webhook

  • 设置post地址为http://qmdx00.cn:9000/hooks/blog
  • 设置content-type为json
  • 设置secret

webhook 配置

编写shell脚本

启动webhook脚本start.sh
#!/bin/bash
config=./webhook.json
nohup webhook -hooks $config -port 9000 -verbose >out &
if [[ $? -eq 0 ]];then
    echo "start success !"
else
    echo "start error !"
fi
停止webhook脚本stop.sh
#!/bin/bash
process=webhook
pids=$(ps aux|grep $process|grep -v grep|awk '{print $2}')
for pid in $pids; do
    kill $pid
    if [[ $? -eq 0 ]]; then
        echo "$pid is killed !"
    else
        echo "process isn't running !"
    fi
done
webhook触发执行脚本hexo-deploy.sh
#!/bin/bash
path=./qmdx00.github.io
cd $path && git pull
if [[ $? -eq 0 ]]; then
    echo "pull success !"
else
    echo "pull failed !"
    exit
fi

用docker启动nginx容器托管静态页面

此时目录结构为

root@ubuntu:~/blog# tree -L 1
.
├── hexo-deploy.sh
├── out
├── qmdx00.github.io
├── start.sh
├── stop.sh
└── webhook.json

1 directory, 5 files

拉取并启动nginx容器,将容器80端口映射到宿主机80端口,并将容器/usr/share/nginx/html目录挂载到静态页面目录/root/blog/qmdx00.github.io

docker pull nginx:latest \
    && docker run -d \
    --name hexo-nginx \
    -p 80:80 \
    -v /root/blog/qmdx00.github.io:/usr/share/nginx/html \
    nginx:latest

配置 https

上传ssl证书文件
  • 目录结构如下:
.
├── hexo-deploy.sh
├── nginx
│   ├── conf
│   │   └── default.conf    # nginx配置文件
│   └── ssl
│       ├── qmdx00.cn.key   # ssl密钥
│       └── qmdx00.cn.pem   # ssl证书
├── out
├── qmdx00.github.io
├── start.sh
├── stop.sh
└── webhook.json
  • default.conf 配置文件:
server {
    listen 80;
    server_name qmdx00.cn;
    rewrite ^(.*)$ https://$host$1 permanent;

    location / {
        index index.html index.htm;
    }
}

server {
    listen 443 ssl;
    server_name qmdx00.cn;

    root   /usr/share/nginx/html;
    index  index.html index.htm;
    
    ssl_certificate  /etc/nginx/cert/qmdx00.cn.pem;
    ssl_certificate_key /etc/nginx/cert/qmdx00.cn.key;

    ssl_session_timeout 5m;

    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location / {
        index index.html index.htm;
    }
}
挂载证书文件和nginx配置文件到容器中
docker pull nginx:latest \
    && docker run -d \
    --name hexo-nginx-ssl \
    -p 80:80 \
    -p 443:443 \
    -v /root/blog/qmdx00.github.io:/usr/share/nginx/html \
    -v /root/blog/nginx/ssl:/etc/nginx/cert \
    -v /root/blog/nginx/conf:/etc/nginx/conf.d \
    nginx:latest

测试

hexo的package.json片段

{
  "scripts": {
    "serve": "hexo cl && gulp && hexo s",
    "deploy": "hexo cl && hexo g && gulp && hexo d"
  }
}

本地新建文章hexo new "new post", 并发布npm run deploy,刷新页面看到新建的文章说明成功,webhook推送记录如下:

webhook推送记录

posted @ 2020-08-10 21:42  一条小咸鱼  阅读(739)  评论(0)    收藏  举报