热部署具体操作
旧版的nginx必须用绝对路径启动!!!
[root@web01 sbin]# /Learn_Nginx/nginx/sbin/nginx
(1)备份旧版本的nginx二进制文件
[root@web01 sbin]# pwd /Learn_Nginx/nginx/sbin
[root@web01 sbin]# mv nginx nginx.old
[root@web01 sbin]# ls
nginx.old
(2)检查旧版本nginx的编译参数
[root@web01 sbin]# nginx.old -V
nginx version: nginx/1.14.0 built by gcc 4.8.5 20150623 (Red Hat
4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/Learn_Nginx/nginx/
–with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio
(3)编译安装新版本nginx
#下载新nginx源码
[root@web01 Learn_Nginx]# wget
http://nginx.org/download/nginx-1.17.8.tar.gz
#解压源码
[root@web01 Learn_Nginx]# tar -zxvf nginx-1.17.8.tar.gz
#编译三部曲
[root@web01 Learn_Nginx]# cd nginx-1.17.8/ [root@web01 nginx-1.17.8]#
./configure --prefix=/Learn_Nginx/nginx/ --with-http_ssl_module
–with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio [root@web01 nginx-1.17.8]# make && make install
(4)此时发现存在2个版本的nginx程序
[root@web01 sbin]# ls
nginx nginx.old
(5)替换旧的nginx可执行文件
[root@web01 nginx-1.17.8]# cp -a /Learn_Nginx/nginx-1.17.8/objs/nginx/Learn_Nginx/nginx/sbin/
(5.1)检查nginx进程,这里用绝对路径启动nginx
[root@web01 sbin]# ps -ef |grep nginx
root 36969 1 0 21:18 ? 00:00:00 ngin: master process /Learn_Nginx/nginx/sbin/nginx
nobody 36970 36969 0 21:18 ? 00:00:00 ngin: worker process
nobody 36971 36969 0 21:18 ? 00:00:00 ngin: worker process
nobody 36972 36969 0 21:18 ? 00:00:00 ngin: worker process
nobody 36973 36969 0 21:18 ? 00:00:00 ngin: worker process
root 39616 36692 0 21:47 pts/0 00:00:00 grep --color=auto
nginx
(6)发送USR2信号给旧版本进程,使得nginx旧版本停止接受请求,切换为新nginx版本
[root@web01 sbin]# kill -USR2 `cat …/logs/nginx.pid`
(7)检查此时的nginx进程
nginx-master首先会重命名pid文件,在文件后面添加.oldbin后缀 然后会再启动一个新的master进程以及worker,且使用的是新版Nginx nginx能够自动将新来的请求,过度到新版master进程下,实现平滑过度
#可以发现新的master进程由旧master启动,由PPID可看出
[root@web01 sbin]# ps -ef |grep nginx root 36969 1 0 21:18 ? 00:00:00 ngin: master process /Learn_Nginx/nginx/sbin/nginx
nobody 36970 36969 0 21:18 ? 00:00:00 ngin: worker process
nobody 36971 36969 0 21:18 ? 00:00:00 ngin: worker process
nobody 36972 36969 0 21:18 ? 00:00:00 ngin: worker process
nobody 36973 36969 0 21:18 ? 00:00:00 ngin: worker process
root 39646 36969 0 22:07 ? 00:00:00 ngin: master process
/Learn_Nginx/nginx/sbin/nginx nobody 39647 39646 0 22:07 ?
00:00:00 ngin: worker process nobody 39648 39646 0 22:07 ?
00:00:00 ngin: worker process nobody 39649 39646 0 22:07 ?
00:00:00 ngin: worker process nobody 39650 39646 0 22:07 ?
00:00:00 ngin: worker process root 39653 36692 0 22:09 pts/0
00:00:00 grep --color=auto nginx [root@web01 sbin]# ls …/logs/
access.log error.log nginx.pid nginx.pid.oldbin
(8)发送WINCH信号给旧master进程,优雅的关闭旧worker进程
[root@web01 sbin]# kill -WINCH
cat ../logs/nginx.pid.oldbin
(9)再次检查进程情况,旧master的worker已经关闭了,旧master不会自己退出,用作版本回退
[root@web01 sbin]# ps -ef |grep nginx root 36969 1 0 21:18 ? 00:00:00 ngin: master process /Learn_Nginx/nginx/sbin/nginx
root 39646 36969 0 22:07 ? 00:00:00 ngin: master process
/Learn_Nginx/nginx/sbin/nginx nobody 39647 39646 0 22:07 ?
00:00:00 ngin: worker process nobody 39648 39646 0 22:07 ?
00:00:00 ngin: worker process nobody 39649 39646 0 22:07 ?
00:00:00 ngin: worker process nobody 39650 39646 0 22:07 ?
00:00:00 ngin: worker process root 39667 36692 0 22:17 pts/0
00:00:00 grep --color=auto nginx
(10)如果你觉得没问题了,可以关闭旧master进程
[root@web01 sbin]# kill 36969
[root@web01 sbin]# ps -ef |grep nginx
root 39646 1 0 22:07 ? 00:00:00 ngin: master process /Learn_Nginx/nginx/sbin/nginx nobody 39647 39646 0 22:07 ?
00:00:00 ngin: worker process nobody 39648 39646 0 22:07 ?
00:00:00 ngin: worker process nobody 39649 39646 0 22:07 ?
00:00:00 ngin: worker process nobody 39650 39646 0 22:07 ?
00:00:00 ngin: worker process root 39670 36692 0 22:20 pts/0
00:00:00 grep --color=auto nginx