How to install Nginx on Ubuntu 22.04?

Overview

Nginx, one of the most popular web servers worldwide, is the host of some of the biggest and busiest websites on the internet. It's a versatile choice that can function as a web server or reverse proxy. This tutorial will show you how to set up Nginx on your Ubuntu 22.04 system.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • Ubuntu 22.04-equipped system

  • A regular and a non-root user with Sudo privileges

  • Internet connection

Get Started

Step 1: Installing Nginx

Since Nginx is a part of the default Ubuntu repository, it will be installed via the apt packaging method.

In order to get access to the most recent package listings, as this will be the first engagement with the apt system in this session, we will update our local package index. Nginx can then be installed after that:

sudo apt update
sudo apt install nginx

When asked to confirm installation, press Y. Press ENTER to accept the defaults and carry on if you are required to restart any services. Nginx and any necessary dependencies will be installed on your server by apt.

Step 2: Adjusting the Firewall

The firewall program needs to be set up to permit access to the service before Nginx can be tested. Upon installation, Nginx registers with ufw as a service, making it simple to provide Nginx access.

Type the following to see a list of the configurations that ufw can work with for applications:

sudo ufw app list

Nginx Full, Nginx HTTP, and Nginx HTTPS are the three profiles that are available, as shown by the output.

It is advised that you activate the most robust profile that still permits the traffic configured. For the time being, we only need to allow port 80 traffic.

You can activate this by keying in the:

sudo ufw allow 'Nginx HTTP'

Step 3: Checking the Web Server

After the installation is complete, Ubuntu 22.04 launches Nginx, and the web server should be operational by now.

By typing, we can verify that the service is active with the systemd init system.

systemctl status nginx

This demonstrates that the service has launched successfully. However, the best approach to test this is to actually request a page via Nginx.

By going to your server's IP address, you may visit the default Nginx landing page to verify that the program is functioning as intended. The icanhazip.com tool can be used to determine your server's IP address if you are unsure about it. It will provide you with your public IP address as it was acquired from another internet location:

curl -4 icanhazip.com

Once you know your server's IP address, key it into the address bar of your browser as follows: http://your_server_IP

You should be able to view the standard Nginx landing page:

If you are viewing this page, your server is functioning properly and is prepared for management.

Step 4: Controlling the Nginx Service

Nginx's behavior can be modified. Nginx can be started, stopped, enabled, or disabled during boot with the following set of commands.

To start the Nginx service if it isn't already operating, enter the command below:

sudo systemctl start nginx

Enter the following to make Nginx start loading when the machine boots:

sudo systemctl enable nginx

Enter the following to terminate the Nginx service:

sudo systemctl stop nginx

When the system boots, avoid Nginx loading by doing the following:

sudo systemctl disable nginx

The Nginx service can be reloaded in order to implement configuration changes:

sudo systemctl reload nginx

To force Nginx to restart:

sudo systemctl restart nginx

Conclusion

After installing your Nginx on your system, you now have a variety of options for the content types you will serve and the technologies you will employ to give users a richer experience.

Last updated