Install and Configure Nginx on Ubuntu 24.04

By Raman Kumar

Updated on Aug 05, 2024

In this tutorial, we'll explain how to install and configure Nginx on Ubuntu 24.04. We'll host a website and install SSL certificate.

Nginx, pronounced "engine-x," is a high-performance web server and reverse proxy server that is widely used for serving static content, managing load balancing, and acting as a gateway for dynamic applications. It was initially developed by Igor Sysoev in 2004 to address the C10k problem, which involves handling 10,000 concurrent connections.

In addition to serving as a web server, Nginx can function as a reverse proxy server, forwarding client requests to backend servers while providing caching, load balancing, and security features. This makes it a popular choice for scaling web applications and improving their performance and reliability. Nginx supports a variety of protocols, including HTTP, HTTPS, SMTP, POP3, and IMAP, allowing it to handle diverse web and mail server tasks.

Nginx is highly configurable and can be extended through modules, enabling customization and optimization for specific use cases. It is often used in conjunction with other web technologies, such as PHP, Python, and Ruby on Rails, to create robust and scalable web applications.

Prerequisites

  • A Ubuntu 24.04 installed dedicated server or KVM VPS.
  • A root user access for normal user with sudo rights.
  • Basic Linux commands knowledge.

Install and Configure Nginx on Ubuntu

Step 1: Update and Upgrade the System

First, update your package index and upgrade your system packages to ensure you have the latest versions:

sudo apt update
sudo apt upgrade -y

Step 2: Install Nginx

Install Nginx using the package manager:

sudo apt install nginx -y

Start and enable Nginx service using following command:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 3: Adjust Firewall Settings

If you have a firewall enabled, allow traffic on HTTP (port 80) and HTTPS (port 443):

sudo ufw allow 'Nginx Full'

Step 4: Configure Nginx to Host a Website

Create a Directory for Your Website:

Create a directory to store your website files. For this example, we'll create a directory for a site called example.com. Replace example.com with your domain name:

sudo mkdir -p /var/www/example.com/html

Set Permissions:

Assign ownership of the directory to the web server user:

sudo chown -R www-data:www-data /var/www/example.com/html

Create a Sample HTML File:

Create a sample HTML file to test your configuration:

nano /var/www/example.com/html/index.html

Add the following content to the file:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Example.com!</title>
</head>
<body>
    <h1>Success! The example.com server block is working!</h1>
</body>
</html>

Create Nginx Server Block Configuration:

Create a new server block configuration file for your website:

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

Note: Replace exmaple.com with your domain name. Add the following content:

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

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

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

Save and exit the file.

Enable the Server Block:

Enable the server block by creating a symbolic link to the sites-enabled directory:

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

Test to make sure there are no syntax errors in your Nginx configuration:

sudo nginx -t

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Steps 5: Secure the Website with SSL

To secure your website with HTTPS, you can use Let's Encrypt to obtain a free SSL certificate. Follow these steps:

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Obtain an SSL Certificate:

sudo certbot --nginx -d example.com -d www.example.com

Certbot will automatically modify your Nginx configuration to use the SSL certificate. Verify the changes by testing your configuration:

Your website should now be accessible via HTTPS. Visit https://example.com to check it.

Conclusion

We've successfully seen how to install and configure Nginx on Ubuntu 24.04 and hosted a website. You also secured your website with an SSL certificate using Let's Encrypt.