Installing Nginx by Compiling Source Code
This section uses Nginx 1.14.2 as an example. You can also refer to this section for Nginx of other versions.
Table 1 lists the Nginx versions and OS versions that can be installed by compiling the source code.
Software Version |
Whether to Support CentOS 7.6 |
Whether to Support CentOS 8.1 |
Whether to Support openEuler 20.03 LTS-SP1 |
Whether to Support openEuler 22.03 LTS-SP1 |
|---|---|---|---|---|
Nginx 1.14.2 |
Yes |
Yes |
Yes |
Yes |
Nginx 1.19.3 |
Yes |
No |
No |
Yes |
Nginx 1.21.5 |
No |
No |
No |
Yes |
In the following commands, 1.14.2 indicates the Nginx version. When installing another version, replace 1.14.2 with the actual version.
- Obtain the Nginx source code.
- Download the source package using the local browser.
- Upload the source package to the /home directory on the server.
If the server is connected to the Internet, run the wget command to download the source code. The following describes how to download the source code of Nginx 1.14.2 by using the wget command. When the source code of other Nginx versions is downloaded, replace the download link of the Nginx source code.
1 2
cd /home wget https://nginx.org/download/nginx-1.14.2.tar.gz --no-check-certificate
- Decompress the Nginx installation package.
1tar -xvf nginx-1.14.2.tar.gz
- Go to the Nginx source code directory.
1cd nginx-1.14.2/
- Configure Nginx.
- If Nginx 1.19.3 is installed on CentOS 7.6 or Nginx 1.14.2 is installed on CentOS 8.1, run the following command to configure Nginx:
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_perl_module --with-pcre --with-cc-opt="-fPIC"
- If Nginx 1.14.2 is installed on CentOS 7.6, run the following command to configure Nginx:
1./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_perl_module --with-pcre --with-openssl=/home/openssl-OpenSSL_1_1_1a
- If Nginx 1.14.2 is installed on openEuler 20.03 or openEuler 22.03, run the following command to configure Nginx:
1./configure --prefix=/usr/local/nginx --with-http_ssl_module
Table 2describes the Nginx parameters.
Table 2 Parameter description Parameter
Description
--prefix=PATH
Specifies the Nginx installation directory. The default installation directory is /usr/local/nginx.
--with-http_ssl_module
Uses the HTTPS module. By default, this module is not built. If you want to use the HTTPS module, ensure that OpenSSL and openssl-devel have been installed.
--with-http_realip_module
Changes the client IP address (for example, X-Real-IP or X-Forwarded-For) in the client request header to enable the back-end server to record the original client IP address.
--with-http_addition_module
Adds text content before or after a response.
--with-http_flv_module
Loads the streaming media (flash).
--with-http_mp4_module
Loads the streaming media (mp4).
--with-pcre
Sets the source code path of the Perl Compatible Regular Expressions (PCRE) library. If Nginx is installed by using the yum command, run --with-pcre to locate the library file. If --with-pcre=PATH is used, you need to download the PCRE library source code (version 4.4-8.30) from the PCRE website and decompress it. The Perl regular expressions are used in the location instruction and ngx_http_rewrite_module module.
--with-openssl=PATH
Specifies the directory where the OpenSSL (version 1.1.1a) source code is decompressed.
--with-zlib=PATH
Specifies the directory where the zlib (version 1.1.3-1.2.5) source code is decompressed. zlib is used by the ngx_http_gzip_module that is enabled by default.
--with-http_stub_status_module
Monitors the Nginx status.
The with-http_stub_status_module module does not need to be configured because it affects the Nginx performance.
--add-module=PATH
Adds a third-party module, for example, the nginx-sticky-module-ng or cache module. Recompilation is required each time a new module is added. (Tengine does not need to be recompiled when a new module is added.)
- If Nginx 1.19.3 is installed on CentOS 7.6 or Nginx 1.14.2 is installed on CentOS 8.1, run the following command to configure Nginx:
- Compile and install Nginx.
1make -j60 && make install
The -j 60 parameter leverages multi-core CPUs to accelerate the compilation. The number following -j indicates the number of CPU cores. You can run the lscpu or cat /proc/cpuinfo | grep processor | wc -l command to query the number of CPU cores. The value specified must be equal to or less than the number of CPU cores.
- View the Nginx installation directory.
1ls /usr/local/nginx - View the Nginx version.
1/usr/local/nginx/sbin/nginx -v