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…

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.

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

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/nginx.pid \
  --with-pcre \
  --with-http_ssl_module \
  --with-http_v2_module \
  --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:

make 
# 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 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.

[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/bin/nginx -t  
ExecStart=/usr/bin/nginx  
ExecReload=/usr/bin/nginx -s reload  
ExecStop=/bin/kill -s QUIT $MAINPID  
PrivateTmp=true  

[Install]  
WantedBy=multi-user.target

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