How to install Nginx on Ubuntu 18.04 / 20.04

How to install Nginx on Ubuntu 18.04 / 20.04

Nginx is a fast and mostly used web server. Nginx offers a variety of features such as Reverse proxy, load balancer, etc. As compared to Apache, Nginx is very fast, lightweight, and easy to scale the web server. With multiple virtual hosts, we can serve the contents of multiple websites. For more information about the Nginx, you can visit the official page of Nginx

In this tutorial, we will learn how we can install Nginx on Ubuntu 18.04 / 20.04.

Prerequisites

  • Server with Ubuntu 18.04 / 20.04
  • Root user or another user with Sudo privileges

Install Nginx Web Server

  • Update the repositories of the server to install the latest Nginx version.
sudo apt update -y
  • Run the following command to install the latest packages of Nginx.
sudo apt install nginx -y
  • Start the Nginx service with the following command.
sudo systemctl start Nginx
  • Check the status of the Nginx service.
sudo systemctl status Nginx
Checking the status of the Nginx service
  • To check the Nginx is working properly. Enter the server IP (localhost for the local system) on the browser.
Default Nginx Page
  • If you don’t know the server IP, then run the following command to check the server IP.
curl ifconfig.me

Manage Nginx Service

  • To enable the Nginx service
sudo systemctl enable nginx
  • To disable the Nginx service
sudo systemctl disable nginx
  • To start the Nginx service
sudo systemctl start nginx
  • To stop the Nginx service
sudo systemctl stop nginx
  • To check the status of the Nginx service
sudo systemctl status nginx

Allow ports from Firewall

If the server has an active firewall, then you need to allow the Nginx ports for the incoming traffic. But if the Firewall is inactive, then skip this step.

For Port 80

sudo ufw allow 'Nginx HTTP'

For Port 443

sudo ufw allow 'Nginx HTTPS'

Conclusion

In this tutorial, we have successfully configured the virtual host to server the website content. Now you can serve any type of content on the website.

If you guys have any queries or issues, let me know in the comments sections.