本次把nginx-1.8.1升级到最新稳定版nginx-1.16.1
1、升级
#查看nginx进程
[root@localhost ~]# ps -ef |grep nginx
root 13422 1 0 11:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13423 13422 0 11:37 ? 00:00:00 nginx: worker process
root 13741 999 0 11:37 pts/0 00:00:00 grep --color=auto nginx
#更改配置文件为4个工作进程,然后重载配置文件
[root@localhost ~]#sed -i /worker_processes/s/1/4/g /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# /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
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# !ps
ps -ef |grep nginx
root 13422 1 0 11:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 18873 13422 0 11:42 ? 00:00:00 nginx: worker process
nobody 18874 13422 0 11:42 ? 00:00:00 nginx: worker process
nobody 18875 13422 0 11:42 ? 00:00:00 nginx: worker process
nobody 18876 13422 0 11:42 ? 00:00:00 nginx: worker process
root 18958 999 0 11:42 pts/0 00:00:00 grep --color=auto nginx
#备份老nginx的二进制文件
[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ll
总用量 3272
-rwxr-xr-x. 1 root root 3350032 2月 28 11:36 nginx
[root@localhost sbin]# cp nginx nginx.old
#查看老nginx的版本号及编译安装信息
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.8.1
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module
#下载新版本安装包,执行预编译和编译。切记不可执行make install,否则会清空配置文件
root@localhost ~]# tar zxf nginx-1.16.0.tar.gz
[root@localhost ~]# ll
总用量 1836
-rw-------. 1 root root 1291 7月 23 2019 anaconda-ks.cfg
drwxr-xr-x. 8 1001 1001 158 4月 23 2019 nginx-1.16.0
-r--------. 1 root root 1032345 4月 30 2019 nginx-1.16.0.tar.gz
drwxr-xr-x. 9 1001 1001 186 2月 28 11:34 nginx-1.8.1
-r--------. 1 root root 833473 2月 28 11:29 nginx-1.8.1.tar.gz
-rw-r--r--. 1 root root 177 2月 27 14:36 web.yml
[root@localhost ~]# cd nginx-1.16.0
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module
[root@localhost nginx-1.16.0]# make
#拷贝新nginx的二进制文件到原sbin目录下
[root@localhost nginx-1.16.0]# cp objs/nginx /usr/local/nginx/sbin/ -f
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y
[root@localhost sbin]# ll
总用量 7044
-rwxr-xr-x. 1 root root 3858992 2月 28 11:53 nginx
-rwxr-xr-x. 1 root root 3350032 2月 28 11:55 nginx.old
[root@localhost sbin]# ps -ef |grep nginx
root 13422 1 0 11:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 18873 13422 0 11:42 ? 00:00:00 nginx: worker process
nobody 18874 13422 0 11:42 ? 00:00:00 nginx: worker process
nobody 18875 13422 0 11:42 ? 00:00:00 nginx: worker process
nobody 18876 13422 0 11:42 ? 00:00:00 nginx: worker process
root 35837 999 0 11:56 pts/0 00:00:00 grep --color=auto nginx
#对nginx进行热部署,平滑升级nginx,给nginx发送一个信号,告诉nginx我们要开始进行热部署了
[root@localhost sbin]# kill -USR2 13422
[root@localhost sbin]# ps -ef |grep nginx
root 13422 1 0 11:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 18873 13422 0 11:42 ? 00:00:00 nginx: worker process
nobody 18874 13422 0 11:42 ? 00:00:00 nginx: worker process
nobody 18875 13422 0 11:42 ? 00:00:00 nginx: worker process
nobody 18876 13422 0 11:42 ? 00:00:00 nginx: worker process
root 38136 13422 0 11:58 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 38137 38136 0 11:58 ? 00:00:00 nginx: worker process
nobody 38138 38136 0 11:58 ? 00:00:00 nginx: worker process
nobody 38139 38136 0 11:58 ? 00:00:00 nginx: worker process
nobody 38140 38136 0 11:58 ? 00:00:00 nginx: worker process
root 38560 999 0 11:58 pts/0 00:00:00 grep --color=auto nginx
#nginx会启动一个新的nginx master进程,新的master进程是使用新版本的nginx二进制文件启动的,而老的worker进程也在运行,新的master进程会启动新的worker进程
#向老的nginx进程发送一个信号,让老master进程优雅的关闭老的worker进程
[root@localhost sbin]# kill -WINCH 13422
[root@localhost sbin]# ps -ef |grep nginx
root 13422 1 0 11:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 38136 13422 0 11:58 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 38137 38136 0 11:58 ? 00:00:00 nginx: worker process
nobody 38138 38136 0 11:58 ? 00:00:00 nginx: worker process
nobody 38139 38136 0 11:58 ? 00:00:00 nginx: worker process
nobody 38140 38136 0 11:58 ? 00:00:00 nginx: worker process
root 45316 999 0 12:05 pts/0 00:00:00 grep --color=auto nginx
#可以看到,老的worker进程已经关闭了,新的访问请求由新的worker进程进行处理,但是可以看到老的master进程还存在,建议保留,以备版本回退
#验证nginx是否升级成功
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.16.0
#此时,nginx-1.8.1已经平滑升级到了最新稳定版nginx-1.16.0