installation, upgrade and dynamic adding module of Linux Nginx

Source: Internet
Author: User
Tags sha1

System based onubuntu server 14.04.4 amd64

Install the first step download and unzip the Nginx compressed package

Download Nginx from Nginx website, or perform wget http://nginx.org/download/nginx-1.10.1.tar.gz direct download on Linux
Unzip the nginx-1.10.1.tar.gz file:

Tar zxvf nginx-1.10.1.tar.gz
Second Step configuration
CD Nginx-1.10.1./configure--prefix=/usr/local/nginx

Attention:

① If you have not previously installed the C compiler (C compiler), this step will report the following error message:

[Email protected]:~/download/nginx-1.10.1$./configure–prefix=/usr/local/nginx
Checking for OS
+ Linux 4.2.0-27-generic x86_64
Checking for C compiler ... not found

./configure:error:c compiler cc is not found

[Email protected]:~/download/nginx-1.10.1$

You can refer to this article to install C compiler, and then continue with the following operation

② If you have not previously installed Pcre, this step will report the following error message:

Checking for PCRE library ... not found
Checking for PCRE library in/usr/local/... not found
Checking for PCRE library in/usr/include/pcre/... not found
Checking for PCRE library in/usr/pkg/... not found
Checking for PCRE library in/opt/local/... not found

./configure:error:the HTTP Rewrite module requires the PCRE library.
You can either disable the module by Using–without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
Statically from the source with Nginx by using–with-pcre= option.

[Email protected]:~/download/nginx-1.10.1$

You can refer to this article to install Pcre, and then continue with the following actions

③ If you have not previously installed zlib, this step will report the following error message:

Checking for MD5 in System MD Library ... not found
Checking for MD5 in System MD5 library ... not found
Checking for MD5 in System OpenSSL Crypto library ... not found
Checking for SHA1 in System MD Library ... not found
Checking for SHA1 in System OpenSSL Crypto library ... not found
Checking for zlib library ... not found

./configure:error:the HTTP gzip module requires the Zlib library.
You can either disable the module by Using–without-http_gzip_module
option, or install the Zlib library into the system, or build the Zlib library
Statically from the source with Nginx by using–with-zlib= option.

Xueli[email protected]:~/download/nginx-1.10.1$

You can refer to this article to install zlib, and then continue with the following actions

You can also skip this step, perform the default installation, --prefix the default value is /usr/local/nginx , Nginx official website for this description: Building nginx from Sources

Third Step compilation
Make
Fourth step to complete the installation
sudo make install
Smooth upgrade

When it is necessary to upgrade the running Nginx, you can replace the old executable program with the new version or the recompiled executable program without stopping Nginx, here we nginx-1.10.1 upgrade to nginx-1.11.1 .

First step back up the old version

Because Nginx upgrade, the real only with the new version of the executable, replace the old version of the executables, so for the backup, you can either back up only the old version of the executable file, you can also package backup the entire old version of the installation directory, the reference command is as follows:

Back up old versions of executable files only

sudo cp/usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginx.bak

Package backup the entire old version of the installation directory

sudo tar-cvf/usr/local/nginx.bak/usr/local/nginx
Second step to download the new version and unzip the Nginx compression package

For the new version of Nginx compressed package download and decompression, you can refer to this article on the installation of Nginx first to second step.

The third step uses the old version configuration parameter, configures and compiles the new version Nginx

Because only Nginx upgrade, does not involve the configuration parameter modification, so we generally use the same configuration as the old version (you can also use the new configuration information), to compile a new version of Nginx, using the following command to view the old version configuration information:

/usr/local/nginx/sbin/nginx-v

The results can be obtained as follows:

[Email protected]:~/download/nginx-1.11.1$/usr/local/nginx/sbin/nginx-v
Nginx version:nginx/1.10.1
Built by GCC 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
Configure Arguments:–prefix=/usr/local/nginx
[Email protected]:~/download/nginx-1.11.1$

Where [Configure Arguments:–prefix=/usr/local/nginx] This line is the old version of Nginx configuration information, it can be seen that the old version just specify the installation path, using [Configure arguments:] After the parameter information, the new version of Nginx is configured identically and then compiled:

./configure--prefix=/usr/local/nginx
Fourth step to compile the new version Nginx executable program
Make
Step fifth overwrite the old version executable program with the new version of Nginx executables

On the basis of the previous step, execute the command:

sudo cp Objs/nginx/usr/local/nginx/sbin/nginx

To execute this command, the following exception may be reported, prompting the file to be occupied:

[Email protected]:~/download/nginx-1.11.1$ sudo cp Objs/nginx/usr/local/nginx/sbin/nginx
Cp:cannot Create regular file '/usr/local/nginx/sbin/nginx ': Text file busy
[Email protected]:~/download/nginx-1.11.1$

You can use the following command to enforce overrides:

sudo cp-rfp Objs/nginx/usr/local/nginx/sbin/nginx
The sixth step starts the new version of Nginx master process

Send USR2 signal to old version main process number:

KILL-USR2 old Nginx Master process number

The old Nginx master process receives the -USR2 signal, renames its .pid file .oldpid , and then executes the new version of the Nginx executable, starting the new version of the main process and the worker process in turn:

