What is Nginx?
Nginx is open-source software that works as a web server, reverse proxy, load balancer and so much more. It was specially designed to render maximum performance and high stability.
The software follows a master-slave architecture, with one master process maintaining multiple slave processes. Nginx was developed by a Russian developer Igor Sysoev as he was frustrated with the Apache web server and wanted a better replacement that could handle 10,000 concurrent connections with low memory usage. Thanks to him, the Nginx web server came into being. Today, Nginx serves some of the world’s top websites and its growth seems to be only going up.
Now let’s see how can we get Nginx on our system with a few easy steps.
- Ubuntu Server 20.04 LTS
- An account with root access
- Required Packages (mentioned throughout the tutorial)
To install Nginx on Ubuntu server, we have two methods:
- With the help of the Operating system’s built-in packages manager
- By building Nginx from the source code
For this tutorial, we have followed the second method. I will show you how you can install Nginx on your Ubuntu server with the help of its source code. Let’s begin!
Step 1: Update and Upgrade Operating System
Before beginning the installation, update and upgrade your operating system with the help of the command below so that you only have updated packages on your server.
sudo apt update && sudo apt upgrade -y
Step 2: Add latest Nginx PPA (Mainline)
This step will commence the installation process. To begin, add latest Nginx PPA by running the command below. In this tutorial, we are installing the mainline PPA.
sudo add-apt-repository ppa:ondrej/nginx-mainline -y && sudo apt update
Step 3: Install Nginx:
After installing the PPA and updating the repository list, you will install Nginx with this command:
sudo apt install nginx-core nginx-common nginx nginx-full
After running this command, you may be asked if you want to keep or replace your existing /etc/nginx/nginx.conf configuration file during installation. We recommend that you keep this existing config file by pressing n. This will allow a copy of the file to be made that you can use in the future if needed.
Step 4: Add Nginx source code to the repository
When installing PPA, by default the source code will not be installed. First, you will have to manually enable it. To do that, open the configuration file in /etc/apt/sources.list.d with an editor as shown below:
sudo nano /etc/apt/sources.list.d/ondrej-ubuntu-nginx-mainline-*.list
Now locate this line and uncomment it by removing the hash symbol.
This is how it should look like:
Now save the file by pressing control + X, then Y and hit enter. The changes will be saved.
Now update the repository list like this:
sudo apt update
Step 5: Download Nginx source
Next, we will download Nginx source code in case you need to compile a dynamic module later. To do that, you need to download and add the source package in the location /etc/local/src/nginx.
Create a directory
Make a directory like this:
sudo mkdir /usr/local/src/nginx && cd /usr/local/src/nginx
Install dependencies and download the package
Now download the source package by running the command below:
sudo apt install dpkg-dev -y && sudo apt source nginx
After running the command, you may come across an error as shown below. It is safe to ignore it.
Step 6: Verify Nginx source version
Run the ls command to list down the directory’s files:
This is how the list will appear in your /usr/src/local/nginx directory:
Now run the command below to make sure the source package version is the same as the Nginx version you just installed on your Operating System.
You should get a similar output as shown below. This means the source package and the Nginx Operating System should have the same version number. In our case, it is 1.21.1.
If you have made it this far, congratulations, you have successfully installed Nginx.
How to remove Nginx
Now that we have seen how to install Nginx, let’s also have a look at how to uninstall it.
Step 1: Stop Nginx:
To do that, first of all, stop Nginx as it will be running by using this command:
sudo systemctl stop nginx
Step 2: Remove Nginx
Now delete Nginx installation by running the following command:
sudo apt-get purge nginx -y && sudo apt autoremove nginx -y
After this, Nginx will no longer be on your operating system.
In this tutorial, you saw how you can easily install Nginx on your Ubuntu server with the help of a few easy-to-follow commands. You also saw how you can remove Nginx from your system with the help of two simple commands.
To see how you can setup Nginx as reverse proxy on Ubuntu 20.04, visit: