How to smoothly upgrade and rollback Nginx version in 1 minute

How to smoothly upgrade and rollback Nginx version in 1 minute

Today, let's talk about a situation that is often encountered in the actual production environment of an enterprise, upgrading Nginx to a new version and how to roll back to the old version.

1. Environment Introduction

The two nginx versions prepared today are as follows:

[root@nginx ~]# cd /download/nginx/
[root@nginx nginx]# ll
total 1952
-rw-r--r-- 1 root root 981687 Oct 17 2017 nginx-1.12.2.tar.gz
-rw-r--r-- 1 root root 1015384 Dec 4 09:58 nginx-1.14.2.tar.gz

2. Compile and install the old and new versions

Compile and install nginx-1.12.2

[root@nginx nginx]# tar zxf nginx-1.12.2.tar.gz 
[root@nginx nginx]# cd nginx-1.12.2
[root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx-1.12.2
[root@nginx nginx-1.12.2]# echo $?
0
[root@nginx nginx-1.12.2]# make && make install
[root@nginx nginx-1.12.2]# echo $?
0
[root@nginx nginx-1.12.2]# ll /usr/local/nginx-1.12.2/
total 0
drwxr-xr-x 2 root root 333 Mar 1 09:01 conf
drwxr-xr-x 2 root root 40 Mar 1 09:01 html
drwxr-xr-x 2 root root 6 Mar 1 09:01 logs
drwxr-xr-x 2 root root 19 Mar 1 09:01 sbin

Compile and install nginx-1.14.2

[root@nginx ~]# cd /download/nginx/
[root@nginx nginx]# tar zxf nginx-1.14.2.tar.gz 
[root@nginx nginx]# cd nginx-1.14.2
[root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx-1.14.2
[root@nginx nginx-1.14.2]# echo $?
0
[root@nginx nginx-1.14.2]# make && make install
[root@nginx nginx-1.14.2]# echo $?
0
[root@nginx nginx-1.14.2]# ls -l /usr/local/nginx-1.14.2/
total 0
drwxr-xr-x 2 root root 333 Mar 1 09:03 conf
drwxr-xr-x 2 root root 40 Mar 1 09:03 html
drwxr-xr-x 2 root root 6 Mar 1 09:03 logs
drwxr-xr-x 2 root root 19 Mar 1 09:03 sbin

At this point, two versions of nginx software have been deployed.

3. Start the old version of nginx

[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
root 6327 1244 0 09:06 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 6324 root 6u IPv4 26324 0t0 TCP *:http (LISTEN)
nginx 6325 nobody 6u IPv4 26324 0t0 TCP *:http (LISTEN)

4. Upgrade to the latest version

Version upgrade is actually an upgrade for binary files. The process is as follows:

[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v
nginx version: nginx/1.12.2
[root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/
[root@nginx sbin]# mv nginx nginx-1.12.2
#First, back up the original old version of nginx binary file [root@nginx sbin]# cp /usr/local/nginx-1.14.2/sbin/nginx ./
#Copy the new version of the binary file to the current directory

Next, perform a smooth upgrade operation

[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
root 6338 1244 0 09:11 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# kill -USR2 6324
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6343 1244 0 09:12 pts/0 00:00:00 grep --color=auto nginx

At this time, the new master process has been started normally, but the old worker process still exists, so we use the following command to signal the old worker process to stop smoothly, as follows:

[root@nginx ~]# kill -WINCH 6324
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6346 1244 0 09:14 pts/0 00:00:00 grep --color=auto nginx

At this point, the old worker process has stopped. Next, we test whether it can be accessed normally:

It can be accessed normally. In fact, this smooth upgrade is completely imperceptible to the accessing users, so the nginx hot deployment has been completed.

[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v
nginx version: nginx/1.14.2

Check the version and it is the latest version, the upgrade is complete.

Note: If there are no problems after the version upgrade is completed and you need to shut down the old master process, you can use the following command:

kill -QUIT old_master_PID

5. Version rollback

The most difficult part of upgrading is not upgrading, but rolling back, because there is a possibility of rollback in the actual production environment. For example, the new version is incompatible with the existing application due to some unknown bugs, or the operation becomes unstable, etc.

Therefore, for operation and maintenance engineers, fault rollback is the key point.

In the above results, we can also see that the old master process always exists. It will not shut down by itself unless it is manually shut down. This design is beneficial. The benefit is that after upgrading the new version, if there is a problem, it can be rolled back to the previous stable version in a timely and quick manner.

[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6350 1244 0 09:23 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/
[root@nginx sbin]# mv nginx nginx-1.14.2
[root@nginx sbin]# mv nginx-1.12.2 nginx
[root@nginx sbin]# kill -USR1 6324
[root@nginx sbin]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6355 1244 0 09:24 pts/0 00:00:00 grep --color=auto nginx
[root@nginx sbin]# ./nginx -v
nginx version: nginx/1.12.2

From the above results, we can see that the previous version has been rolled back smoothly. Next, we will test whether it can be accessed normally:

It can still be accessed normally, so the rollback operation is also imperceptible to the user.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of how to upgrade nginx to support HTTP2.0
  • Detailed explanation of Nginx version smooth upgrade solution
  • How to upgrade nginx to support http2
  • Some suggestions for improving Nginx performance
  • Nginx service installation and software upgrade

<<:  Detailed explanation of the observer mode starting from the component value transfer of Vue

>>:  Example of using CASE WHEN in MySQL sorting

Recommend

Detailed explanation of how Zabbix monitors the master-slave status of MySQL

After setting up the MySQL master-slave, you ofte...

The whole process of node.js using express to automatically build the project

1. Install the express library and generator Open...

Using Vue3 (Part 1) Creating a Vue CLI Project

Table of contents 1. Official Documentation 2. Cr...

Convert psd cut image to div+css format

PSD to div css web page cutting example Step 1: F...

Case analysis of several MySQL update operations

Table of contents Case Study Update account balan...

Horizontal header menu implemented with CSS3

Result:Implementation Code html <nav class=&qu...

Use of Linux network configuration tools

This article introduces RHEL8 network services an...

Summary of MySQL logical backup and recovery testing

Table of contents 1. What kind of backup is a dat...

Discuss the value of Web standards from four aspects with a mind map

I have roughly listed some values ​​to stimulate ...

How to split and merge multiple values ​​in a single field in MySQL

Multiple values ​​combined display Now we have th...

Using MySQL database with Python 3.4 under Windows 7

The detailed process of using MySQL database with...