PID PPID USER %cpu VSZ Wchan COMMAND
33126 1 Root 0.0 1164 Pause Nginx:master Process/usr/local/nginx/sbin/nginx
33134 33126 Nobody 0.0 1368 Kqread Nginx:worker process (Nginx)
33135 33126 Nobody 0.0 1380 Kqread Nginx:worker process (Nginx)
33136 33126 Nobody 0.0 1368 Kqread Nginx:worker process (Nginx)
36264 33126 Root 0.0 1148 Pause Nginx:master Process/usr/local/nginx/sbin/nginx
36265 36264 Nobody 0.0 1364 Kqread Nginx:worker process (Nginx)
36266 36264 Nobody 0.0 1364 Kqread Nginx:worker process (Nginx)
36267 36264 Nobody 0.0 1364 Kqread Nginx:worker process (Nginx)
Seventh step gracefully close the old version of the worker process

At this point, the new, old version of the Nginx instance will run concurrently to process the request, if the old version of the main process to send a signal, the old version of the WINCH main process will send a message to its worker process, request that they gracefully close, after that, the old version of the worker process began to gradually exit:

PID PPID USER %cpu VSZ Wchan COMMAND
33126 1 Root 0.0 1164 Pause Nginx:master Process/usr/local/nginx/sbin/nginx
33135 33126 Nobody 0.0 1380 Kqread Nginx:worker process is shutting down (nginx)
36264 33126 Root 0.0 1148 Pause Nginx:master Process/usr/local/nginx/sbin/nginx
36265 36264 Nobody 0.0 1364 Kqread Nginx:worker process (Nginx)
36266 36264 Nobody 0.0 1364 Kqread Nginx:worker process (Nginx)
36267 36264 Nobody 0.0 1364 Kqread Nginx:worker process (Nginx)

Gracefully close the old version of the worker process command:

Kill-winch old Nginx Master process number
Eighth step gracefully close the old version of the main process, complete Nginx upgrade

After a period of time, the old worker process finishes processing all connected requests and exits, and the new request is processed only by the new version of the worker process:

PID PPID USER %cpu VSZ Wchan COMMAND
33126 1 Root 0.0 1164 Pause Nginx:master Process/usr/local/nginx/sbin/nginx
36264 33126 Root 0.0 1148 Pause Nginx:master Process/usr/local/nginx/sbin/nginx
36265 36264 Nobody 0.0 176° Kqread Nginx:worker process (Nginx)
36266 36264 Nobody 0.0 176° Kqread Nginx:worker process (Nginx)
36267 36264 Nobody 0.0 176° Kqread Nginx:worker process (Nginx)

It should be noted that, at this point, the old version of the main process has not yet closed its listening socket, if necessary, you can still restore the old version of the worker process. If for some reason the new version of the executable is not working well, here are a few scenarios to consider:

    • Sends a signal to an older version of the main process HUP . The old version master process will reopen the worker process without re-reading the configuration information. Then, by sending a signal to the new version master process QUIT , all new versions of the process will be closed gracefully.
    • Sends a signal to the new version master process TERM . He will then send a message to its worker process, asking them to exit immediately, and immediately thereafter, the worker processes will exit. (If for some reason the new version process does not exit, the new version master process should be KILL signaled to force the new version master process to exit.) When the new version master process exits, the old version master process will automatically start its worker process.
      After the new version master process exits, the old version master process will remove the file whose name is the .oldpid end and revert to its .pid file.
      If the upgrade succeeds, you should send a signal to the old version master process to QUIT exit, leaving only the new version of the process:
/tr>
PID PPID USER %cpu VSZ Wchan COMMAND
36264 33126 root 36265 36264 nobody 0.0 1364 kqread nginx:worker process (ngin x)
36266 36264 nobody 0.0 1364 kqread nginx:worker process (nginx)
36267 36264 nobody 0.0 1364 kqread nginx:worker process (nginx)
Add Module

Just contact Nginx, only know that Nginx function is sub-module, it is not clear that some modules are not involved in the construction of the default, such as ngx_http_ssl_module modules, is used to support https the protocol, by default is not built into Nginx.

With the expansion of the business, if you need nginx support some modules, and these modules are not in the Nginx build plan, when the build Nginx, and did not specify to add these modules, what should I do? Is it possible to add these modules dynamically to the Nginx that is already running? The answer is YES!

The scheme of adding modules dynamically to Nginx in operation is very similar to the scheme of smooth upgrade nginx as mentioned above. Let's take a look at how to add a module to a running Nginx ngx_http_ssl_module .

The first step is to view the Nginx version in operation and download and unzip the corresponding version of the compressed package.

To view Nginx versions:

/usr/local/nginx/sbin/nginx-v

Results:

[Email protected]:~$/usr/local/nginx/sbin/nginx-v
Nginx version:nginx/1.11.1
[Email protected]:~$

Or:

/usr/local/nginx/sbin/nginx-v

Results:

[Email protected]:~$/usr/local/nginx/sbin/nginx-v
Nginx version:nginx/1.11.1
Built by GCC 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
Configure Arguments:–prefix=/usr/local/nginx
[Email protected]:~$

As can be seen here, the version of the Nginx is running 1.11.1 , refer to the Installation Nginx section, download and unzip the corresponding version of Nginx

The second step is to compile nginx, and add the module configuration

Refer to the third step of the smooth upgrade, view the configuration parameters of the Nginx in operation, and append at the end -with-http_ssl_module .

For example: The original configuration information is --prefix=/usr/local/nginx , then the new configuration information is --prefix=/usr/local/nginx --with_http_ssl_module , configure Nginx to execute the following commands:

./configure--prefix=/usr/local/nginx--with_http_ssl_module
Third Step smooth restart Nginx, complete dynamic module add

This step can refer to the fourth to eighth step of the smooth upgrade

installation, upgrade and dynamic adding module of Linux Nginx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.