技术交流QQ群:①185473046   ②190706903   ③203744115   网站地图
登录

下次自动登录
现在的位置: 首页Nginx>正文
Linux下Nginx版本平滑升级与回滚
2022年08月03日 Nginx 暂无评论 ⁄ 被围观 118次+

操作系统:CentOS 7.x

准备篇

一、防火墙配置

CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙。

1、关闭firewall:

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

systemctl mask firewalld

systemctl stop firewalld

yum remove firewalld

2、安装iptables防火墙

yum install iptables-services #安装

vi /etc/sysconfig/iptables #编辑防火墙配置文件

# sample configuration for iptables service

# you can edit this manually or use system-config-firewall

# please do not ask us to add additional ports/services to this default configuration

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

:wq! #保存退出

systemctl restart iptables.service #最后重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

/usr/libexec/iptables/iptables.init restart #重启防火墙

二、关闭SELINUX

vi /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

三 、系统约定

软件源代码包存放位置:/usr/local/src

源码包编译安装位置:/usr/local/软件名字

四、下载软件包

1、下载nginx

http://nginx.org/download/nginx-1.18.0.tar.gz

http://nginx.org/download/nginx-1.22.0.tar.gz

2、下载pcre(支持nginx伪静态)

https://ftp.exim.org/pub/pcre/pcre-8.45.tar.gz

3、下载zlib(nginx扩展)

https://zlib.net/zlib-1.2.12.tar.gz

4、下载openssl(适用于nginx扩展https)

https://www.openssl.org/source/openssl-1.1.1q.tar.gz

5、下载ngx_cache_purge(nginx缓存模块)

http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

以上软件包上传到/usr/local/src目录

五、安装编译工具包

yum install make gcc gcc-c++ perl zlib-devel

安装篇

一、安装Nginx

1、安装pcre

cd /usr/local/src

mkdir /usr/local/pcre

tar zxvf pcre-8.45.tar.gz

cd pcre-8.45

./configure --prefix=/usr/local/pcre

make

make install

2、安装zlib

cd /usr/local/src

mkdir /usr/local/zlib

tar zxvf zlib-1.2.12.tar.gz

cd zlib-1.2.12

./configure --prefix=/usr/local/zlib

make

make install

3、安装openssl

cd /usr/local/src

mkdir /usr/local/openssl

tar zxvf openssl-1.1.1q.tar.gz

cd openssl-1.1.1q

./config -fPIC shared zlib --prefix=/usr/local/openssl

make

make install

4、安装Nginx

groupadd www

useradd -g www www -s /bin/false

cd /usr/local/src

tar zxvf ngx_cache_purge-2.3.tar.gz

tar zxvf nginx-1.18.0.tar.gz

cd nginx-1.18.0

./configure --prefix=/usr/local/nginx --user=www --group=www --without-http_memcached_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_realip_module --with-openssl=/usr/local/src/openssl-1.1.1q --with-zlib=/usr/local/src/zlib-1.2.12 --with-pcre=/usr/local/src/pcre-8.45 --add-module=../ngx_cache_purge-2.3

注意:--with-openssl=/usr/local/src/openssl-1.1.1q --with-zlib=/usr/local/src/zlib-1.2.12 --with-pcre=/usr/local/src/pcre-8.45指向的是源码包解压的路径,而不是安装的路径,否则会报错。

make

make install

/usr/local/nginx/sbin/nginx #启动Nginx

#查看nginx版本和安装模块信息

/usr/local/nginx/sbin/nginx -V

二、平滑升级nginx版本

1、备份旧版nginx

cp -r /usr/local/nginx /usr/local/nginx.bak

2、查看旧版编译信息

/usr/local/nginx/sbin/nginx -V

3、使用新版本安装包进行配置configure和编译make(切记不执行安装操作 make install)

cd /usr/local/src

tar zxvf nginx-1.22.0.tar.gz

cd nginx-1.22.0

./configure --prefix=/usr/local/nginx --user=www --group=www --without-http_memcached_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_realip_module --with-openssl=/usr/local/src/openssl-1.1.1q --with-zlib=/usr/local/src/zlib-1.2.12 --with-pcre=/usr/local/src/pcre-8.45 --add-module=../ngx_cache_purge-2.3

make

4、备份二进制文件,用新版替换旧版

#备份旧文件

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

#使用新文件替换旧文件

cp /usr/local/src/nginx-1.22.0/objs/nginx /usr/local/nginx/sbin/nginx

5、检查nginx配置文件是否正常运行

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

[root@localhost sbin]# /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

6、查找nginx主进程,向master进程发送USER2信号,使新旧进程同时存在

ps -ef|grep nginx

[root@localhost sbin]# ps -ef|grep nginx

root 80739 1 0 05:15 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80740 80739 0 05:15 ? 00:00:00 nginx: worker process

root 80758 7675 0 05:17 pts/0 00:00:00 grep --color=auto nginx

#查看nginx进程状态

systemctl status 80739

#执行下面的操作,向master进程发送USER2信号

