nginx adds new modules using hot deployment

Posted by biopv on Tue, 30 Jun 2020 03:42:35 +0200

brief introduction

When nginx is first compiled and installed, http_ ssl_ The module module is not compiled into the nginx binary file by default. If you need to add SSL certificate. That is to say, use the https protocol. Then you need to add http_ssl_module module. Suppose your nginx installation package directory is in / home/johnson/nginx-1.17.5, which will be used below

Little knowledge: use the / home/johnson/nginx-1.17.5/configure --help command to see a lot of -- with and -- without module options.

  • - with: by default, it is not compiled into the nginx binary file
  • - without: it is compiled into the nginx binary file by default
/home/johnson/nginx-1.17.5/configure --help
...
  --with-http_ssl_module             enable ngx_http_ssl_module
...
  --without-http_gzip_module         disable ngx_http_gzip_module
...

You can see http_ ssl_ The module module is not compiled into the nginx binary file by default.

Compile add new module

When you need to add http_ssl_module, the command is as follows:

/home/johnson/nginx-1.17.5/configure --with-http_ssl_module

After executing the command, you can run the command in / home / Johnson / nginx-1.17.5/objs/ngx_ The modules. C file shows which modules to install into nginx. As follows:

ngx_module_t *ngx_modules[] = {
    &ngx_core_module,
...
    &ngx_http_ssl_module,
...

You can see http_ ssl_ The module module should be installed in nginx, and then use the make command to set the http_ ssl_ The module is compiled into the nginx binary file

cd /home/johnson/nginx-1.17.5
make

After executing the above command, / home/johnson/nginx-1.17.5/objs/nginx is the compiled nginx binary file. Then we need to perform hot deployment and upgrade.

Hot deployment

Suppose your nginx installation directory is in / usr/local/nginx.

  1. Back up the nginx binaries in use
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
  1. Replace the nginx binaries in use with the latest nginx binaries
cp -r /home/johnson/nginx-1.17.5/objs/nginx /usr/local/nginx/sbin/ -f
  1. View master processes running nginx
ps -ef | grep nginx
root      6503     1  0 Jun23 ?        00:00:00 nginx: master process nginx
ubuntu   26317 19063  0 07:39 pts/0    00:00:00 grep --color=auto nginx
nobody   31869  6503  0 Jun27 ?        00:00:00 nginx: worker process

As you can see, the current master process number of nginx is 6503.

  1. Inform the running master process of nginx that it needs to upgrade nginx
kill -USR2 6503
ps -ef | grep nginx
root      6503     1  0 Jun23 ?        00:00:00 nginx: master process nginx
root      7128  6503  0 08:05 ?        00:00:00 nginx: master process nginx
nobody    7129  7128  0 08:05 ?        00:00:00 nginx: worker process
root      7140 30619  0 08:05 pts/0    00:00:00 grep --color=auto nginx
nobody   31869  6503  0 Jun27 ?        00:00:00 nginx: worker process

You can see that after the command is executed, the new master process of nginx will be started, and the new master process is started by the old master process. If it is not started, you can use nginx -t to check whether the configuration file is correct. If there is no problem, you can generally start a new master process.

  1. Inform the old nginx master process, please gracefully close all old worker processes
kill -WINCH 6503
root@VM-0-13-ubuntu:/usr/local/nginx# ps -ef | grep nginx
root      6503     1  0 Jun23 ?        00:00:00 nginx: master process nginx
root      7128  6503  0 08:05 ?        00:00:00 nginx: master process nginx
nobody    7129  7128  0 08:05 ?        00:00:00 nginx: worker process
root      9431 30619  0 08:17 pts/0    00:00:00 grep --color=auto nginx

As you can see, the old worker processes have been shut down. If an error occurs, you can use the nginx -s reload command to go back to the old version.
If everything is OK and there is no problem, you can close the old master process. kill -9 6503. After the parent process of the new master process (the old master process) is closed, the parent process will be changed to the system process, and the process number of the system process is 1.
At this point, it is perfect to add new modules and realize hot deployment!!!

summary

Because the first time you compile nginx, you may not expect to use other modules, or you may delete some modules. Hot deployment to nginx is often needed at this time. The hot deployment commands in this article refer to Geek time Nginx core knowledge lecture 100: Lecture 10.
Reference article: geek time Nginx core knowledge lecture 100: Lecture 10

Personal blog website: https://colablog.cn/

If my article helps you, you can pay attention to my WeChat official account. I will share the article with you at the first time.

Topics: Nginx Ubuntu SSL