Beginner's Guide to Install and Configure Nginx on Ubuntu 22.04: Step-by-Step Tutorial

Beginner's Guide to Install and Configure Nginx on Ubuntu 22.04: Step-by-Step Tutorial

Nginx is a popular web server used by many websites and web applications. In this tutorial, we'll walk you through the process of installing and configuring Nginx on Ubuntu 22.04. By the end of this guide, you'll have a working Nginx server that can serve web pages and handle incoming traffic.

Prerequisites

Before we get started, you'll need to make sure that you have the following:

  • A server running Ubuntu 22.04

    (Get free $100 on your Digitalocean account to deploy your first server)

  • A non-root user with sudo privileges

  • A basic understanding of the Linux command line

Installation Steps

Step 1: SSH into your server

In order to perform the installation and configuration steps we need to log in or the server via ssh. For login, you will be needing a terminal or PowerShell. So please open it. There are 2 methods that we can use to log in to the server via ssh.

  • Method 1: Via Password

    To log in using a password run this in your terminal/PowerShell

      ssh username@server_ip_address_here
    

    Then it will ask for your password please provide your password for the server.

  • Method 2: Via SSH Key

    If you have set up an ssh key instead of a password for login then you need to run this in your terminal/PowerShell.

      # This is only for the 1st time
      chmod 400 /path/to/the/private_key.pem
    
      # This is for every time
      ssh -i "/path/to/the/private_key.pem" username@server_ip_address_here
    

Note: If you use different port than 22 Then you need to use the -p flag after the ssh commands.

For example:

# In this example we have used port 2022 as the ssh port
ssh username@server_ip_address_here -p2022

Step 2: Update Ubuntu

Before we begin, it is essential to make sure that your Ubuntu system is up-to-date. This can be done using the following commands:

sudo apt update
sudo apt upgrade

The first command updates the package list, and the second command upgrades any outdated packages to their latest version.

Step 2: Install Nginx

Next, we'll install Nginx by running the following command:

sudo apt install nginx

Once the installation is complete, you can start Nginx by running the following command:

sudo systemctl start nginx

You can also check the status of Nginx by running the following command:

sudo systemctl status nginx

Configuration Steps

Step 1: Configure Firewall

It's a good idea to configure your firewall to allow traffic on the ports that Nginx uses. By default, Nginx uses port 80 for HTTP traffic and port 443 for HTTPS traffic.

If you are running a firewall on your server, you will need to configure it to allow traffic to your web server. By default, Ubuntu 22.04 comes with a firewall called UFW (Uncomplicated Firewall). You can check if UFW is running using the following command:

sudo ufw status

Attention!!!

Please allow your ssh port before enabling the UFW or else you will lose access of your server.

# For default port
sudo ufw allow 22

# For custom port
sudo ufw allow custom_port_number_here

If UFW is not running, you can enable it using the following command:

sudo ufw enable

Now to allow traffic on nginx ports, you can run the following commands:

sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'

Step 2: Create a Virtual Host

To serve web pages with Nginx, you'll need to create a virtual host. A virtual host is a configuration file that tells Nginx how to serve your website or web application.

To create a virtual host file, run the following command:

sudo nano /etc/nginx/sites-available/example.com

Replace example.com with your own domain name.

In the file, paste the following configuration:

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

    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.html;

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

This configuration tells Nginx to listen on port 80 and to serve web pages from the /var/www/example.com/html directory.

Once you've saved the configuration file, create a symbolic link from the sites-available directory to the sites-enabled directory by running the following command:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Finally, restart Nginx by running the following command:

sudo systemctl restart nginx

Conclusion

In this tutorial, I've shown you how to install and configure Nginx on Ubuntu 22.04. By following these steps, you should now have a working Nginx server that can serve web pages and handle incoming traffic. If you run into any issues, feel free to leave a comment and I'll try to help you.