0、序言

  • 本地已有 Hexo 博客
1
2
3
$ hexo -v
hexo: 7.2.0
hexo-cli: 4.3.2
  • 拥有一台Linux服务器
1
2
# cat /etc/system-release
CentOS Linux release 7.9.2009 (Core)

1、安装依赖

  • 服务器安装Git、Nginx服务
1
2
3
4
# git -v
git version 2.43.0
# nginx -v
nginx version: nginx/1.20.1

2、配置远程 Git 仓库

  • 2.1 创建 git 用户
1
2
useradd git
passwd git
  • 2.2 创建网站目录和远程仓库
1
2
3
4
5
6
7
8
9
10
// SSH远程连接服务器,使用 git 用户执行以下操作
# su - git
$ mkdir -p /home/git/data/www/blog
$ mkdir -p /home/git/repos/
$ git init --bare /home/git/repos/hexo.git

// 创建一个 hook
$ echo -e '#!/bin/sh
git --work-tree=/data/blog-web/hexo --git-dir=/opt/blog/hexo.git checkout -f' > /home/git/repos/hexo.git/hooks/post-receive
$ chmod +x /home/git/repos/hexo.git/hooks/post-receive

3、配置 Nginx

  • 3.1 修改 Nginx 的配置文件,使网站根目录指向博客网站目录
1
2
3
4
5
6
7
8
9
10
11
# vim /etc/nginx/conf/vhost/blog.conf
server {
listen 80;
server_name _;

location / {
root /home/git/data/www/blog;
index index.html index.htm;
}
# 其他配置
}
  • 3.2 重新加载 Nginx 服务
1
2
# nginx -t  # 检查 Nginx 配置文件是否有语法问题
# systemctl reload nginx # 重新加载 Nginx 服务,使修改后的配置生效

4、将本地 Hexo 部署到远程服务器

  • 4.1 测试远程仓库是否正常
1
2
3
4
5
6
7
8
9
// Windows配置免密
> ssh-copy-id -i C:\User\xxx\.ssh\id_rsa.pub git@192.168.31.93
> ssh git@192.168.31.93 //测试免密

// SSH远程服务器,只保留 git 用户的git功能
# sed -i '/^git:/s/\/bin\/bash/\/usr\/bin\/git-shell/' /etc/passwd

# 在本地任意目录,测试,可把远程服务器上的 hexo 空仓库克隆下来
> git clone root@192.168.31.93:/home/git/repos/hexo.git
  • 4.2 编辑本地博客全局配置文件_config.yml

    • 将url改为服务器IP或者域名
    1
    2
    3
    # 其他配置
    url: https://192.168.31.93
    # 其他配置
    • 修改 deploy 目标
    1
    2
    3
    4
    5
    # 其他配置
    deploy:
    type: 'git'
    repo: git@192.168.31.93:/home/git/repos/hexo.git
    branch: master
  • 4.3 部署博客

1
2
// 博客根目录打开 Windows CMD 或者 Git-Bash
> hexo clean && hexo g -d

5、验证

  • 浏览器访问
    • 域名或者IP