Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize smooth upgrade and rollback of Nginx version

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how Nginx versions can be smoothly upgraded and rolled back. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

1. Introduction to environment

The two versions of nginx prepared today are as follows:

[root@nginx ~]# cd /download/nginx/[root@nginx nginx]# lltotal 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

Build 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 0drwxr-xr-x 2 root root 333 Mar 1 09:01 confdrwxr-xr-x 2 root root 40 Mar 1 09:01 htmldrwxr-xr-x 2 root root 6 Mar 1 09:01 logsdrwxr-xr-x 2 root root 19 Mar 1 09:01 sbin

Build 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 0drwxr-xr-x 2 root root 333 Mar 1 09:03 confdrwxr-xr-x 2 root root 40 Mar 1 09:03 htmldrwxr-xr-x 2 root root 6 Mar 1 09:03 logsdrwxr-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 -tnginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is oknginx: 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 nginxroot 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginxnobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker processroot 6327 1244 0 09:06 pts/0 00:00:00 grep --color=auto nginx[root@nginx ~]# lsof -i :80COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnginx 6324 root 6u IPv4 26324 0t0 TCP *:http (LISTEN)nginx 6325 nobody 6u IPv4 26324 0t0 TCP *:http (LISTEN)

Upgrade to a new 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 -vnginx version: nginx/1.12.2[root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/[root@nginx sbin]# mv nginx nginx-1.12.2#backup the old 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 operation

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

At this point the new master process has started normally, but the old work process also exists, so we use the following command to signal the old work process to stop smoothly, as follows:

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

At this point, the old work process has stopped, and next we test whether it can be accessed normally:

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

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

The updated version is also the latest version, and the upgrade is complete.

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

kill -QUIT old_master_PID

5. Version rollback

For upgrades, the hardest part is not upgrading, but rolling back, because there is a chance of rolling back in the actual production environment, such as: the new version is incompatible with the existing application due to some unknown bug, or the operation is unstable, etc.

Therefore, for operations engineers, fault rollback is the focus.

In the above results, we can also see that the old master process is always present, and it will not close itself before it is manually closed. This design has advantages, and the advantage is that after upgrading to a new version, if there is a problem, it can be quickly rolled back to the previous stable version.

[root@nginx ~]# ps -ef|grep nginxroot 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginxroot 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginxnobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker processroot 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 nginxroot 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginxroot 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginxnobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker processroot 6355 1244 0 09:24 pts/0 00:00:00 grep --color=auto nginx[root@nginx sbin]# ./ nginx -vnginx version: nginx/1.12.2

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

The same can be accessed normally, so this rollback operation is also imperceptible to the user.

Thank you for reading! About "Nginx version how to achieve smooth upgrade and rollback" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report