Jun 5, 2023 4 min read

How to Install Nginx on Ubuntu 20.04

Install Nginx on Ubuntu 20.04 with our step-by-step tutorial. It's a web server and reverse proxy server that powers many websites worldwide.

Install Nginx on Ubuntu 20.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Nginx on Ubuntu 20.04, let's briefly understand – What is Nginx?

Nginx, pronounced "engine X," is a popular web server and reverse proxy server that powers many websites worldwide. It excels in handling high traffic loads, ensuring speedy content delivery, and improving server performance. Nginx offers robust features like load balancing, caching, SSL/TLS encryption, and HTTP/2 support.

It is highly scalable, lightweight, and efficient, making it an ideal choice for businesses seeking optimal web performance. Whether you run a small blog or a large e-commerce site, Nginx can boost your website's speed and reliability, providing a seamless user experience.

In this tutorial, we will describe how to install and manage Nginx on Ubuntu 20.04.

Advantages of Nginx

  1. High Performance: Nginx handles concurrent connections with low memory usage, ensuring fast and efficient delivery of web content.
  2. Scalability: It easily scales to handle heavy traffic, making it suitable for high-demand websites and applications.
  3. Load Balancing: Nginx evenly distributes traffic across multiple servers, optimizing resource utilization and preventing server overload.
  4. Caching: It accelerates content delivery by caching frequently accessed data, reducing server load and improving response times.
  5. Security: Nginx provides robust security features like SSL/TLS encryption, protecting sensitive data and preventing unauthorized access.

Prerequisites to Install Nginx on Ubuntu 20.04

Make sure that Apache and any other processes are not currently running on ports 80 or 443, and that you are signed in as a user with Sudo rights before moving on.

Installing Nginx

The default Ubuntu repositories include Nginx. Run the following commands to install it:

sudo apt update
sudo apt install nginx

Nginx will start immediately when the installation is finished. You may check it by running the following command:

sudo systemctl status nginx

The output will look something like this:

Output

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-05-02 20:25:43 UTC; 13s ago
...

Your Ubuntu computer has been configured to use Nginx. The Nginx service may be controlled in the same manner as any other systemd unit.

Configure firewall

After setting up and starting Nginx on your server, you must ensure that your firewall is set up to permit traffic on the HTTP (80) and HTTPS (443) ports. You can do this, suppose you're running UFW, by activating the "Nginx Full" profile, which has rules for both ports:

sudo ufw allow 'Nginx Full'

To verify the status type:

sudo ufw status

The output will look something like the following:

Output

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

Test the Installation

Open http://YOUR_IP in your preferred browser to check out your new Nginx installation. You should see the standard Nginx landing page as shown in the picture below:

Standard Nginx Landing Page

Nginx Configuration File’s Structure and Best Practices

  • All Nginx configuration files are located in the /etc/nginx directory.
  • The main Nginx configuration file is /etc/nginx/nginx.conf.
  • It is advised to generate a different configuration file for each domain in order to make maintaining the Nginx setup simpler. The number of server block files is unlimited.
  • Block files for the Nginx server are kept in the /etc/nginx/sites-available directory. Except when they are linked to the /etc/nginx/sites-enabled directory, Nginx does not utilize the configuration files contained in this directory.
  • You must make a symlink (a pointer) from the configuration file sites in a sites-available directory to the sites-enabled directory in order to activate a server block.
  • It is advised to adhere to the accepted name practice. For instance, your configuration file should be titled /etc/nginx/sites-available/mydomain.com.conf if your domain name is mydomain.com.
  • Configuration snippets that may be used in the server block files can be found in the /etc/nginx/snippets directory. If your server blocks include repeating configuration segments, you may turn such segments into snippets by refactoring them into separate files.
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx directory. It is recommended to have a different access and error log files for each server block.

Your domain's document root directory may be adjusted to any place you like. Webroot may be found most often in:

  • /home/<user_name>/<site_name>
  • /var/www/<site_name>
  • /var/www/html/<site_name>
  • /opt/<site_name>

FAQs to Install Nginx on Ubuntu 20.04

Where is the Nginx configuration file located on Ubuntu 20.04?

The main Nginx configuration file is located at /etc/nginx/nginx.conf on Ubuntu 20.04.

How do I start, stop, or restart Nginx on Ubuntu 20.04?

You can start, stop, or restart Nginx using the following commands: sudo systemctl start nginx, sudo systemctl stop nginx, and sudo systemctl restart nginx.

How can I check if Nginx is running on Ubuntu 20.04?

Run the command sudo systemctl status nginx to check the status of Nginx. If it's running, you'll see the active (running) message.

How do I enable or disable Nginx at system startup on Ubuntu 20.04?

To enable Nginx to start automatically on system boot, use the command sudo systemctl enable nginx. Use sudo systemctl disable nginx to disable it.

Where can I find the default web root directory in Nginx on Ubuntu 20.04?

The default web root directory for Nginx on Ubuntu 20.04 is /var/www/html.

How can I test the Nginx configuration for syntax errors?

To check for syntax errors in the Nginx configuration, run the command sudo nginx -t. It will validate the configuration and display any errors.

How do I open the Nginx default page in a web browser on Ubuntu 20.04?

Open a web browser and enter http://localhost or http://your_server_ip_address to access the default Nginx page.

Can I use Nginx to serve multiple websites on Ubuntu 20.04?

Yes, Nginx supports hosting multiple websites on a single server. You can create separate server blocks in the Nginx configuration to define different websites with unique configurations.

Conclusion

Here, we discussed how to set up Nginx on Ubuntu 20.04. Nginx is now available for use as a web or proxy server, and you can start deploying your apps.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.