1. 目标 将 hexo 部署到 centos 服务器上。
2. 步骤 (1)centos 安装 git
1 2 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel yum install -y git
(2)创建用户并配置仓库 1 2 3 4 5 6 7 useradd git passwd git su git cd /home/git/mkdir -p project/hexo-blog mkdir repos && cd repos git init --bare hexo-blog-repo.git
(3)创建钩子函数 1 2 cd hexo-blog-repo.git/hooks vim post-receive
1 2 #!/bin/sh git --work-tree=/home/git/project/hexo-blog --git-dir=/home/git/repos/hexo-blog-repo.git checkout -f
验证一下(也可以本地推送完在验证)
(4)生成.ssh
文件夹并设置 SSH 密钥对 1 2 3 4 5 cd ~/.ssh mkdir ~/.ssh chmod 700 ~/.ssh ssh-keygen -t rsa -b 4096 -C "centos@mail.com" chmod 600 ~/.ssh/id_rsa
(5)创建 authorized_keys
以及配置权限 1 2 3 cd /home/git/.sshtouch authorized_keys chmod 600 authorized_keys
(6)建立 SSH
信任关系 1 2 3 cd ~ssh-copy-id -i .ssh/id_rsa.pub git@server_ip ssh git@server_ip
(7)安装 Nginx
1 2 3 4 5 sudo yum updatesudo yum install nginxsudo systemctl start nginx sudo systemctl enable nginx sudo systemctl status nginx
浏览器输入 ip 地址验证:
(8)配置 nginx
如果你的配置文件位于 /etc/nginx/conf.d/
目录下,你可以编辑该目录下的默认配置文件或创建一个新的配置文件:
1 sudo vim /etc/nginx/conf.d/default.conf
1 2 3 4 5 6 7 8 server { listen 80; server_name tangxdou.com www.tangxdou.com; root /home/git/project/hexo-blog; index index.html index.htm; }
重新加载 Nginx 的配置:
1 sudo systemctl reload nginx
或者重启 Nginx 服务:
1 sudo systemctl restart nginx
(9)配置 Hexo
1 2 3 4 deploy: type : git repo: git@server_ip:/home/git/repos/hexo-blog-repo.git branch: master
(10)限制 git 用户的权限 为了安全起见,最好是将 git 用户的权限设置为只能执行 git clone
, git push
命令等等:
1 2 3 cat /etc/shells which git-shell vim /etc/shells
添加第 2 步显示出来的路径,通常为 /usr/bin/git-shell
1 2 3 4 5 6 7 /bin/sh /bin/bash /usr/bin/sh /usr/bin/bash /bin/tcsh /bin/csh /usr/bin/git-shell
同时修改 /etc/passwd 文件内容,更改权限:
1 2 3 4 git:x:1001:1001::/home/git:/bin/bash git:x:1001:1001::/home/git:/usr/bin/git-shell
测试一下:
如果登录不上,就说明正常。
1 2 3 4 Last login: Mon May 27 17:11:51 2024 from 1xx.2xx.3xx.4xx fatal: Interactive git shell is not enabled. hint: ~/git-shell-commands should exist and have read and execute access. Connection to 1xx.2xx.3xx.4xx closed.
3. 相关参考 https://blog.csdn.net/jiunian_2761/article/details/122908142