一。开篇词#
1.1 前言#
- 官方文档介绍了 Hexo 可以同时
一键部署到多个平台,只需要同时使用多个deployer。
Hexo 提供了快速方便的一键部署功能,让您只需一条命令就能将网站部署到服务器上。
$ hexo deploy在开始之前,您必须先在
_config.yml中修改参数,一个正确的部署配置中至少要有type参数,例如:deploy: type: git您可同时使用多个 deployer,Hexo 会依照顺序执行每个 deployer。
deploy: - type: git repo: - type: heroku repo:
1.2. 说明#
- 使用
Git Hooks同步 Hexo 生成的静态文件。(后期我弃用 git,改用 rsync 了)
二。步骤#
2.1 云服务器创建 Git 仓库#
$ cd /opt/git/
$ git init --bare hexo.git # 创建 Hexo 远程仓库 git init --bare 仓库名.git
$ cd /opt/git/hexo.git/hooks/
$ vim post-receive
#!/bin/bash
git --work-tree=/opt/www/hexo --git-dir=/opt/git/hexo.git checkout -f
# --work-tree=/opt/www/hexo 设置工作树的路径
# --git-dir=/opt/git/hexo.git 设置存储库的路径
$ chmod +x post-receive # 给钩子文件添加执行权限
$ mkdir /opt/www/hexo # 创建工作树路径
$ chmod -R 777 /opt/www/hexo # 授权,否则到后面访问报 403
2.1.2 deployer 使用 rsync 进行部署#
前言:后面发现使用 git 上传至云服务器速度太慢了(不清楚啥原因),改用 rsync。
git 上传速度如下图
-
使 git bash 支持 rsync(Windows)
参考
https://blog.csdn.net/m0_48613893/article/details/124104757
https://blog.csdn.net/qq_38689395/article/details/125758842
如果文件重复,就跳过
如果出错就修改文件名.\usr\bin\msys-xxhash-0.8.0.dll → msys-xxhash-0.dll -
./_config.yml文件deploy: - type: rsync host: IP地址 user: 使用者名称 root: 远程主机的根目录 port: 22 delete: true verbose: true ignore_errors: false
在传大文件(也就 10M)时依旧很慢,但比前者速度快些。
2.2 配置本地 Hexo _config.yml#
./_config.yml文件,同步部署GitHub、Gitee、云服务器
# Deployment - 部署
## Docs: https://hexo.io/zh-cn/docs/one-command-deployment.html
deploy:
- type: git
repository: root@IP地址:/opt/git/hexo.git
branch: master
commit: Site updated
message: hexo {{ now('YYYYMMDD') }}
- type: git
repository: git@github.com:mycpen/mycpen.github.io.git
branch: main
commit: Site updated
message: hexo {{ now('YYYYMMDD') }} # message为自定义提交信息,默认为 Site updated: YYYY-MM-DD HH:mm:ss
- type: git
repository: git@gitee.com:mycpen/mycpen.gitee.io.git
branch: master
commit: Site updated
message: hexo {{ now('YYYYMMDD') }}
2.3 配置 SSH 免密登录#
- 本地 SSH 公钥 如
~/.ssh/id_rsa.pub内容添加至云服务器~/.ssh/authorized_keys中
2.4 部署#
- 本地 Hexo 源文件项目下敲
hexo clean ; hexo g ; hexo d
2.5 使用 NGINX 做 Web 服务器#
- 新增虚拟主机配置
$ cat /usr/local/nginx/conf/conf.d/blog.cpen.top.conf
server {
listen 80;
listen 443 ssl;
server_name blog.cpen.top;
error_page 404 /404.html;
# 强制HTTPS跳转
if ($scheme = "http") {
return 307 https://$host$request_uri;
}
root /opt/www/hexo;
ssl_certificate /usr/local/nginx/ssl/blog.cpen.top.pem;
ssl_certificate_key /usr/local/nginx/ssl/blog.cpen.top.key;
access_log /data/service_logs/nginx/blog.cpen.top_access.log misc;
error_log /data/service_logs/nginx/blog.cpen.top_error.log;
}
- 最后浏览器访问 https://blog.cpen.top/
三。注意点#
-
配置 SSH
免密; -
存储目录
授权;给工作树路径 /opt/www/hexo 777 权限,~~ 一开始没授权 访问报 403,环境为 CentOS 8.2;~~ 原因找到了,第 5 点。
-
开放端口。注意是否需要在云服务器新增
安全组策略,80、443 端口需要打开; -
SELinux 等原因。
-
403 权限问题
后面查询发现了403的原因,Nginx 编译安装时我指定了启动用户为 普通用户(默认为 nobody),导致起的工作进程权限不够。而 Hexo 上传的新的静态文件权限不为 777。所以修改 Nginx 的启动用户为 root。
$ vim /usr/local/nginx/conf/nginx.conf user root; $ nginx -s reload
四。参考文章#
使用 Git Hooks 部署至云服务器 新增 git 用户、授权目录
