Ad Code

Build NGINX from source on Ubuntu


Photo by Blaz Erzetic from Pexels


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

I will be using Ubuntu 20.04 LTS as the operating system, this guide will work with other linux based operating systems as well.

1. Download the source code:

Visit NGINX download page by clicking here. Download the source code from stable link or choose mainline if you want to experiment new features. (I downloaded nginx-1.21.4.
Nginx download page.

Use wget or curl to download.

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

Extract the tar, replace filename with the version you downloaded.

tar -zxvf nginx-1.21.4.tar.gz

Now cd into nginx-<version> directory.

2. Install required tools:

First we need build essential tools.

I’m using Ubuntu so I will use apt package manager

sudo apt install build-essential

Install additional 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 go to nginx docs > module reference section for details of modules.

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

Run below commands to compile and install NGINX on Ubuntu.

make 

make install 

5 Restart NGINX and configure as service

Reload NGINX nginx -s reload .

If following error occurs then follow below steps

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

Add Nginx as service :
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

Start nginx using systemd (systemctl)

sudo systemctl start nginx

Enable nginx startup 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.

😀🤟

Post a Comment

0 Comments

Ad Code