VPS配置Git Hooks实现hexo博客更新

客户端使用Hexo生成静态文件,通过Git推送到VPS的Git仓库。VPS配置Git Hooks,将静态文件同步到站点目录,实现hexo博客更新。

本机准备工作

  • 安装Hexo环境
  • 安装Git
  • 生成ssh秘钥

VPS配置工作

VPS以CentOS 7为例,使用Xshell5连接到VPS,登录root账户。

安装Git

1
2
3
git --version    # 查看系统是否安装git
yum install git # 安装git
yum remove git # 卸载git

新建用户

1
2
3
adduser git
chmod 740 /etc/sudoers
vi /etc/sudoers

在vi编辑器中找到如下行

1
2
## Allow root to run any commands anywhere
root ALL=(ALL) ALL

在下面新增一行

1
git   ALL=(ALL)     ALL

保存后退出(linux命令 :wq),执行

1
chmod 440 /etc/sudoers

新建git仓库

1
2
3
4
5
6
7
8
su git
cd ~
mkdir .ssh && cd .ssh
touch authorized_keys
vi authorized_keys # 粘贴进本地机器的ssh公钥(一般在C:\Users\用户名\.ssh\id_rsa.pub文件中)
cd ~
mkdir hexo.git && cd hexo.git
git init --bare # 初始化git仓库

ssh连接测试

git bash命令行中输入ssh git@VPS的IP地址,如果能登录远程主机,则表示Git配置成功

ssh可同时支持publickey和password两种授权方式,publickey默认不开启,开启配置为yes。
如果客户端不存在.ssh/id_rsa,则使用password授权。
存在则使用publickey授权;如果publickey授权失败,依然会继续使用password授权。

赋予git用户对网站目录的权限

1
2
3
cd /var/www/html                        # apache服务器的网站根目录
mkdir blog # 新建blog文件夹用以放置hexo博客文件
chown git:git -R /var/www/html/blog # 将目录所有者改为git用户

配置Git Hooks

1
2
3
4
su git
cd /home/git/hexo.git/hooks
touch post-receive # 新建脚本文件
vi post-receive

在vi编辑中输入以下代码后保存退出

1
2
3
4
5
6
7
8
9
#!/bin/bash
GIT_REPO=/home/git/hexo.git # git仓库
TMP_GIT_CLONE=/tmp/hexo
PUBLIC_WWW=/var/www/html/blog # 网站目录
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}
chmod +x post-receive # 赋予脚本的执行权限

本机配置工作

进入本地hexo博客文件夹,打开站点配置文件_config.yml,修改deploy选项

1
2
3
4
5
6
7
deploy:
type: git
repo:
github: git@github.com:ChangingFond/ChangingFond.github.io.git #同步到GitHub
vps: git@vps的ip:hexo.git仓库地址,master #同步到自己的VPS
branch: master
message: Hexo Blog updated - {{ now('YYYY-MM-DD HH:mm:ss') }})

按住shift右击,选择在此处打开命令窗口,运行hexo g -d,如果一切正常,静态文件已经被成功的push到了blog的仓库里。
至此,我们完成了通过git将hexo博客部署到VPS上的功能。

参考资料

感谢以下作者的文章对笔者的帮助

常见问题

  • ssh使用公钥授权免密登录不通过Permission denied(publickey,gssapi-keyex,gssapi-with-mic)

    尝试以下步骤:
    1.运行vi /etc/ssh/sshd_config,找到以下代码,按如下格式修改

    1
    2
    3
    4
    RSAAuthentication yes
    PubkeyAuthentication yes
    StrictModes no
    AuthorizedKeysFile .ssh/authorized_keys

    保存后退出,重启sshd服务

    1
    2
    3
    4
    5
    6
    # sshd服务相关命令
    systemctl restart sshd.service
    systemctl status sshd.service # 查看ssh服务的状态
    systemctl start sshd.service # 开启ssh服务
    sytemctl enable sshd.service # ssh服务随开机启动,反之disabled
    systemctl stop sshd.ervice # 停止ssh服务

    2.修改文件夹以及文件权限

    1
    2
    chmod 700 /home/git/.ssh
    chmod 644 /home/git/.ssh/authorized_keys

    3.关闭SELinux

    1
    2
    3
    4
    setenforce 0            # 暂时关闭,重启后恢复

    vi /etc/selinux/config # 永久关闭
    SELINUX=disabled

VPS配置Git Hooks实现hexo博客更新

http://blog.fcj.one/hexo-git-vps.html

作者

ChangingFond

发布于

2017-09-07

更新于

2017-09-07

许可协议

评论