原环境:
# /usr/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.1.1c 28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-openssl=/usr/local/ssl --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --add-module=/opt/nginx/ngx_devel_kit-0.3.1 --add-module=/opt/nginx/lua-nginx-module-0.10.15 --add-module=/opt/nginx/nginx_upstream_check_module-master
# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
安装包下载:
http://nginx.org/download/nginx-1.20.1.tar.gz
安装nginx1.20.1过程:
cp /opt/nginx/conf/nginx.conf{,.bak}
tar -zxf nginx-1.20.1.tar.gz
cd nginx-1.20.1
cp auto/lib/openssl/conf{,.bak}
vim auto/lib/openssl/conf
:39,42s@/.openssl@@g修改去掉.openssl/
# CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
# CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
# CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
# CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
# CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
改为:
CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
# 新下载的nginx在执行./configure的时候--prefix指定的目录是需要指向旧的nginx所指向的prefix目录
./configure --prefix=/opt/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-openssl=/usr/local/ssl --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --add-module=/opt/nginx/ngx_devel_kit-0.3.1 --add-module=/opt/nginx/lua-nginx-module-0.10.15 --add-module=/opt/nginx/nginx_upstream_check_module-master
make
# 执行完成之后不要执行make install指令,这点需要注意。
平滑升级
#先备份旧的nginx二进制可执行程序
cp /opt/nginx/sbin/nginx{,.bak}
#使用nginx-1.20.1的新二进制文件将nginx-1.16.1的二进制文件进行强制覆盖
[root@web nginx-1.20.1]# cp -f objs/nginx /opt/nginx/sbin/nginx
cp: overwrite ‘/opt/nginx/sbin/nginx’? y
【方法一】:用这个方法更简单,执行这个即可很快升级完成
[root@web nginx-1.20.1]# make upgrade
/opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /var/run/nginx.pid`
sleep 1
test -f /var/run/nginx.pid.oldbin
kill -QUIT `cat /var/run/nginx.pid.oldbin`
[root@web nginx-1.20.1]# /usr/sbin/nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.1.1c 28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-openssl=/usr/local/ssl --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --add-module=/opt/nginx/ngx_devel_kit-0.3.1 --add-module=/opt/nginx/lua-nginx-module-0.10.15 --add-module=/opt/nginx/nginx_upstream_check_module-master
【方法二】:步骤多一点
# 设定旧的服务不再接收用户请求(下线),新服务启动子进程接收用户请求(上线)
[root@web nginx-1.20.1]# ps aux | grep nginx
root 3672 0.0 0.0 97976 3116 ? Ss 2019 0:00 nginx: master process /usr/sbin/nginx -c /opt/nginx/conf/nginx.conf
root 9069 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 9070 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 9071 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 9072 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 9073 0.0 0.0 98380 2780 ? S 2019 0:00 nginx: worker process
root 9074 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 9075 0.0 0.0 98380 2532 ? S 2019 0:00 nginx: worker process
root 9076 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 23460 0.0 0.0 112712 980 pts/0 R+ 11:22 0:00 grep --color=auto nginx
#找到nginx父进程的pid号,现在对其发送USR2信号
[root@web nginx-1.20.1]# kill -USR2 3672
##设定新的子进程开始接收用户的访问请求,旧的不再接受用户的访问请求
[root@web nginx-1.20.1]# ps aux | grep nginx
root 3672 0.0 0.0 97976 3120 ? Ss 2019 0:00 nginx: master process /usr/sbin/nginx -c /opt/nginx/conf/nginx.conf
root 9069 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 9070 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 9071 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 9072 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 9073 0.0 0.0 98380 2780 ? S 2019 0:00 nginx: worker process
root 9074 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 9075 0.0 0.0 98380 2532 ? S 2019 0:00 nginx: worker process
root 9076 0.0 0.0 98380 2528 ? S 2019 0:00 nginx: worker process
root 23461 0.0 0.0 97732 3888 ? S 11:22 0:00 nginx: master process /usr/sbinnginx -c /opt/nginx/conf/nginx.conf
root 23462 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23463 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23464 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23465 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23466 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23467 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23468 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23469 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23473 0.0 0.0 112716 980 pts/0 S+ 11:22 0:00 grep --color=auto nginx
#现在是nginx的新老版本的进程共存的一种情况。虽然现在旧版本的nginx进程还存在,但是已经不再接受用户的请求了。除此之外,旧版本的nginx进程也依然处于监听的状态,我们通过lsof命令可以看到,虽然在监听,但实际不会处理新连接,因为fd已经从epoll中移出了。另外,旧master是新master的父进程,所以新master才能共享打开的监听端口。保留旧版本的master是为了方便回滚
[root@voicenginx01ability nginx-1.20.1]# lsof -p 3672 | grep LISTEN
nginx 3672 root 7u IPv4 144852840 0t0 TCP *:http (LISTEN)
## 进行旧服务进程的关闭,该pid号是旧版本的nginx的master进程的pid号
[root@web nginx-1.20.1]# kill -WINCH 3672
[root@web nginx-1.20.1]# ps aux | grep nginx
root 3672 0.0 0.0 97976 3120 ? Ss 2019 0:00 nginx: master process /usr/sbinnginx -c /opt/nginx/conf/nginx.conf
root 23461 0.0 0.0 97732 3888 ? S 11:22 0:00 nginx: master process /usr/sbinnginx -c /opt/nginx/conf/nginx.conf
root 23462 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23463 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23464 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23465 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23466 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23467 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23468 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23469 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23480 0.0 0.0 112716 976 pts/0 R+ 11:23 0:00 grep --color=auto nginx
#可以看到现在的旧版本的nginx的worker进程已经全部被杀死了,只剩下的旧版本nginx的master进程
确定升级没有任何问题的话,那么现在我们可以把这个master进程给杀死掉。可以用kill -QUIT把旧master进程杀掉
kill -QUIT 3672
[root@web nginx-1.20.1]# ps aux | grep nginx
root 23461 0.0 0.0 97732 3888 ? S 11:22 0:00 nginx: master process /usr/sbinnginx -c /opt/nginx/conf/nginx.conf
root 23462 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23463 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23464 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23465 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23466 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23467 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23468 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23469 0.0 0.0 98136 1984 ? S 11:22 0:00 nginx: worker process
root 23505 0.0 0.0 112712 976 pts/0 R+ 11:27 0:00 grep --color=auto nginx