NGINX is a powerful open-source web server capable of serving dynamic web content and handling high-traffic loads while acting as a reverse proxy. Its reputation is built on high performance, stability, and low resource consumption. Building NGINX from the source and compiling it with additional modules can enhance its capabilities and make it more suitable for specific needs. This guide will explain the advantages of building NGINX from the source and list some of its key features and benefits.
Features and Benefits of building NGINX from source:
- Flexibility: Building NGINX from the source allows you to customize and configure the software to suit your specific requirements.
- Control: By building from the source, you have complete control over the software and can ensure that it is up-to-date and secure.
- Performance: Building from the source can improve performance as it allows you to optimize the software for your specific environment and hardware.
- Compatibility: Building from the source allows you to add and remove modules as required, ensuring compatibility with other software and technologies that you are using.
- Security: Building from the source allows you to apply security patches and updates as soon as they are available, keeping your server and website more secure.
Table of Contents
Step 1: Update and Upgrade Ubuntu
Before we begin, ensuring that your Ubuntu system is up to date is essential. Run the following commands in the terminal to update and upgrade your system:
Step 2: Install Required Dependencies
To compile Nginx, we need to install some dependencies. Run the following command to install them:
sudo apt install build-essential libpcre3-dev libssl-dev zlib1g-dev libgd-dev
Step 3: Download Nginx Source Code
Once you have the necessary dependencies installed, visit the NGINX website to download the source code. You can choose from the latest mainline, stable version, or any other version you prefer. Once you have identified the appropriate version, use the wget command to download it.
wget http://nginx.org/download/nginx-x.x.x.tar.gz
For this guide, an example link for the latest Nginx mainline version 1.23.3 is provided below, but it is essential to check for newer versions and not just blindly copy this one.
wget https://nginx.org/download/nginx-1.23.3.tar.gz
Step 4: Extract the Source Code
The source code is compressed in a tarball, so we need to extract it. Please run the following command to extract the source code using our example as a reference.
tar -xzvf nginx-1.23.3.tar.gz
Next, navigate to the newly extracted directory.
cd nginx-1.23.3
Step 5: Configure Nginx Options
Configure the options for building Nginx from the source; you can use the following command to configure NGINX with the most commonly used options and setup paths.
./configure --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module
Example output:
Next, I will review some examples where you can customize your NGINX installation with the ./configure script. For example, you would use the following command to configure Nginx with support for the HTTP/2 module and the PCRE library.
./configure --with-http_v2_module --with-pcre
It’s important to note that some options may require you to download additional modules before using the ./configure script included with the Nginx source code.
You can also specify the prefix path for the Nginx installation using the –prefix option. For example, install Nginx to the /usr/local/nginx directory.
./configure --prefix=/usr/local/nginx
You can also specify the path of additional modules to include with Nginx using the –add-module option. For example, to include the ngx_cache_purge module.
./configure --add-module=/path/to/ngx_cache_purge
You can also specify the path of additional libraries to include with Nginx using the –with-XXX-module option. For example, to include the libxslt library.
./configure --with-libxslt-module
You can also specify the path of additional libraries to include with Nginx using the –with-XXX-module option. For example, to include the libssl library:
./configure --with-openssl=/path/to/openssl
You can also specify the configuration options for nginx with –with-http_ssl_module.
./configure --with-http_ssl_module
You can also specify the configuration options for nginx with –with-http_realip_module.
./configure --with-http_realip_module
These are just a few examples of the options used with the ./configure script. You can view a complete list of options by running ./configure –help.
Step 6: Install Nginx (Compile and Build Nginx)
After configuring the options for building Nginx from the source, you can run the following commands to build and install Nginx.
The first command, make, will compile the Nginx source code using the options specified in the ./configure
script. This will create the Nginx binary executable, which can be found in the objs directory.
make
Example output:
The second command, sudo make install, will install the Nginx binary, configuration files, and other files to the prefix path specified in the ./configure script (by default, it installs to /usr/local/nginx/).
sudo make install
Example output:
After installation, Nginx will be located in the sbin directory of the prefix path.
Step 7: Create NGINX SystemD Service
After building and compiling NGINX from the source, you can create a systemd process to manage the NGINX service on your system. Here are the steps to create a systemd service for NGINX on Ubuntu:
Create a new systemd service file by running the following command.
sudo nano /etc/systemd/system/nginx.service
Add the following content to the file, and replace /path/to/nginx with the actual path to the NGINX binary if it is different than the /usr/sbin/nginx location.
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Reload the systemd daemon by running the following command.
sudo systemctl daemon-reload
Start the NGINX service using the following command.
sudo systemctl start nginx
Enable the NGINX service to start automatically at boot.
sudo systemctl enable nginx
Step 7: Test Nginx
To test that Nginx is running correctly, open a web browser and navigate to the test page using your local host or server IP address. An example below, replace 192.128.1.1 with your server IP address if localhost does not work.
http://localhost
## OR
http://196.128.1.1
Additional Commands & Tips
Compile Nginx with Additional Modules
It is possible to enhance the functionality of NGINX by compiling it with additional modules. In this example, we will demonstrate how to use the Nginx HTTP push module. We will do this by using the –add-module flag during the configuration of NGINX. The following commands should be executed:
./configure --add-module=/path/to/nginx-http-push-module
make
sudo make install
Nginx Service Commands
Here are some standard nginx systemctl service commands that can be run in a Linux terminal on an Ubuntu system:
To start the nginx service:
sudo systemctl start nginx
To stop the nginx service:
sudo systemctl stop nginx
To restart the nginx service:
sudo systemctl restart nginx
To reload the nginx service configuration:
sudo systemctl reload nginx
To check the status of the nginx service:
sudo systemctl status nginx
To enable the nginx service to start at boot:
sudo systemctl enable nginx
To disable the nginx service from starting at boot:
sudo systemctl disable nginx
Note: The above commands are for Ubuntu systems that use systemctl as the service manager, and other distributions may use different commands to manage services.
Conclusion
In this article, we have gone through the steps to compile Nginx on Ubuntu 22.04 or 20.04 using the command line terminal. By compiling Nginx from the source, you can customize your installation by including additional modules not included in the default package. By following the steps outlined in this article, you should be able to have Nginx up and running in no time. Remember that you can add other modules following the same pattern as the example provided, such as the Nginx HTTP push module.