How to Install and Configure Nginx on Ubuntu

Nginx, pronounced as “Engine-x”, is a popular high-performance Linux web server that is also used as a reverse proxy and load balancer. In particular, it connects multiple internet resources through HTTP. 

The primary job of Nginx is to store, process, and then deliver website content to users. To access a website, a web browser makes a request through HTTP. The request is received by the web server, which then gets approved, and the webserver (in this case, Nginx) sends the required material, using HTTP, to the browser.

Performance is optimized with Nginx since it uses less memory processing web requests with a single thread, and each request is carried over simultaneously and seamlessly. If you don’t know how to install and configure Nginx on Ubuntu, read this guide, as we will be explaining every step for doing it.

So, without further ado, let’s get started!

Step 1. Installing the Nginx Package.

The Nginx package is already available in Ubuntu’s default repositories. This will save you the time and hassle of needing to download the Nginx package first for installation and configuration. 

You can install the package directly using the “apt” package system. Before that, you should first update your packages so you can access the latest versions. 

Follow these steps to successfully install the Nginx package on your system:

  1. First, open the Command Terminal on your system.
  2. Once opened, type the following command:
$ sudo apt update
update
  1. Once the updates are finished, type the following command to install the Nginx package:
$ sudo apt install nginx
Install and Configure Nginx on Ubuntu
  1. Lastly, verify the presence of Nginx using this command: 
$ nginx -v
Install and Configure Nginx on Ubuntu

With this, you are done with the first step. Next, we move on to the configuration phase.

Step 2. Starting the Nginx Service.

After Nginx has been installed, you need to know the different commands used to either launch, enable/disable or check the status of the service. We will be going through each command.

Follow these steps:

  1. First, open the Terminal.
  2. Next, type this command to first check the status of Nginx:
$ sudo systemctl status nginx
sudo systemctl

If you get active or running on output, Nginx is working. 


  1. In case Nginx isn’t running, use this command to launch it:
$ sudo systemctl start nginx
  1. To stop Nginx, this command is used:
$ sudo systemctl stop nginx
  1. Afterward, to automatically launch Nginx on system startup, use this command:
$  sudo systemctl enable nginx
  1. To prevent Nginx from launching at startup, you can use this command:
$ sudo systemctl disable nginx

These are the few important commands used to launch and disable Nginx. 

Step 3. Adjusting the Firewall for Nginx.

Before you test out the Nginx service, you must adjust your firewall setting to direct safe internet traffic to your server. There are three profiles available with Nginx:

  • Nginx HTTP: This profile uses port 80 for normal and unencrypted web traffic.
  • Nginx HTTPS: This profile uses port 443 for TLS/SSL encrypted traffic.
  • Nginx Full: This profile uses both ports 80 and 443 to manage internet traffic.

Experts who know what they are doing can choose any profile available. For beginners, it’s best to use Nginx HTTP.

Follow these steps to choose your preferred profile:

  1. Open the Terminal.
  2. Type the following command in the Terminal:
$ sudo ufw app list

You’ll be given this list as your output:

sudo
  1. To choose the ‘HTTP‘ profile, use this command:
$ sudo ufw allow 'Nginx HTTP'
Install and Configure Nginx on Ubuntu

With this, you’re now aware of how to configure your firewall settings.

Step 4. Accessing the Nginx landing page.

The $ status command should ensure that your web server is working, but there is another way to be sure of it. 

To do this, access the Nginx landing page using your web server’s IP address. If you can access it, then the software is working properly. If you don’t know the IP address of your web server, you can use the ‘icanhazip.com‘ tool to find it out.

Follow these steps to access your Nginx landing page:

  1. First, open the Command Terminal.
  2. Once opened, type in this command to find the IP address of your server:
$ curl -4 icanhazip.com

When you get the IP, open any web browser and type it in the address bar. This should lead you to the Nginx landing page.

With this, you have now completed the basic steps on how to install and configure Nginx on Ubuntu.

Setting Up Server Blocks.

Nginx enables you to set up different domains on a single web server. This feature is known as “server blocks”. In Ubuntu, there is one server block enabled by default. This server block functions out of the “/var/www/html” directory. The directory works fine for a single website but for multiple sites, it can become a mess. 

To create your domain, you will make another directory within “/var/www” that will host it. 

Follow these steps to set up your domain on Nginx:

  1. First, open the Terminal.
  2. Next, create a directory using the following command:
$ sudo mkdir -p /var/www/domain_name/html
Install and Configure Nginx on Ubuntu

In it, “domain_name” is the title you will give to your domain. 

  1. With that done, give ownership of the domain using the $USER command by typing the following:
$ sudo chown -R $USER:$USER /var/www/domain_name/html

To ensure the owner has permission to read, write, and execute the files, while others can only read and execute the files, use this command:

$ sudo chmod -R 755 /var/www/domain_name
  1. To create a sample HTML page, use the following command:
$ nano /var/www/domain_name/html/sample.html
  1. Upon opening, write the text as shown in the image:
Install and Configure Nginx on Ubuntu
  1. Now you need to make a new configuration file so that Nginx can host this domain correctly:
$ sudo nano /etc/nginx/sites-available/domain_name
  1. Make the following edits:
Install and Configure Nginx on Ubuntu
  1. Next, create a link to enable the file:
$ sudo ln -s /etc/nginx/sites-available/domain_name/etc/nginx/sites-enabled/
  1. After this, you need to test to make sure you have made no errors:
$ sudo nginx -t
  1. Lastly, if there are no errors, restart Nginx to apply your changes:
$ sudo systemctl restart nginx

With this, you have made two server blocks and you can access your site by opening a web browser and typing “http://domain_name” in the address bar.

Furthermore, if you want to make your Ubuntu device more secure, learning how to add fingerprint login in Ubuntu and other Linux distros should help you out.

That ends this guide on how to install and configure Nginx on Ubuntu. We hope that we helped you understand each step and you were able to execute them successfully. If you have any queries or doubts, let us know in the comment section down below.

If this guide helped you, please share it. 🙂

Author

Leave a Reply

Your email address will not be published. Required fields are marked *