1.下载nginx 源码包,下载地址:http://nginx.org
2.使用远程工具上传至相应的服务器(putty、xshell)
3.解压
示例:[root@shuaishuai ~]# tar -xf nginx-1.19.5.tar.gz
4.切换目录
示例:[root@shuaishuai ~]# cd nginx-1.19.5
5.查看之前nginx所安装的模块
示例:[root@shuaishuai nginx-1.19.5]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.15.10built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=../openssl-1.1.1 --with-openssl-opt='enable-tls1_3 enable-ec_nistp_64_gcc_128' --with-pcre --without-http_gzip_module
6.预配置新版本nginx,且进行编译
示例: [root@shuaishuai nginx-1.19.5]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=../openssl-1.1.1 --with-openssl-opt='enable-tls1_3 enable-ec_nistp_64_gcc_128' --with-pcre --without-http_gzip_module && make
注:安装路径需与旧版一致,模块也需要与旧版的一致;此处千万不能执行make install
7.备份旧二进制文件nginx
示例:[root@shuaishuai nginx-1.19.5]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
8.用新的二进制文件nginx替换旧的
示例:[root@shuaishuai nginx-1.19.5]# cp -p objs/nginx /usr/local/nginx/sbin/nginx
9. 确保配置文件无误
示例:[root@shuaishuai nginx-1.19.5]# /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
10. 向主进程(master)发送USR2信号,Nginx会启动一个新版本的master进程和对应工作进程,和旧版一起处理请求
[root@shuaishuai nginx-1.19.5]# ps aux | grep nginx
root 19658 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 16404 0.0 0.7 47532 3548 ? S 14:54 0:00 nginx: worker process
root 21993 0.0 0.2 112724 996 pts/1 S+ 20:42 0:00 grep --color=auto nginx
[root@shuaishuai nginx-1.19.5]# kill -USR2 19658
[root@shuaishuai nginx-1.19.5]# ps aux | grep nginx
root 19658 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 16404 0.0 0.7 47532 3548 ? S 14:54 0:00 nginx: worker process
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process
root 21997 0.0 0.2 112724 996 pts/1 R+ 20:44 0:00 grep --color=auto nginx
11. 向旧的Nginx主进程(master)发送WINCH信号,它会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理
[root@shuaishuai nginx-1.19.5]# kill -WINCH 19658
[root@shuaishuai nginx-1.19.5]# ps aux | grep nginx
root 19658 0.0 0.5 47136 2684 ? Ss 14:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process
注:
如果这时需要回退继续使用旧版本,可向旧的Nginx主进程发送HUP信号,它会重新启动工作进程, 仍使用旧版配置文件。然后可以将新版Nginx进程杀死(使用QUIT、TERM、或者KILL)
[root@shuaishuai nginx-1.19.5]# kill -HUP 19658
12.升级完毕,可向旧的Nginx主进程(master)发送(QUIT、TERM、或者KILL)信号,使旧的主进程退出
[root@shuaishuai nginx-1.19.5]# kill -QUIT 19658
[root@shuaishuai nginx-1.19.5]# ps aux | grep nginx
root 21994 0.0 0.6 45968 3288 ? S 20:44 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21995 0.0 0.3 46428 1892 ? S 20:44 0:00 nginx: worker process
13.验证nginx版本号
[root@shuaishuai nginx-1.19.5]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.19.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=../openssl-1.1.1 --with-openssl-opt='enable-tls1_3 enable-ec_nistp_64_gcc_128' --with-pcre --without-http_gzip_module