Prerequisites and Dependency Installation
Before compiling Nginx, ensure your system's package lists are up-to-date and install the necessary development libraries.
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y build-essential libpcre3 libpcre3-dev zlib1g-dev libssl-dev
build-essential: Includes essential tools for compilation likemakeandgcc.libpcre3andlibpcre3-dev: Provide runtime and development files for the PCRE library, used for regular expressions.zlib1g-dev: Contains development files for the zlib compression library.libssl-dev: Provides development files for the OpenSSL library, enabling HTTPS support.
Download and Extract Nginx Source Code
Obtain the Nginx source code archive and extract it to a working directory.
# Download the desired Nginx version (e.g., 1.20.2)
# wget http://nginx.org/download/nginx-1.20.2.tar.gz
# Create a directory for Nginx source files
mkdir ~/nginx_source
cd ~/nginx_source
# Extract the archive
tar -zxvf /path/to/nginx-1.20.2.tar.gz
# Navigate into the extracted directory
cd nginx-1.20.2
Compile and Install Nginx
Configure the build with desired modules, compile the source code, and install the binaries.
# Configure the build with specified modules and installation prefix
./configure \
--prefix=/usr/local/nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_flv_module
# Compile the source code
make
# Install Nginx to the specified prefix
make install
# Verify installation by listing the contents of the installation directory
ls /usr/local/nginx
--prefix=/usr/local/nginx: Sets the base directory for Nginx installation.--with-pcre: Enables regular expression support.--with-http_ssl_module: Activates HTTPS capabilities.--with-http_gzip_static_module: Allows pre-compressed gzip static files.--with-http_v2_module: Enables HTTP/2 protocol support.--with-http_realip_module: Allows retrieving the client's real IP address from proxy headers.--with-http_stub_status_module: Provides a status page for monitoring.--with-http_flv_module: Supports streaming FLV media.
Start Nginx to confirm it's running:
# Start Nginx
/usr/local/nginx/sbin/nginx
# Check if Nginx is listening on port 80
sudo ss -pantu | grep nginx
Control Nginx using the following commands:
./nginx -s stop: Immediately stops Nginx../nginx -s quit: Gracefully stops Nginx, completing current requests../nginx -s reload: Reloads the configuration file without interrupting service.
Create a systemd Service File
To manage Nginx as a system service, create a systemd unit file.
Create and edit the service file:
sudo vim /etc/systemd/system/nginx.service
Populate the file with the following content:
[Unit]
Description=Nginx Web Server
After=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /usr/local/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s QUIT $(/bin/cat /usr/local/nginx/logs/nginx.pid)"
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Reload systemd, start, check the status, and enable the Nginx service:
sudo systemctl daemon-reload
sudo systemctl start nginx.service
sudo systemctl status nginx.service
sudo systemctl enable nginx.service