1、 目的
升级一般是添加新的模块,或者升级版本。下面将演示添加echo-nginx-module-0.61模块。
首先下载并解压echo-nginx-module-0.61模块到nginx安装包同级目录:
[root@localhost nginx-1.16.1]# ls /usr/src
debug echo-nginx-module-0.61 echo-nginx-module-0.61.tar.gz kernels nginx-1.16.1 nginx-1.16.1.tar.gz
方案一:
添加模块,重新编译
[root@localhost nginx-1.16.1]# pwd
/usr/src/nginx-1.16.1
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --add-module=../echo-nginx-module-0.61
[root@localhost nginx-1.16.1]#make
[root@localhost nginx-1.16.1]#make install
#只会重新生成二进制文件,不会覆盖其他文件
[root@localhost nginx-1.16.1]# ls /usr/local/nginx/sbin/
nginx nginx.old
查看pid
[root@localhost nginx-1.16.1]# ps -ef|grep nginx
root 13529 1 0 19:10 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13530 13529 0 19:10 ? 00:00:00 nginx: worker process
nobody 13531 13529 0 19:10 ? 00:00:00 nginx: worker process
nobody 13532 13529 0 19:10 ? 00:00:00 nginx: worker process
nobody 13533 13529 0 19:10 ? 00:00:00 nginx: worker process
root 13595 9891 0 19:17 pts/0 00:00:00 grep --color=auto nginx
升级
[root@localhost nginx-1.16.1]# make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
再次查看pid,和升级前pid不同,升级完成
[root@localhost nginx-1.16.1]# ps -ef|grep nginx
root 13601 1 0 19:17 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13603 13601 0 19:17 ? 00:00:00 nginx: worker process
nobody 13604 13601 0 19:17 ? 00:00:00 nginx: worker process
nobody 13605 13601 0 19:17 ? 00:00:00 nginx: worker process
nobody 13606 13601 0 19:17 ? 00:00:00 nginx: worker process
root 13612 9891 0 19:17 pts/0 00:00:00 grep --color=auto nginx
方案二:
将方案一的二进制文件还原,进行方案二试验
[root@localhost nginx-1.16.1]# rm -rf /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.16.1]# mv /usr/local/nginx/sbin/nginx.old /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -V
nginx version: MYWS/1.8
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
查看pid
[root@localhost nginx-1.16.1]# !ps
ps -ef|grep nginx
root 13601 1 0 19:17 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13603 13601 0 19:17 ? 00:00:00 nginx: worker process
nobody 13604 13601 0 19:17 ? 00:00:00 nginx: worker process
nobody 13605 13601 0 19:17 ? 00:00:00 nginx: worker process
nobody 13606 13601 0 19:17 ? 00:00:00 nginx: worker process
root 13646 9891 0 19:28 pts/0 00:00:00 grep --color=auto nginx
生成新的master进程
[root@localhost nginx-1.16.1]# kill -USR2 13601
再次查看pid,新老进程同时存在。新进程一旦被拉起,所有请求将由新进程进行处理,老进程将不再处理
[root@localhost nginx-1.16.1]# !ps
ps -ef|grep nginx
root 13601 1 0 19:17 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13603 13601 0 19:17 ? 00:00:00 nginx: worker process
nobody 13604 13601 0 19:17 ? 00:00:00 nginx: worker process
nobody 13605 13601 0 19:17 ? 00:00:00 nginx: worker process
nobody 13606 13601 0 19:17 ? 00:00:00 nginx: worker process
root 13647 13601 0 19:29 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13648 13647 0 19:29 ? 00:00:00 nginx: worker process
nobody 13649 13647 0 19:29 ? 00:00:00 nginx: worker process
nobody 13650 13647 0 19:29 ? 00:00:00 nginx: worker process
nobody 13651 13647 0 19:29 ? 00:00:00 nginx: worker process
root 13653 9891 0 19:29 pts/0 00:00:00 grep --color=auto nginx
老worker进程优雅退出
[root@localhost nginx-1.16.1]# kill -WINCH 13601
[root@localhost nginx-1.16.1]# !ps
ps -ef|grep nginx
root 13601 1 0 19:17 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 13647 13601 0 19:29 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13648 13647 0 19:29 ? 00:00:00 nginx: worker process
nobody 13649 13647 0 19:29 ? 00:00:00 nginx: worker process
nobody 13650 13647 0 19:29 ? 00:00:00 nginx: worker process
nobody 13651 13647 0 19:29 ? 00:00:00 nginx: worker process
root 13656 9891 0 19:33 pts/0 00:00:00 grep --color=auto nginx
抉择1:观察一段时间如果新的进程没问题,干掉老进程,升级完成
kill -QUIT 13601
抉择2:观察一段时间如果新的进程有问题,则重新拉起老进程
[root@localhost nginx-1.16.1]# kill -HUP 13601
[root@localhost nginx-1.16.1]# !ps
ps -ef|grep nginx
root 13601 1 0 19:17 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 13647 13601 0 19:29 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13648 13647 0 19:29 ? 00:00:00 nginx: worker process
nobody 13649 13647 0 19:29 ? 00:00:00 nginx: worker process
nobody 13650 13647 0 19:29 ? 00:00:00 nginx: worker process
nobody 13651 13647 0 19:29 ? 00:00:00 nginx: worker process
nobody 13658 13601 0 19:35 ? 00:00:00 nginx: worker process
nobody 13659 13601 0 19:35 ? 00:00:00 nginx: worker process
nobody 13660 13601 0 19:35 ? 00:00:00 nginx: worker process
nobody 13661 13601 0 19:35 ? 00:00:00 nginx: worker process
root 13663 9891 0 19:35 pts/0 00:00:00 grep --color=auto nginx
干掉新的进程,还原到老进程
[root@localhost nginx-1.16.1]# kill -QUIT 13647
[root@localhost nginx-1.16.1]# !ps
ps -ef|grep nginx
root 13601 1 0 19:17 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13658 13601 0 19:35 ? 00:00:00 nginx: worker process
nobody 13659 13601 0 19:35 ? 00:00:00 nginx: worker process
nobody 13660 13601 0 19:35 ? 00:00:00 nginx: worker process
nobody 13661 13601 0 19:35 ? 00:00:00 nginx: worker process
root 13665 9891 0 19:36 pts/0 00:00:00 grep --color=auto nginx