Effortless Nginx Installation on Ubuntu 20.04: A Developer's Guide

How to Install Nginx on Ubuntu 20.04

Nginx is a popular web server known for its efficiency and high performance. In this tutorial, we'll walk you through the process of installing Nginx on an Ubuntu 20.04 server. This guide is designed for web developers and individuals interested in setting up a web server for their projects.

Prerequisites

Before you begin, ensure you have the following:

  • An Ubuntu 20.04 server instance.

  • A non-root user with sudo privileges.

Step 1: Updating the Server

Keep your server software up to date with the following commands:

sudo apt update
sudo apt upgrade -y

Step 2: Installing Nginx

Install Nginx using the package manager:

sudo apt install nginx -y

Step 3: Managing the Nginx Service

Manage the Nginx service with these commands:

sudo systemctl start nginx      # Start Nginx
sudo systemctl stop nginx       # Stop Nginx
sudo systemctl restart nginx    # Restart Nginx
sudo systemctl enable nginx     # Enable Nginx to start on boot
sudo systemctl disable nginx    # Disable Nginx from starting on boot

Step 4: Adjusting the Firewall

Configure the firewall to allow HTTP and HTTPS traffic:

sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
sudo ufw status                 # Check firewall status

Step 5: Testing the Nginx Installation

Open a web browser and enter your server's IP address or domain name. You should see the default Nginx landing page.

Step 6: Exploring Nginx Configuration Files

Nginx's main configuration file is located at /etc/nginx/nginx.conf. Open it using a text editor:

sudo nano /etc/nginx/nginx.conf

Step 7: Creating a Basic Virtual Host

Create a new virtual host configuration file for your domain:

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

Add a basic configuration like this:

server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        root /var/www/example.com;
        index index.html;
    }
}

Step 8: Enabling the Virtual Host

Create a symbolic link to enable the virtual host:

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

Step 9: Testing the Virtual Host

Edit your local hosts file to map your domain to your server's IP address:

sudo nano /etc/hosts

Add a line like this:

your_server_ip    example.com www.example.com

Conclusion

Congratulations! You've successfully installed and configured Nginx on your Ubuntu 20.04 server. This guide covered the essential steps, from installation to setting up a basic virtual host. Now you're ready to explore more advanced Nginx configurations to optimize your web projects.

Did you find this article valuable?

Support Mandeep Singh by becoming a sponsor. Any amount is appreciated!