nginx的热部署及回退过程
系统中已经运行了一个版本的nginx的进程:
1、编译生成心得可执行程序
tar zxf nginx-1.16.0.tar.gz
vim auto/cc/gcc
./configure --prefix=/usr/local/nginx --with-file-aio
make 只make不安装
2、在线升级
cd ~/nginx-1.16.0/objs
cp -f nginx /usr/local/nginx/sbin/nginx
ps -ef | grep nginx
root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
nginx 18035 18034 0 10:45 ? 00:00:00 nginx: worker process
nginx 18036 18034 0 10:45 ? 00:00:00 nginx: worker process
root 18232 1406 0 10:48 pts/0 00:00:00 grep --color=auto nginx
kill -USR2 18034(master进程) //使worker不在接收请求,同时打开新版本的nginx的master进程,和它的两个线程,实现热升级
ps -ef | grep nginx //旧版进程和新版进程同时存在
root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
nginx 18035 18034 0 10:45 ? 00:00:00 nginx: worker process
nginx 18036 18034 0 10:45 ? 00:00:00 nginx: worker process
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 18127 18126 0 10:46 ? 00:00:00 nginx: worker process
nginx 18128 18126 0 10:46 ? 00:00:00 nginx: worker process
root 18232 1406 0 10:48 pts/0 00:00:00 grep --color=auto nginx
kill -WINCH 18034 //关闭旧版本master进程的两个worker线程
ps -ef | grep nginx //旧版本的两个worker线程已经关闭
root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 18127 18126 0 10:46 ? 00:00:00 nginx: worker process
nginx 18128 18126 0 10:46 ? 00:00:00 nginx: worker process
root 18829 1406 0 11:00 pts/0 00:00:00 grep --color=auto nginx
/usr/local/nginx/sbin/nginx -V //查看此时的nginx版本为 nginx/1.16.0
注:新版本上线完成后不要结束原来的master进程,防止上线失败需要版本回退;若完全成功后,便可结束原master进程
3、热回退:若上线失败,需在线回退为原来的版本
cd ~/nginx-1.17.1/objs
cp -f nginx /usr/local/nginx/sbin/nginx
kill -UIP 18034 //唤醒旧版本的master进程,使之产生新的worker线程
ps -ef | grep nginx
root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 20016 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20017 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20146 18034 0 11:26 ? 00:00:00 nginx: worker process
nginx 20147 18034 0 11:26 ? 00:00:00 nginx: worker process
root 20275 1406 0 11:28 pts/0 00:00:00 grep --color=auto nginx
kill -USR2 18126 //新版本的master进程的worker线程不再接收新的用户请求,使回退版本的worker进程接收新的用户请求。
ps -ef | grep nginx
root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 20016 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20017 18126 0 11:24 ? 00:00:00 nginx: worker process
nginx 20146 18034 0 11:26 ? 00:00:00 nginx: worker process
nginx 20147 18034 0 11:26 ? 00:00:00 nginx: worker process
root 20626 1406 0 11:35 pts/0 00:00:00 grep --color=auto nginx
kill -WINCH 18126 //关闭新版本master进程的两个worker进程
ps -ef | grep nginx //新版本的两个worker线程已经关闭
root 18034 1 0 10:45 ? 00:00:00 nginx: master process nginx
root 18126 18034 0 10:46 ? 00:00:00 nginx: master process nginx
nginx 20146 18034 0 11:26 ? 00:00:00 nginx: worker process
nginx 20147 18034 0 11:26 ? 00:00:00 nginx: worker process
root 20734 1406 0 11:38 pts/0 00:00:00 grep --color=auto nginx
kill -9 18126 //回退完成后可以结束掉新版本的master进程