网站搭建:通过Git部署到服务器
文章摘要
Efu Chat
安装 & 创建 Git用户
- 安装 Git
1
sudo apt-get install git
- 创建 Git 群组并添加git用户
1
2
3
4
5
6
7
8
9# Ubuntu
groupadd git
useradd -g git git
# Debian
groupadd git
usermod -a -g git git
# CentOS
groupadd git
useradd -g git git - 设置密码
1
passwd git
- 屏蔽git用户登录
1
2vim /etc/passwd
# 修改 git:x:1001:1001::/home/git:/bin/bash 为 git:x:1001:1001::/home/git:/usr/bin/git-shell把/bin/sh改为/usr/bin/git-shell,用户就只能用来克隆或者推送数据到远程 git 仓库,而不能用它登录到主机。
配置Nginx
使用面板的话,可以直接在面板上配置
这里以1panel为例
- 创建站点,选择静态站点
/opt/1panel/apps/openresty/openresty/www/sites/efu.me/index
即为下一步中的工作目录
- 设置目录所有者给git用户
1
2chown -R git:git /opt/1panel/apps/openresty/openresty/www/sites/efu.me
chown -R 755 /opt/1panel/apps/openresty/openresty/www/sites/efu.me
创建裸仓库
- 新建一个裸仓库
1
2
3mkdir -p /home/git/project/blog.git # -p 递归创建目录
cd /home/git/project/blog.git
git init --bare --initial-branch=master # --bare 创建裸仓库 --initial-branch=master 创建主分支 - 设置权限
1
chown -R git:git /home/git/project/blog.git
- 配置 Hook
1
vim hooks/post-receive
- 添加以下内容 –work-tree 指定工作目录(你在Nginx上设置的网站根目录),–git-dir 指定仓库目录
1
2
git --work-tree=/home/git/project/blog --git-dir=/home/git/project/blog.git checkout -f - 设置权限,让钩子可执行
1
chmod +x hooks/post-receive
配置Hexo
- 安装 hexo-deployer-git
1
npm install hexo-deployer-git --save
- 修改配置文件
1
2
3
4deploy:
type: git
repo: ssh://git@你的服务器IP:/home/git/project/blog.git
branch: master - 部署
1
hexo clean && hexo g && hexo d
效果
- 感谢您的赞赏。
赞赏名单
因为有你们的支持,我才体会到写文章的价值。
本文是原创文章,采用CC BY-NC-SA 4.0协议,完整转载请注明来自Ever Fu
评论 ()