linux 升级 nginx版本

一、yum安装nginx 升级

平滑升级

执行命令前,首先配置nginx 源

vim /etc/yum.repos.d/nginx.repo
#nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
#centos7
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
#执行升级
yum update nginx
#nginx -t
#nginx -s reload
检查版本号
nginx -v

二、二进制安装nginx的升级

2.1、升级前的准备
  • 新建一个目录存放安装包。可以放在/root/nginx
  • 升级前对原nginx的配置文件 ‘ nginx.conf ’ 做下备份。
  • 先查看下当前nginx安装位置和版本号。
2.2、开始检查

ps -ef | grep nginx
/usr/local/nginx-1.13.4/sbin/nginx -v

2.3、开始升级

1、下载nginx 的包

http://nginx.org/download/
cd /root/nginx

tar -zxvf nginx-1.16.1.tar.gz

cd nginx-1.16.1
./configure --prefix=/usr/local/nginx-1.13.4 --with-http_stub_status_module --with-http_ssl_module&&make   【nginx-1.13.4这个文件夹是我之前版本的文件夹名称,按照自己的实际情况写哦,直接粘贴可能会有问题】

2、开始替换新老版本的执行文件

cd /usr/local/nginx-1.13.4/sbin

mv nginx nginx.old

cp  -r /root/nginx/nginx-1.16.1/objs/nginx .

3、查看下nginx配置文件是否正确

/usr/local/nginx-1.13.4/sbin/nginx -t

4、开始新旧进程替换

1、向主进程(master)发送USR2信号,Nginx会启动一个新版本的master进程和对应工作进程,和旧版一起处理请求

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 

kill -USR2 19658

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 

2、向旧的Nginx主进程(master)发送WINCH信号,它会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理

kill -WINCH 19658

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)
#kill -HUP 19658

3、升级完毕,可向旧的Nginx主进程(master)发送(QUIT、TERM、或者KILL)信号,使旧的主进程退出

kill -QUIT 19658

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

4、验证nginx版本号

posted @ 2022-08-01 11:27  devops运维-小灰灰  阅读(2982)  评论(0)    收藏  举报