Smooth upgrade and rollback of Nginx version in 1 minute

Posted by zingbats on Tue, 10 Sep 2019 09:15:54 +0200

Nginx is a knowledge point for operation, maintenance, development and testing. Many articles about Nginx have been written before.

  • Nginx Service Introduction and Installation
  • Introduction to Nginx Service Profile
  • Nginx Configuration Virtual Host
  • Nginx reverse proxy load balancing configuration
  • Nginx+Tomcat Multi-instance and Load Balancing Configuration
  • Introduction to HTTP Server Nginx Service Continuation
  • Detailed Explanation of Nginx Optimal Configuration
  • Nginx+keepalived High Availability Configuration Actual Warfare
  • Apache VS Nginx, did you pick it right?
  • Nginx-based HTTP Performance Optimization Practice
  • What on earth can Nginx do? You will understand after reading this article!
  • In-depth summary
  • Ultimate guide: 12 tips for improving the hardness of Nginx servers

Interested people can pay attention to it.[ The Technological Way of Migrant Workers'Brothers ] Wechat Public Number Look at the above article.

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

1. Introduction to the Environment

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 new and old 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 a new version

Version upgrade is actually an upgrade of 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, backup the 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, smooth upgrade

[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 opened normally, but the old work process also exists, so we use the following command to send the old work process a smooth stop signal, 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 work process has stopped, and then we test whether it can be accessed properly:

It can be accessed normally, in fact, this smooth upgrade action is completely imperceptible to the 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

View version is also the latest version, the upgrade is completed.

Note: If there is no problem after the version upgrade and the old master process needs to be shut down, you can use the following command:

kill -QUIT old_master_PID

5. Version rollback

For upgrade, the most difficult is not upgrade, but rollback, because the actual production environment rollback probability exists, such as: the new version due to some unknown bug s lead to incompatibility with existing applications, or run instability, and so on.

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

In the above results, we can also see that the old master process has always existed. It will not shut down itself before it is closed manually. This design is beneficial. The advantage is that after upgrading the new version, if there is a problem, it can quickly roll back to the last stable version.

[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 find that the previous version of the rollback has been smoothly rolled back. Next, we test whether it can be accessed properly.

It can also be accessed normally, so this rollback operation is also imperceptible to users.

Hella, today's production tips are shared here. If you think this article is helpful or useful to you, please forward and share this article, your forwarding is my greatest support!!

More wonderful articles, please pay attention to Migrant Worker's personal Wechat Public Number: Migrant Worker's Technological Road

Topics: Web Server Nginx Tomcat Apache lsof