How to Install Nginx on Ubuntu 22.04

January 18, 2024


Nginx (pronounced engine-x) is a powerful and widely used open-source web server and reverse proxy server. It is known for its high performance, scalability, and efficiency in handling concurrent connections.

This tutorial teaches you how to install Nginx on Ubuntu 22.04, manage Nginx processes, and set up server blocks. The text also explains Nginx's important files and processes.

How to install NGINX on Ubuntu 22.04


Install Nginx on Ubuntu 22.04

Understanding how to install and configure Nginx is essential for system administrators, web developers, and anyone managing web servers or web applications on Ubuntu.

The following steps elaborate on the installation process.

Step 1: Update Ubuntu Repositories

Nginx is, by default, available in the Ubuntu package index. Ensuring you get the latest Nginx version is important for the installation process. To get the latest version, first update the repository with this command:

sudo apt update
Terminal output for sudo apt update

Step 2: Install Nginx

After updating repositories, begin the Nginx installation. Run the following command to install the latest version:

sudo apt install nginx
sudo apt install nginx terminal output

The command installs Nginx and any required dependencies to the server.

Step 3: Configure the Firewall

Upon installation, Nginx automatically registers itself as a service with ufw (Uncomplicated Firewall), simplifying the process of granting access to Nginx.

Nevertheless, confirm the firewall permits incoming traffic to the Nginx service with the following command:

sudo ufw app list
Terminal output for sudo ufw app list

The output displays a list of available application profiles registered with the ufw on the system. These profiles have predefined rules that allow or deny traffic for specific services or applications. 

In this example, there are application profiles for:

  • CUPS. Stands for Common Unix Printing System. Contains rules that allow traffic related to printer services.
  • Nginx Full. Designed for the Nginx web server and allows both HTTP and HTTPS traffic. 
  • Nginx HTTP. Allows only HTTP traffic for the Nginx web server. 
  • Nginx HTTPS. Allows only HTTPS traffic for the Nginx web server. 
  • OpenSSH. Created for the OpenSSH server, which is used for secure remote access to the system. It allows SSH traffic.

After confirming the required application profiles are listed, use these profiles with the ufw command to enable or disable access to the associated services. For instance, you might allow access to the Nginx Full profile to permit HTTP and HTTPS traffic or allow only HTTP traffic using the Nginx HTTP profile.

However, it's recommended only to grant the minimum necessary permissions for the application to function correctly. Therefore, enable only the most restrictive profile, which in this case is Nginx HTTP.

To accomplish that, run the following:

sudo ufw allow 'Nginx HTTP'
Terminal output for sudo ufw allow 'Nginx HTTP'

Step 4: Verify the Change 

To ensure HTTP traffic is allowed, use the sudo ufw status command:

sudo ufw status
sudo ufw status terminal output

The output shows the service is active.

Step 5: Check Ngnix Status

Once the installation completes, Nginx starts up. Make sure the service is running:

sudo systemctl status nginx
systemctl status nginx terminal output

The output shows Nginx is active. However, in some cases, Nginx won't start by default, as shown in the example below.

systemctl status nginx inactive terminal output

If that is the case, start Nginx with:

sudo systemctl start nginx

The command has no output. Verify the change by rechecking the Nginx status.

Step 6: Access the Nginx Landing Page

Another way to verify the software is running correctly is to navigate to the server's IP address via a web browser

Access any web browser and type in:

http:// [your_ip_adress]

In our example, this is:
Nginx landing page

If successful, the browser shows the Nginx landing page.

Note: If you don't know how to find your IP address, check out how to do it with hostname or ifconfig commands.

Manage Nginx Process on Ubuntu

With the web server operational, users can run various commands to manage the Nginx process. Note that none of the following commands provide any output. Here are some examples:

1. To stop the web server, enter:

sudo systemctl stop nginx

2. When the server is not active, start it with:

sudo systemctl start nginx

3. To stop and then start the service again, execute:

sudo systemctl restart nginx

4. When making changes to the Nginx web server configuration, such as modifying settings in its configuration files, users often must apply those changes to the running instance of Nginx. Reloading Nginx allows users to do this without disrupting active connections to the server. To accomplish that, run the following:

sudo systemctl reload nginx

5. By default, Nginx starts automatically during server boot. To deactivate this behavior, run:

sudo systemctl disable nginx

6. To enable the service for automatic startup upon boot, execute:

sudo systemctl enable nginx

Set up Server Blocks

Server blocks in Nginx serve as containers for configuration details, allowing the hosting of multiple domains from a single server. Each server block represents a specific configuration for how the server should handle requests for a particular domain or IP address.

By default, Nginx on Ubuntu 22.04 has one enabled server block configured to serve documents from the directory at /var/www/html. While suitable for a single site, this setup becomes unmanageable when hosting multiple sites.

The following are the steps to set up server blocks in Nginx on Ubuntu.

Step 1: Create Directories

Establish a directory structure within the /var/www directory to systematically organize the website's content. This method provides the advantage of having a designated directory for each website, simplifying the web server file management and organization.

When you avoid modifying the /var/www directory directly, you ensure its preservation as the default directory.

For instance, create a sample directory,, using the mkdir command with the -p flag. Including the -p flag ensures the creation of the specified directory and any parent directories. Run the following command:

sudo mkdir -p /var/www/
Terminal output for sudo mkdir -p /var/www/

The command has no output. However, to verify the directory is created, use the ls command:

