git

通过Git将Hexo部署到自己的服务器上

这个问题困扰了我两周之长!

Posted by WK on 2021-10-13
Estimated Reading Time 2 Minutes
Words 535 In Total
Viewed Times

以下操作都在git中完成,如果你在cmd或者别的win终端中进行,你会发现一些命令你无法使用

基本准备

1.服务器安装了Nginx和git,具体方法略

准备(建立SSH信任关系)

1.在本地生成公钥和密钥:

1
ssh-keygen -t rsa 

2.将本机生成的公钥发送到服务器上(建立信任关系):

1
ssh-copy-id -i C:/Users/UserName/.ssh/id_rsa.pub root@server_ip 

3.测试ssh远程登录是否成功:

1
ssh root@server_ip 

这里如果不在提示输入密码,即证明本机与web服务器建立了ssh信任关系

4.注意:因为这里是本机与服务器的root用户建立关系,后面部署hexo用的是git用户,因此在后面服务器配置时还需要重复此步骤,进行本机与服务器的git用户建立ssh信任关系

服务端配置

1.新建git用户并设置密码:

1
adduser git       
1
passwd git

2.编辑配置文件,加入git到sudo用户组:

1
vi  /etc/sudoers
1
2
3
## Allow root to run any commands anywhere 
root ALL=(ALL) ALL
git ALL=(ALL) ALL ##新增这一行

3.配置Git仓库:

将网站目录放到/home/git/

1
2
3
mkdir /home/git/blog
mkdir -p /home/git/blog/hexo.git #Git仓库,不存储网站文件
mkdir /home/git/blog/hexo #实际存储网站文件目录

初始化空的Git仓库:

1
git init --bare /home/git/blog/hexo.git

进入该仓库,配置post-update hooks:

1
2
cd /home/git/blog/hexo.git/hooks
vi post-update.sample

在最下面一行添加这句话

1
git --work-tree=/home/git/blog/hexo --git-dir=/home/git/blog/hexo.git checkout -f

重命名:

1
mv post-update.sample post-update

赋予可执行权限:

1
chmod +x post-update

4.将git用户与本机建立ssh信任关系

方法见上

5.nginx web server配置

修改配置文件:

将root改为你的hexo主目录

1
2
3
4
5
 server {
......
\# root /var/www/html;
root /home/git/blog/hexo;
}

本地配置

编辑_config.yml文件,找到deploy项目,修改如下:

1
2
3
4
deploy:
type: git #用户名
repository: git@server_ip:/home/git/blog/hexo.git #仓库地址
branch: master #分支

保存

使用命令:hexo clean && hexo g && hexo d测试是否能够成功部署


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !