知识总结

CODE IS POETRY

centos7 git+nginx自动部署项目

git项目源只管理版本,是不能直接执行的,需在服务器上git clone 项目源,再启动这个克隆项目。
Git代码上传到服务器,默认是不会自动更新服务器上的克隆项目,所以需要配置自动克隆,才能达到客户端上传后,服务器端自动克隆部署的效果。

1、创建项目文件夹

1
2
cd /home/project
git clone /home/git-repos/test.git

此时/home/project里面克隆是test项目

为了让nginx来管理项目,需要把project项目权限改给nginx

1
2
3
cd /home
chown –R nginx:nginx project
chown -R nginx:nginx git-repos

为了验证自动部署,先配置nginx添加project目录的路由

1
vi  /etc/nginx/conf.d/test.conf

修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
server {
listen 80;
server_name 有域名写域名,无域名写服务器ip;

location / {
root /home/project/test;
index index.html;
}
location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {
client_max_body_size 100m;
auth_basic "Git User Authentication";
auth_basic_user_file /home/httpwd/gitpass.db;
root /home/git-repos;
}

location ~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
client_max_body_size 100m;
auth_basic "Git User Authentication";
auth_basic_user_file /home/httpwd/gitpass.db;
root /home/git-repos;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_connect_timeout 24h;
fastcgi_read_timeout 24h;
fastcgi_send_timeout 24h;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend
fastcgi_param PATH_INFO $uri;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /home/git-repos
fastcgi_param REMOTE_USER $remote_user;
include fastcgi_params;
}
}

这样http://ip/绑定的目录为/home/project/test,本地修改test项目上传后且自动部署后,nginx 会自动加载

2、添加自动部署代码

1
2
cd /home/git-repos/test.git/hooks
vi post-update

填入如下代码

1
2
3
4
5
WEB_PATH='/home/project/test'  #网站根目录
unset GIT_DIR
cd $WEB_PATH
git reset --hard
git pull

保存后修改权限且此文件可执行

1
2
chown nginx:nginx post-update
chmod 775 post-update

修改本地文件,提交后,不用再到服务器git pull,浏览器查看http://ip/和http://ip/sub.html可以看到更新了


© 2020 Tung

粤ICP备19047572号