
点击:Linux技术宅,关注我!!!
备份
# 编译安装目录[root@RockyLinux9 ~]# ll usr/local/nginx/total 4drwxr-xr-x. 2 nginx nginx 4096 May 11 13:43 confdrwxr-xr-x. 2 nginx nginx 40 May 11 13:42 htmldrwxr-xr-x. 2 nginx nginx 6 May 11 13:42 logsdrwxr-xr-x. 2 nginx nginx 19 May 11 13:42 sbin# 备份tar czvf opt/nginx-backup-$(date +%Y%m%d-%H-%M-%S).tar.gz usr/local/nginx/
下载新版本
[root@RockyLinux9 ~]# curl -O https://nginx.org/download/nginx-1.26.0.tar.gz
解压和编译
# 查看已安装版本及依赖项[root@RockyLinux9 nginx-1.24.0]# nginx -Vnginx version: nginx/1.24.0built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)built with OpenSSL 3.0.7 1 Nov 2022TLS SNI support enabledconfigure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module# 解压[root@RockyLinux9 ~]# tar xf nginx-1.26.0.tar.gz[root@RockyLinux9 ~]# cd nginx-1.26.0# 编译[root@RockyLinux9 nginx-1.26.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module#只执行make,会在objs目录下生成新的二进制文件[root@RockyLinux9 nginx-1.26.0]# make[root@RockyLinux9 nginx-1.26.0]# objs/nginx -vnginx version: nginx/1.26.0
替换二进制文件
# 备份[root@RockyLinux9 nginx-1.26.0]# mv usr/local/nginx/sbin/nginx{,.old}# 拷贝文件[root@RockyLinux9 nginx-1.26.0]# cp objs/nginx usr/local/nginx/sbin/[root@RockyLinux9 nginx-1.26.0]# chown nginx:nginx usr/local/nginx/sbin/nginx[root@RockyLinux9 nginx-1.26.0]# ll usr/local/nginx/sbin/total 11168-rwxr-xr-x. 1 nginx nginx 5753480 May 11 17:10 nginx-rwxr-xr-x. 1 nginx nginx 5678264 May 11 13:42 nginx.old
发送USR2信号
#查看进程,仍为老版本的进程[root@RockyLinux9 nginx-1.26.0]# ps auxf|grep nginxroot 4704 0.0 0.0 9924 2052 ? Ss 17:09 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c usr/local/nginx/conf/nginx.confnginx 4705 0.0 0.1 14200 5252 ? S 17:09 0:00 \_ nginx: worker processnginx 4706 0.0 0.1 14200 5252 ? S 17:09 0:00 \_ nginx: worker process[root@RockyLinux9 nginx-1.26.0]# curl -I 127.0.0.1HTTP/1.1 200 OKServer: nginx/1.24.0# 发送USR2信号[root@RockyLinux9 nginx-1.26.0]# kill -USR2 `cat usr/local/nginx/logs/nginx.pid`# 生成了新的master进程和两个worker进程[root@RockyLinux9 nginx-1.26.0]# ps auxf|grep nginxroot 4704 0.0 0.0 9924 2436 ? Ss 17:09 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c usr/local/nginx/conf/nginx.confnginx 4705 0.0 0.1 14200 5252 ? S 17:09 0:00 \_ nginx: worker processnginx 4706 0.0 0.1 14200 5252 ? S 17:09 0:00 \_ nginx: worker processroot 4725 0.0 0.1 9932 6528 ? S 17:11 0:00 \_ nginx: master process usr/local/nginx/sbin/nginx -c usr/local/nginx/conf/nginx.confnginx 4726 0.0 0.1 14208 4996 ? S 17:11 0:00 \_ nginx: worker processnginx 4727 0.0 0.1 14208 4996 ? S 17:11 0:00 \_ nginx: worker process
报错日志如下:发送USR2信号后,未生成新的master进程
# cat /usr/local/nginx/logs/error.log[alert] 55522#0: execve() failed while executing new binary process "nginx" (2: No such file or directory)
原因:因启动时使用nginx(软链接指向/usr/sbin目录)命令启动,而非绝对路径(/usr/local/nginx/sbin/nginx)文件
处理:结束原有进程,以服务方式启动
[root@RockyLinux9 nginx-1.26.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf# or[root@RockyLinux9 nginx-1.26.0]# systemctl start nginx
发送WINCH信号
# 生成了新的PID文件[root@RockyLinux9 nginx-1.26.0]# ll usr/local/nginx/logs/total 20-rw-r--r--. 1 root root 4719 May 11 17:26 access.log-rw-r--r--. 1 root root 2823 May 11 17:26 error.log-rw-r--r--. 1 root root 5 May 11 17:11 nginx.pid-rw-r--r--. 1 root root 5 May 11 17:09 nginx.pid.oldbin# 发送WINCH信号[root@RockyLinux9 nginx-1.26.0]# kill -WINCH `cat usr/local/nginx/logs/nginx.pid.oldbin`# 旧的worker进程已经没有了# 若此时有占用的worker进程,会处于等待关闭状态,待当前任务结束后关闭[root@RockyLinux9 nginx-1.26.0]# ps auxf|grep nginxroot 4704 0.0 0.0 9924 2436 ? Ss 17:09 0:00 nginx: master process usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confroot 4725 0.0 0.1 9932 6528 ? S 17:11 0:00 \_ nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confnginx 4726 0.0 0.1 14208 4996 ? S 17:11 0:00 \_ nginx: worker processnginx 4727 0.0 0.1 14208 4996 ? S 17:11 0:00 \_ nginx: worker proces
验证新版本
[root@RockyLinux9 nginx-1.26.0]# nginx -vnginx version: nginx/1.26.0#查看新的连接已使用新版本来响应了[root@RockyLinux9 nginx-1.26.0]# curl -I 127.0.0.1HTTP/1.1 200 OKServer: nginx/1.26.0
发送QUIT信号
[root@RockyLinux9 nginx-1.26.0]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`[root@RockyLinux9 nginx-1.26.0]# ps auxf|grep nginxroot 4725 0.0 0.1 9932 6528 ? S 17:11 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confnginx 4726 0.0 0.1 14208 5380 ? S 17:11 0:00 \_ nginx: worker processnginx 4727 0.0 0.1 14208 5252 ? S 17:11 0:00 \_ nginx: worker process
回滚
#此时的进程情况[root@RockyLinux9 nginx-1.26.0]# ps auxf|grep nginxroot 4704 0.0 0.0 9924 2436 ? Ss 17:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confroot 4725 0.0 0.1 9932 6528 ? S 17:37 0:00 \_ nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confnginx 4726 0.0 0.1 14208 4996 ? S 17:37 0:00 \_ nginx: worker processnginx 4727 0.0 0.1 14208 4996 ? S 17:37 0:00 \_ nginx: worker process# 发现业务出现问,需要进行回滚操作# 恢复原二进制文件[root@RockyLinux9 nginx-1.26.0]# mv /usr/local/nginx/sbin/nginx{.old,}mv: overwrite '/usr/local/nginx/sbin/nginx'? y[root@RockyLinux9 nginx-1.26.0]# ll /usr/local/nginx/sbin/total 5548-rwxr-xr-x. 1 nginx nginx 5678264 May 11 13:42 nginx# 查看二进制程序版本[root@RockyLinux9 nginx-1.26.0]# nginx -vnginx version: nginx/1.24.0# 检查配置文件[root@RockyLinux9 nginx-1.26.0]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful# 发送HUP信号,启用旧master的worker进程[root@RockyLinux9 nginx-1.26.0]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid.oldbin`[root@RockyLinux9 nginx-1.26.0]# !psps auxf|grep nginxroot 4704 0.0 0.0 9924 2436 ? Ss 17:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confroot 4725 0.0 0.1 9932 6528 ? S 17:37 0:00 \_ nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confnginx 4726 0.0 0.1 14208 5380 ? S 17:37 0:00 | \_ nginx: worker processnginx 4727 0.0 0.1 14208 5124 ? S 17:37 0:00 | \_ nginx: worker processnginx 5007 0.0 0.1 14200 4996 ? S 18:01 0:00 \_ nginx: worker processnginx 5008 0.0 0.1 14200 4996 ? S 18:01 0:00 \_ nginx: worker process# 查看Web响应版本[root@RockyLinux9 nginx-1.26.0]# curl -I 127.0.0.1HTTP/1.1 200 OKServer: nginx/1.26.0# 发送QUIT信号,退出新的master进程[root@RockyLinux9 nginx-1.26.0]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`[root@RockyLinux9 nginx-1.26.0]# ps auxf|grep nginxroot 5022 0.0 0.0 6408 2176 pts/2 S+ 18:08 0:00 | \_ grep --color=auto nginxroot 4704 0.0 0.0 9924 2436 ? Ss 17:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confnginx 5007 0.0 0.1 14200 4996 ? S 18:01 0:00 \_ nginx: worker processnginx 5008 0.0 0.1 14200 4996 ? S 18:01 0:00 \_ nginx: worker process# 查看当前Web响应版本,已恢复到旧版本[root@RockyLinux9 nginx-1.26.0]# curl -I 127.0.0.1HTTP/1.1 200 OKServer: nginx/1.24.0

分享、在看与点赞
只要你点,我们就是胖友

文章转载自Linux技术宅,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




