How to install Nginx in Ubuntu 22.04

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to install Nginx in Ubuntu 22.04.

Nginx is a free and open source web server, that allows you to do a lot more than just serving content on your website.

Nginx for example, can be used also as a reverse proxy server, or as a load balancer that allows you to distribute the traffic to multiple servers so that the traffic is not handled by one server only which would cause the server to have issues or even go offline.

In this tutorial, we'll be installing Nginx in Ubuntu 22.04, by using two different methods.

Installing Nginx in Ubuntu 22.04

In the first method, we'll be installing Nginx via the default repository in Ubuntu.

In the second method, we'll be installing Nginx via the official Nginx repository.

Method 1: Install Nginx via Ubuntu repository

Installing Nginx in Ubuntu is quite simple, and you can do so by downloading the nginx package that can be found in the default Ubuntu repositories.

To install Nginx, you can use apt.

sudo apt install nginx

Method 2: Install Nginx via official Nginx repository

Step 1 - Install the prerequisites

In order to be able to install Nginx in your Ubuntu machine, first you must install these packages in your system.

These packages will be used to download and setup Nginx in your system, so make sure to install them first, before proceeding to the next step.

To install these packages, simply run the command below.

sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring

Step 2 - Add Nginx GPG Key

Once you have installed all the required packages in the first step, proceed to add Nginx GPG key in your Ubuntu machine.

To download the GPG key of Nginx, you can easily do so by running the command below.

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

Once nginx GPG key is downloaded, proceed to verify it.

gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

And you should see the following output in your terminal.

The fingerprint that you need to look for is 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62, so if you get the same fingerprint in the output, you can proceed to install Nginx.

pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
      573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid                      nginx signing key <signing-key@nginx.com>

Step 3 - Setup Nginx repository

Now that you verified authenticity of Nginx GPG key, you need to setup Nginx repository in your system.

Creating Nginx repository in Ubuntu is quite simple, and it can be done by running the command below.

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

Step 4 - Update package index

Next, refresh the package index so that Nginx repository gets added in the repository database.

To do so, simply run the command below.

sudo apt update

Step 5 - Install Nginx

Now you should be able install Nginx in your Ubuntu machine.

To do so, simply run the command below.

sudo apt install nginx

Step 6 - Verify Nginx Installation

Before using Nginx, we'd recommend checking that Nginx was installed correctly and that you are able to run in.

nginx -v

Nginx commands that you should know

How to check Nginx status

To check the state of Nginx process, you can do so by using the status command.

sudo systemctl status nginx

How to start Nginx

To start Nginx process in Ubuntu, you can use the command below.

sudo systemctl start nginx

How to restart Nginx

If you need to restart your Nginx webserver, you can easily do so by using the restart command.

sudo systemctl restart nginx

How to enable Nginx

To make sure that Nginx can start automatically when you boot up your server, you need to enable Nginx process.

To enable Nginx process, simply run the command below.

sudo systemctl enable nginx

How to stop Nginx

If you need to stop your Nginx webserver, then simply run the following.

sudo systemctl stop nginx

Conclusion

In this tutorial, you learned how to install Nginx in Ubuntu 22.04, via the official Nginx repository.