kill -USR2 80739

说明:

#旧版nginx的master进程将/usr/local/nginx/logs/nginx.pid重命名为/usr/local/nginx/logs/nginx.pid.oldbin

#新版nginx启动新的master进程,重新生成新的nginx.pid文件,此时新的nginx进程和旧的nginx进程同时存在,之前旧的请求继续在旧版本运行,新的请求在新版本运行

[root@localhost sbin]# ps -ef|grep nginx

root 80739 1 0 05:15 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80740 80739 0 05:15 ? 00:00:00 nginx: worker process

root 80761 80739 0 05:18 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80762 80761 0 05:18 ? 00:00:00 nginx: worker process

root 80764 7675 0 05:18 pts/0 00:00:00 grep --color=auto nginx

7、平滑升级和回退操作

7.1平滑升级

#验证新版本是否正常运行,如果运行正常就向旧的worker进程发送平滑停止的信号,停止旧的worker进程

kill -WINCH 80739

[root@localhost sbin]# ps -ef|grep nginx

root 80739 1 0 05:15 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

root 80761 80739 0 05:18 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80762 80761 0 05:18 ? 00:00:00 nginx: worker process

root 80766 7675 0 05:20 pts/0 00:00:00 grep --color=auto nginx

#退出旧版master进程,旧版文件/usr/local/nginx/logs/nginx.pid.oldbin自动删除

kill -QUIT 80739

[root@localhost sbin]# ps -ef|grep nginx

root 80761 1 0 05:18 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80762 80761 0 05:18 ? 00:00:00 nginx: worker process

root 80768 7675 0 05:22 pts/0 00:00:00 grep --color=auto nginx

#查看版本,nginx已经平滑升级到新版本

/usr/local/nginx/sbin/nginx -V

7.2回退到旧版本

#如果新版本有问题,同样可以平滑回退到旧版本

ps -ef|grep nginx

[root@localhost sbin]# ps -ef|grep nginx

root 80841 1 0 05:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80842 80841 0 05:37 ? 00:00:00 nginx: worker process

root 80850 80841 0 05:38 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80851 80850 0 05:38 ? 00:00:00 nginx: worker process

root 80856 7675 0 05:39 pts/0 00:00:00 grep --color=auto nginx

#查看旧版本的/usr/local/nginx/logs/nginx.pid.oldbin

cat /usr/local/nginx/logs/nginx.pid.oldbin

[root@localhost sbin]# cat /usr/local/nginx/logs/nginx.pid.oldbin

80841

#启动旧版worker进程

kill -s HUP 80841

[root@localhost sbin]# ps -ef|grep nginx

root 80841 1 0 05:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80842 80841 0 05:37 ? 00:00:00 nginx: worker process

root 80850 80841 0 05:38 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80851 80850 0 05:38 ? 00:00:00 nginx: worker process

www 80858 80841 0 05:40 ? 00:00:00 nginx: worker process

root 80860 7675 0 05:40 pts/0 00:00:00 grep --color=auto nginx

#查看新版本的/usr/local/nginx/logs/nginx.pid

cat /usr/local/nginx/logs/nginx.pid

[root@localhost logs]# cat /usr/local/nginx/logs/nginx.pid

80850

#关闭新版本worker进程

kill -WINCH 80850

[root@localhost sbin]# ps -ef|grep nginx

root 80841 1 0 05:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80842 80841 0 05:37 ? 00:00:00 nginx: worker process

root 80850 80841 0 05:38 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80858 80841 0 05:40 ? 00:00:00 nginx: worker process

root 80863 7675 0 05:41 pts/0 00:00:00 grep --color=auto nginx

#关闭新版master进程,旧版文件/usr/local/nginx/logs/nginx.pid.oldbin自动覆盖新版文件nginx.pid

kill -QUIT 80850

[root@localhost sbin]# ps -ef|grep nginx

root 80841 1 0 05:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

www 80842 80841 0 05:37 ? 00:00:00 nginx: worker process

www 80858 80841 0 05:40 ? 00:00:00 nginx: worker process

root 80866 7675 0 05:41 pts/0 00:00:00 grep --color=auto nginx

#删除新版本二进制文件

rm -rf /usr/local/nginx/sbin/nginx

#恢复旧版本二进制文件

mv /usr/local/nginx/sbin/nginx.old /usr/local/nginx/sbin/nginx

#查看版本,nginx已经平滑回退到旧版本

/usr/local/nginx/sbin/nginx -V

至此,Linux下Nginx 版本平滑升级与回滚教程完成。

     

  系统运维技术交流QQ群:①185473046 系统运维技术交流□Ⅰ ②190706903 系统运维技术交流™Ⅱ ③203744115 系统运维技术交流™Ⅲ

给我留言

您必须 [ 登录 ] 才能发表留言!



Copyright© 2011-2022 系统运维 All rights reserved
版权声明:本站所有文章均为作者原创内容,如需转载,请注明出处及原文链接
陕ICP备11001040号-3