How to Build NGINX from Source on Ubuntu 22.04

Introduction

Before we begin talking about how to build NGINX from Source on Ubuntu 22.04, let's briefly understand – What is NGINX?

NGINX is a popular web server that manages internet traffic efficiently. It acts as a mediator between users and web servers, handling tasks like load balancing, caching, and security. NGINX optimizes websites, ensuring they load quickly and securely.

It is widely used due to its speed, scalability, and reliability. Businesses rely on NGINX to deliver websites and applications seamlessly to users across the globe. If you want to enhance your website's performance, NGINX is a trusted choice for managing web traffic effectively.

In this tutorial, you will build NGINX from Source on Ubuntu 22.04. We will also address a few FAQs on how to build NGINX from Source on Ubuntu 22.04.

Install Initial Packages For Nginx Source on Ubuntu 22.04

Making sure your Ubuntu system is up-to-date is crucial before we start. Use the terminal to enter the following commands to upgrade and update your system:

sudo apt update && sudo apt upgrade

Next, compile Nginx; some dependencies need to be installed. Utilize the subsequent instruction to install them:

sudo apt install build-essential libpcre3-dev libssl-dev zlib1g-dev libgd-dev

Download Nginx Source Code on Ubuntu 22.04

Download the source code from the NGINX website after installing the required dependencies. The most recent mainline version, the stable version, or any other version you like can be selected. Use the wget command to download the relevant version after you've located it.

wget http://nginx.org/download/nginx-x.x.x.tar.gz

An example link to the most recent Nginx mainline version 1.27.0 is given below for this guide, but you should always check for updates rather than just copying this one.

wget https://nginx.org/download/nginx-1.27.0.tar.gz

Extract Nginx Source Code on Ubuntu 22.04

Take the tarball and extract the source code. Utilize the subsequent command, keeping in mind our example:

tar -xzvf nginx-1.23.3.tar.gz

Proceed to the newly extracted directory.

cd nginx-1.23.3

Configure Build Options for Nginx on Ubuntu 22.04

Set up the options so that Nginx can be built from the source. To configure NGINX with the most often used setup paths and options, use the following command.

./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
Terminal output showcasing the configuration steps for building NGINX on Ubuntu 22.04

Next, I'll go over a few instances of how to use the ./configure script to personalize your NGINX installation. To configure Nginx with support for the HTTP/2 module and the PCRE library, for instance, use the following command.

./configure --with-http_v2_module --with-pcre

It's crucial to remember that, in order to use the ./configure script that comes with the Nginx source code, some options might need you to download extra modules.

The --prefix option allows you to specify the prefix path for the Nginx installation as well. Install Nginx, for instance, in the /usr/local/nginx directory.

./configure --prefix=/usr/local/nginx

With Nginx, you can also use the --add-module option to specify the path of additional modules to include. To incorporate the ngx_cache_purge module, for instance.

./configure --add-module=/path/to/ngx_cache_purge

With the --with-XXX-module option, you can also specify the path of additional libraries to include with Nginx. To incorporate the libxslt library, for instance.

./configure --with-libxslt-module

With the --with-openssl=/path/to/openssl option, you empower Nginx to leverage the OpenSSL library. To specify the location of your OpenSSL installation, use the /path/to/openssl placeholder with the actual directory path.

./configure --with-openssl=/path/to/openssl

With --with-http_ssl_module, you can also specify the nginx configuration options.

./configure --with-http_ssl_module

You can also use --with-http_realip_module to specify the nginx configuration options.

./configure --with-http_realip_module

Some of the options you can use with the ./configure script are highlighted in these examples. Run ./configure --help to view the full list of options.

Compile Nginx on Ubuntu 22.04

The following commands can be used to build and install Nginx after setting up the options for building it from the source.

Using the parameters from the ./configure script, the make command compiles the Nginx source code. The Nginx binary executable, which is stored in the objs directory, is the result of this process.

make
The ‘make’ command output during the NGINX build process on Ubuntu 22.04

The Nginx binary, configuration files, and other files will be installed to the prefix path indicated in the./configure script (by default, it installs to /usr/local/nginx/) by the second command, sudo make install.

sudo make install
Terminal output of the ‘make’ and ‘install’ commands while building and compiling NGINX on Ubuntu 22.04

Nginx can be found in the prefix path's sbin directory once it has been installed.

Create NGINX SystemD Service on Ubuntu 22.04

To manage the NGINX service on your system, create a systemd process after building and compiling NGINX from the source. To set up a systemd service for NGINX on Ubuntu, follow these steps:

To create a new systemd service file, use the command below.

sudo nano /etc/systemd/system/nginx.service

Replace /path/to/nginx with the real path to the NGINX binary, if it is different from the location of /usr/sbin/nginx, and add the following content to the file.

[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

Use the following command to reload the systemd daemon.

sudo systemctl daemon-reload

Use the command below to launch the NGINX service.

sudo systemctl start nginx

Make the NGINX service run automatically when the computer boots up.

sudo systemctl enable nginx

Test Nginx on Ubuntu 22.04

Open a web browser and use your local host or server IP address to navigate to the test page in order to verify that Nginx is operating properly. If localhost is not working, try substituting your server's IP address for 192.128.1.1, as shown in the example below.

http://localhost

## OR

http://196.128.1.1

Additional Tips Building Nginx on Ubuntu 22.04

Compile Nginx with Additional Modules on Ubuntu

By adding more modules to NGINX, you can increase its functionality. The integration of the Nginx HTTP push module is demonstrated by this example. Use the --add-module flag when configuring NGINX to accomplish this. Run the following commands:

./configure --add-module=/path/to/nginx-http-push-module
make
sudo make install

Nginx Service Commands on Ubuntu

On an Ubuntu system, use the following common nginx systemctl service commands in a Linux terminal:

Start the nginx service:

sudo systemctl start nginx

Stop the nginx service:

sudo systemctl stop nginx

Restart the nginx service:

sudo systemctl restart nginx

Reload the nginx service configuration:

sudo systemctl reload nginx

Check the status of the nginx service:

sudo systemctl status nginx

Enable the nginx service to start at boot:

sudo systemctl enable nginx

Disable the nginx service from starting at boot:

sudo systemctl disable nginx

FAQs to Build NGINX from Source on Ubuntu 22.04

What are the prerequisites for building NGINX from source on Ubuntu 22.04?

Ensure you have essential development tools, PCRE library, and zlib development files installed on your system.

How can I download the NGINX source code on Ubuntu 22.04?

You can download the latest NGINX source code from the official NGINX website or directly from their GitHub repository.

Can I customize modules and features during the NGINX build process on Ubuntu 22.04?

Yes, you can enable or disable specific modules/features by passing appropriate options to the configure script before compiling.

What is the advantage of building NGINX from source over using package managers on Ubuntu 22.04?

Building from source provides more control over NGINX configurations, optimizations, and ensures you have the latest version available.

What is the general process of building NGINX from source on Ubuntu 22.04?

The process involves configuring NGINX with desired modules and options, compiling the source code, and installing the compiled binaries.

How can I ensure the security of NGINX when building from source on Ubuntu 22.04?

It's crucial to stay up-to-date with security advisories, apply patches promptly, and follow best practices during the NGINX compilation and installation process.

Does building NGINX from source on Ubuntu 22.04 affect its compatibility with other software or tools?

Depending on the configuration options chosen, building NGINX from source may impact compatibility with certain software or modules. Compatibility testing is recommended.

Conclusion

We hope this tutorial helped you understand how to build NGINX from Source on Ubuntu 22.04.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.