Compiling Nginx from source provides greater control over the binary features and optimization for specific server environments. This process involves setting up the build enviroment, installing necessary libraries, and executing the compilation workflow.
1. Preparation of Installation Directories
Create a dedicated workspace to store the source code packages. A common practice is using the /opt directory for software source management.
mkdir -p /opt/source/nginx_build
cd /opt/source/nginx_build
Ensure that the following source tarballs are placed in this directory:
nginx-1.20.1.tar.gzopenssl-1.0.1t.tar.gzpcre-8.37.tar.gzzlib-1.2.8.tar.gz
2. Environment Setup
Nginx and its depandencies require C and C++ compilers. Use the package manager to verify and install these build tools.
yum install -y gcc gcc-c++
3. Building Dependencies
Nginx relies on PCRE (for regular expressions), OpenSSL (for HTTPS/TLS), and Zlib (for Gzip compression). These must be compiled and installed sequentially.
PCRE Installation
tar -zxvf pcre-8.37.tar.gz
cd pcre-8.37
./configure
make
make install
cd ..
OpenSSL Installation
tar -zxvf openssl-1.0.1t.tar.gz
cd openssl-1.0.1t
./config
make
make install
cd ..
Zlib Installation
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
cd ..
4. Compiling and Installing Nginx
Once the libraries are ready, proceed to compile the Nginx binary. The default installation path is typically /usr/local/nginx.
tar -zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
./configure
make
make install
5. Post-Installation Verification
The installation creates four main subdirectories under /usr/local/nginx:
conf: Configuration files (e.g.,nginx.conf)html: Static web assetslogs: Log files and PID filessbin: The Nginx binary executable
6. Service Management Commands
Navigate to the binary directory to manage the Nginx service lifecycle.
cd /usr/local/nginx/sbin
Start Nginx
./nginx
Stop Nginx
./nginx -s stop
Reload Configuration
To apply changes in nginx.conf without restarting the service:
./nginx -s reload
Check Version Information
./nginx -v
7. Configuration File Location
The primary configuration file is located at:
/usr/local/nginx/conf/nginx.conf
You can verify the presence of the default configuration and its backups in the conf folder using ls -l /usr/local/nginx/conf.