使用 Github Deploy Key 部署、更新网站
目录
使用 Github Deploy Key 部署、更新网站
1 背景
原来习惯用 BeyondCompare 通过 ftp、sftp 更新网站,但网站的特点是文件数量众多、小文件多,用这种方式效率很低,于是改用 git 方式进行更新。
2 生成、添加密钥
-
在服务器上生成 ssh 密钥:
-
ssh-keygen -t rsa -b 4096 -C "电子邮箱@email.com"
(这里的电子邮件地址与你的 GitHub 账号无关) -
1 2 3 4 5 6
Enter file in which to save the key (/home/user/.ssh/id_rsa): # 密钥保存路径,这里保持默认 Enter passphrase (empty for no passphrase): # 给密钥设置密码,可以留空(这样以后使用的时候就不用输密码) Enter same passphrase again: # 确认密码,和上面一样
-
-
提交公钥
-
验证
- 此时在服务器上执行
ssh -T [email protected]
命令 - 会显示:
Hi HonorKnight8/HelloWorld.Monster! You've successfully authenticated, but GitHub does not provide shell access.
- 此时在服务器上执行
3 git 部署、更新
-
服务器安装git
-
1 2
sudo yum install -y git git --version
-
-
备份一下当前的网站目录
-
1 2 3 4
cd ~/www # 这里是我的站点路径 tar -zcvf html.bak0713a.tar.gz html
-
-
清空 html 文件夹
-
1 2 3 4
cd html ll rm -rf * ll
-
-
git 初始化、拉取文件
-
1 2 3
git init git pull [email protected]:HonorKnight8/HelloWorld.Monster.git # 以后更新也是用这条命令
-
注意:这里要用 repository 的 ssh 链接,不是默认的 https 链接
-
-
完工,打开网站验证一下
4 文件权限调整
这样子还存在一点小问题,网站根目录下存在 .git 文件夹,比如访问 http://helloworld.monster/.git/index
是能访问到这个文件的
如果不想被访问到这个文件夹及其中的文件,可以设置一下权限:chmod -R 700 .git
如果开启了 SELinux ,还可以多设一步:chcon -R -v -t user_home_dir_t .git