×
Community Blog How to Build NGINX from Source on Ubuntu 20.04 LTS

How to Build NGINX from Source on Ubuntu 20.04 LTS

In this guide, we'll build NGINX from the source on Ubuntu 20.04 with an Alibaba Cloud Elastic Compute Service (ECS) instance.

By Arman Ali

NGINX was created on 4 October 2004 by Russian developer Igor Sysoev as he was frustrated with Apache web server and wanted to build a replacement capable of handling 10,000 concurrent connections with a focus on performance, high concurrency, and low memory usages.

Now NGINX servers world's top websites and while this growth is largely due to, it's also because NGINX is relatively easy to get started with.

There are two methods of installing NGINX on Ubuntu 20.04.

  1. Via the operating system's build-in packages manager.
  2. Via building NGINX from the source.

In this guide, we'll build NGINX from the source on Ubuntu 20.04.

Prerequisites

To complete this guide, you will need to have an Ubuntu 20.04 server installed on your machine. We will be using an Alibaba Cloud Elastic Compute Service (ECS) instance for our guide.

Step 1 - Update and Install Dependencies for NGINX

In order to build NGINX from the source first, we need to install a couple of dependencies for NGINX.

Login to your server via SSH terminal.

    ssh username@you-IP-address

Update the Ubuntu's package manager

    sudo apt-get update 

Now, install development libraries along with source code compilers.

    sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev

Press Y for yes and press Enter.

Step 2 - Download NGINX Source Code and Configure

We now have all the necessary tools to compile NGINX.

Now, we need to download the NGINX source from their Official website.

Run the following command to download the source code.

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

We have now NGINX source code in tarball format. We can extract it by using this command

    tar -zxvf nginx-1.20.0.tar.gz

Go to the extracted directory by using this command

    cd nginx-1.20.0

Now we have to use the configure flag for configuring NGINX by using this 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

In the above command, we configured our custom path for the NGINX configuration file, access, and Error Log path with some NGINX's module.

Step 3 - Build NGINX & Adding Modules

There are many configuration options available in NGINX, you can use it as per your need. To find all the configuration options available in NGINX by visiting nginx.org.

There are some modules that come pre-installed in NGINX.

Modules Built by Default

There many modules comes with NGINX pre-installed If you don't need a module that is built by default, you can disable it by naming it with the --without-<MODULE-NAME> option on the configure script, for example:

    ./configure --without-http_empty_gif_module

Compiling the NGINX source code

After custom configuration complete we can now compile NGINX source code by using this command :

    make 

This will take quite a bit of time and once that's done install the compiled source code by using this command.

    make install

Start NGINX by using this command

    nginx

Now we have successfully installed NGINX. To verify this, check the NGINX version by using this command.

    nginx -V

Or you can visit your IP to see the holding page NGINX.

    http://your-IP-address 

1

Step 4 - Adding a NGINX Service

With our custom-built of NGINX working and listening on HTTP Port 80, the next step is configuring system service for NGINX.

More specifically will be adding NGINX as a systemd service, the newer and more popular standard for services.

Now, before we continue to note that systemd is only available since Ubuntu 15.04.

Creating an NGINX service will not only allow us to manage starting, stopping and reloading NGINX in a more standardized way but also make starting NGINX on boot much simpler.

Standard NGINX Command-Line Tools

Before we start, however, let's quickly see how to use the standard NGINX command-line tools to execute service signals.

We can confirm that NGINX is running by checking for the process.

ps aux | grep nginx 

We can see the master and worker process here.

So with NGINX running in the background, let's see how to send it a stop signal. Using the standard command-line tools.

For example, with NGINX running, we can send the stop signal with NGINX by using this command.

nginx -s stop

You can check the NGINX status by visiting your IP address; you will not see any holding page as NGINX is now stopped or more accurately terminated.

So next, let's add that systemd service.

To enable the service, we're going to have to add a small script, which is the same across operating systems.

Create an Nginx systemd unit file by using nano editor

nano /lib/systemd/system/nginx.service

and paste this script

[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

You can change the PIDfile location as per your custom configuration path.

Now, save the file by pressing the key CTRL+X, Y, and Enter to save this file.

Start your NGINX by using systemd with this command.

systemctl restart nginx

Now you can manage your NGINX by using Systemd.

You can also check the status of NGINX whether it is running or not by using this command.

systemctl status nginx

This gives us a really informative printout of the NGINX server status.

Step 5 - Enable NGINX on Boot

Now, as we mentioned, the other very useful feature of a systemd service is enabling NGINX to start automatically when the system Boots at the moment, when this machine is shut down or rebooted NGINX will no longer be running.

Obviously not good for a web server in particular.

So to enable start-up on boot, run this command.

systemctl enable nginx

So we get confirmation of a start-up, symlink being created for this service.

We can test this by rebooting the machine.

That's it!

Conclusion

In this guide, we have built NGINX from source on Ubuntu 20.04. If you'd like to host your application on NGINX, you can use Alibaba Cloud's powerful yet affordable Elastic Compute Service (ECS).

1 2 2
Share on

Arman Ali

3 posts | 1 followers

You may also like

Comments

Dikky Ryan Pratama May 4, 2023 at 5:31 pm

Your post is very inspiring and inspires me to think more creatively., very informative and gives interesting new views on the topic., very clear and easy to understand, makes complex topics easier to understand, very impressed with your writing style which is smart and fun to work with be read. , is highly relevant to the present and provides a different and valuable perspective.

Arman Ali

3 posts | 1 followers

Related Products