nginx linux

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

Posted by Vikash Patel on Friday, Feb 2, 2024 (Updated Wednesday, Feb 7, 2024) Reading time: 3 Min

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…


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.


After downloading, extract the tar file using:

tar -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:

sudo apt install build-essential

Additionally, install required dependencies and libraries:

sudo 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:

sudo ./configure \
  --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/ \
  --with-pcre \
  --with-http_ssl_module \
  --with-http_v2_module \

–with-pcre –with-http_ssl_module

4. Compile and Install the Application:

Compile NGINX by running:

# Run below command to install NGINX
make 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 file missing, follow these steps:

nginx: [error] open() “/var/run/” 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.
Change pid and sbin path as set in step 3.

Description=The NGINX HTTP and reverse proxy server  

ExecStartPre=/usr/bin/nginx -t  
ExecReload=/usr/bin/nginx -s reload  
ExecStop=/bin/kill -s QUIT $MAINPID  


5.2. Start NGINX using systemd:

sudo systemctl start nginx

5.3. Enable NGINX to start on boot:

sudo 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! 🤟

comments powered by Disqus