NGINX: Building from Source and Installation on Linux (Updated)

In this blog we are going to build NGINX from source code. And we will configure NGINX's settings, paths, and add or remove modules.

WORDS: 475 | CODE BLOCKS: 9 | EXT. LINKS: 7

NGINX is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. NGINX is a free and open source software licensed under 2-clause BSD.
You can easily download and use NGINX in Windows, macOS, Linux and other operating systems link read NGINX installation instructions from the official docs and install on your operating system for free.

But, in this blog we are going to build NGINX from source code. And we will configure NGINX’s settings, paths, and add or remove modules.

So let’s get started…

Preparation

We’ll use Ubuntu 20.04 LTS as our operating system, though these instructions are applicable to other Linux-based systems as well.

1. Download the source code:

Start by visiting the NGINX download page and choose either the stable release or the mainline version for experimental features. For example, you can download nginx-1.21.4.

Link: NGINX download page.
Note: You can use wget or curl to download.

shell
1wget https://nginx.org/download/nginx-1.21.4.tar.gz

After downloading, extract the tar file using:

shell
1tar -zxvf nginx-1.21.4.tar.gz

Now cd into nginx-<version> directory.

2. Install Required Tools:

Ensure you have the necessary build tools. On Ubuntu, use apt package manager:

shell
1sudo apt install build-essential

Additionally, install required dependencies and libraries:

shell
1sudo apt install libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

3. Configure NGINX for build

Check all configuration options:

sudo ./configure --help

Refer to the NGINX documentation’s module reference section for details on modules. Configure installation paths and modules as needed.
Follow the link: nginx docs > module reference section

Configure Installation:

shell
 1sudo ./configure \
 2  --sbin-path=/usr/bin/nginx \
 3  --conf-path=/etc/nginx/nginx.conf \
 4  --error-log-path=/var/log/nginx/error.log \
 5  --http-log-path=/var/log/nginx/access.log \
 6  --pid-path=/var/run/nginx.pid \
 7  --with-pcre \
 8  --with-http_ssl_module \
 9  --with-http_v2_module \
10  --with-http_gunzip_module

–sbin-path=/usr/bin/nginx
–conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–pid-path=/var/run/nginx.pid
–with-pcre –with-http_ssl_module
–with-http_v2_module
–with-http_gunzip_module

4. Compile and Install the Application:

Compile NGINX by running:

shell
1make
2# Run below command to install NGINX
3make install

5. Restart NGINX and Configure as a Service:

Reload NGINX to apply changes:

nginx -s reload

If you encounter an error related to the nginx.pid file missing, follow these steps:

nginx: [error] open() “/var/run/nginx.pid” failed (2: No such file or directory)

5.1. Add Nginx as service:

Add NGINX as a service by creating a systemd config file. Create or modify /lib/systemd/system/nginx.service with appropriate paths:
Visit: nginx systemd config and create the file.
--/lib/systemd/system/nginx.service--
Change pid and sbin path as set in step 3.

/lib/systemd/system/nginx.service
 1[Unit]
 2Description=The NGINX HTTP and reverse proxy server
 3After=syslog.target network-online.target remote-fs.target nss-lookup.target
 4Wants=network-online.target
 5
 6[Service]
 7Type=forking
 8PIDFile=/var/run/nginx.pid
 9ExecStartPre=/usr/bin/nginx -t
10ExecStart=/usr/bin/nginx
11ExecReload=/usr/bin/nginx -s reload
12ExecStop=/bin/kill -s QUIT $MAINPID
13PrivateTmp=true
14
15[Install]
16WantedBy=multi-user.target

5.2. Start NGINX using systemd:

sh
1sudo systemctl start nginx

5.3. Enable NGINX to start on boot:

sh
1sudo systemctl enable nginx

That’s all you need to do to compile and run NGINX on Ubuntu.
If you get any error or suggestion please leave a comment.

Happy NGINX-ing! 🤟