学习nginx热部署实战
检查当前安装的nginx版本
[root@gujipeng ~]# nginx -v
nginx version: nginx/1.18.0
备份旧的二进制命令
修改当前的nginx
[root@gujipeng ~]# cd /opt/nginx-1.18sbin/
[root@gujipeng sbin]# mv nginx nginx.1.18
检查旧的二进制命令编译参数
[root@gujipeng sbin]# nginx.1.18 -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx-1.18 --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio
准备一个新的nginx程序版本,编译安装
http://nginx.org/download/nginx-1.16.1.tar.gz
[root@gujipeng opt]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@gujipeng opt]# tar -zxvf nginx-1.16.1.tar.gz
[root@gujipeng opt]# cd nginx-1.16.1
[root@gujipeng nginx-1.16.1]# ./configure --prefix=/opt/nginx-1.18 --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio
[root@gujipeng nginx-1.16.1]# make
[root@gujipeng nginx-1.16.1]# make install
检查新版的nginx信息
[root@gujipeng nginx-1.16.1]# cd /opt/nginx-1.18/sbin/
[root@gujipeng sbin]# ls
nginx nginx.1.18
再次检查当前系统的nginx状态
[root@gujipeng sbin]# ./nginx -v
nginx version: nginx/1.16.1
[root@gujipeng sbin]# ./nginx.1.18 -v
nginx version: nginx/1.18.0
[root@gujipeng sbin]# ps -ef |grep nginx
root 20311 1 0 15:12 ? 00:00:00 nginx: master process nginx
nobody 20312 20311 0 15:12 ? 00:00:00 nginx: worker process
root 20314 13589 0 15:12 pts/0 00:00:00 grep --color=auto nginx
发送一个USR2信号给旧的master process,使得nginx旧的版本停止接收用户请求,并且切换为新的nginx版本
[root@gujipeng sbin]# kill -USR2 'cat /opt/nginx-1.18/logs/nginx.pid '
[root@gujipeng sbin]# ps -ef |grep nginx
root 20311 1 0 15:12 ? 00:00:00 nginx: master process nginx
nobody 20312 20311 0 15:12 ? 00:00:00 nginx: worker process
root 20335 20311 0 15:13 ? 00:00:00 nginx: master process nginx
nobody 20336 20335 0 15:13 ? 00:00:00 nginx: worker process
root 20338 13589 0 15:13 pts/0 00:00:00 grep --color=auto nginx
[root@gujipeng sbin]# ls ../logs/
access.log error.log nginx.pid nginx.pid.oldbin
发送WINCH信号,给旧的master进程,让旧的master进程优雅的退出
[root@gujipeng sbin]# kil -WINCH 'cat /opt/nginx-1.18/logs/nginx.pid.oldbin'
再次检查进程情况,旧master的worker已经关闭了,旧master不会自己退出,用作版本回退
[root@gujipeng sbin]# ps -ef|grep nginx
root 20311 1 0 15:12 ? 00:00:00 nginx: master process nginx
root 20335 20311 0 15:13 ? 00:00:00 nginx: master process nginx
nobody 20336 20335 0 15:13 ? 00:00:00 nginx: worker process
root 20607 13589 0 15:25 pts/0 00:00:00 grep --color=auto nginx
新的nginx服务一切正常,干掉旧的master主进程
[root@gujipeng sbin]# kill 20311
[root@gujipeng sbin]# ps -ef|grep nginx
root 20335 1 0 15:13 ? 00:00:00 nginx: master process nginx
nobody 20336 20335 0 15:13 ? 00:00:00 nginx: worker process
root 20665 13589 0 15:28 pts/0 00:00:00 grep --color=auto nginx
nginx版本热部署,热跟换实验结束