ls /var/www
Terminal output for ls /var/vwww

The output shows a newly created directory ( within /var/www. This directory organizes and stores the files associated with its respective website.

Step 2: Assign Permissions

Use the chown command to set directory ownership to the user who initiates the command. Execute the following command:

sudo chown -R $USER:$USER /var/www/
sudo chown -R $USER:$USER /var/www/ terminal output

The $USER environment variable adopts the current user username, establishing the ownership of the current user and their primary group. 

The chmod command changes the permissions of a file or directory. To confirm the owner possesses read, write, and execute permissions while allowing only read and execute permissions to groups and others, use the following command:

sudo chmod 750 /var/www/
sudo chmod 750 /var/www/ terminal output

The three digits represent permissions for the owner, group, and others:

  • 7. Represents the owner's permissions. In binary, 7 is 111, meaning read (4), write (2), and execute (1) permissions are granted.
  • 5. Represents the group's permissions. In binary, 5 is 101, meaning read and execute permissions are granted, but write permission is not.
  • 0. Represents others (everyone else) and indicates no permissions.

Use chown to grant the Nginx user the required permissions to access the website directory. The www-data is a common username associated with the web server process on many Linux systems. Assigning ownership to the web server user ensures it has the necessary permissions to access and serve the files.

Execute the following commands:

sudo chown -R www-data /var/www/
sudo chown -R www-data /var/www/ terminal output

Step 3: Create a Sample HTML Page

Create a sample index.html page using a text editor of choice. For instance, with Vim:

sudo vim /var/www/

Add the following sample HTML to the newly created file:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome to!</title>
        <p>Congratulations! You've successfully reached the server.</p>
        <p>Thank you for visiting!</p>

Save and exit the file.

Step 4: Configure the Server Block

Create a server block configuration file. Instead of modifying the default configuration file directly, make a new one. For instance,

sudo vim /etc/nginx/sites-available/

Next, paste the following code into the file:

server {
    listen 80;
    listen [::]:80;

    root /var/www/;
    index index.html index.htm index.nginx-debian.html;


    location / {
        try_files $uri $uri/ =404;

Save and exit the file.

Step 5: Create a Symbolic Link

Establish a symbolic link from its configuration file to the sites-enabled directory to activate the server block. Nginx references this directory during startup to determine which server blocks are to be enabled.

Execute the following command:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/terminal output

Step 6: Deactivate the Default Configuration

Nginx includes a configuration file by default. Deactivate it to prevent potential conflicts among configuration files. Use the following command:

sudo unlink /etc/nginx/sites-enabled/default
sudo unlink /etc/nginx/sites-enabled/defaultterminal output

Step 7: Test Nginx Service

There are two ways to test the Nginx service. The first one is :

sudo nginx -t
sudo nginx -t terminal output

The output confirms Nginx is working. The second way to verify this is to test the simple web page made earlier. Enter the following in the web browser:


For instance:

The example website is now in the place of the Nginx landing page.

Step 8: Restart the Nginx Service

If the test shows Nginx working properly, restart the service before using it further. Run the following:

sudo systemctl restart nginx
sudo systemctl restart nginx terminal output

The command has no output, but it restarts Nginx.

Important Nginx Files and Directories

Understanding the critical files and directories with Nginx configuration and related data is crucial for effective Nginx management.

Here are the key Nginx files and directories:

  • /etc/nginx/nginx.conf - The main Nginx configuration file contains directives for global settings, worker processes, error logging, and other fundamental configurations.
  • /etc/nginx/sites-available - The Nginx sites configuration directory holds individual configuration files for each server block defined on the server. These files define settings for various hosted websites or applications.
  • /etc/nginx/sites-enabled - The directory contains symbolic links to configuration files in the sites-available directory. Nginx uses this directory to enable or disable specific server blocks.
  • /var/www/html - The Nginx HTML root directory stores web files for the default server block. Nginx serves files from this location when accessed directly via the server's IP address.
  • /etc/nginx/modules-available - This directory manages installed Nginx modules, each typically having its configuration file stored here.
  • /var/log/nginx - Nginx logs directory stores log files, including access and error logs. These logs are essential for troubleshooting and monitoring the web server.
  • /run/ - Nginx PID File contains the Process ID (PID) of the main Nginx process. System tools and processes use this information to manage and control the Nginx process.


After reading this article, you now know how to install, set up, and manage Nginx on Ubuntu 22.04.

Next, learn how to install an SSL certificate on the Nginx server.

Was this article helpful?
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
Apache vs Nginx: Detailed Comparison
October 5, 2023

According to all relevant research, Apache and Nginx are two top web server choices in the world today. This article analyses how Apache, a process-based/threaded HTTP server, compares with event...
Read more
How to Install and Configure Nginx on Ubuntu 20.04
October 1, 2020

Nginx is an open-source application used for managing web servers, load balancing, and security. This tutorial offers a step-by-step guide for installing and configuring...
Read more
How to Secure Nginx with Let's Encrypt On Ubuntu 20.04 / 18.04
July 9, 2020

This guide shows you how to secure your Nginx with Let’s Encrypt that generates SSL/TLS certificates for your domain. In addition, you will learn how to enabled Certbot to...
Read more
How to Start, Stop, and Restart Nginx (systemctl & Nginx Commands)
May 20, 2020

Knowing how to start, stop and restart Nginx is essential for managing an Nginx web server. For example. whenever making any configuration...
Read more