Logo

How to Install Nginx on Ubuntu 24.04

Mar 15, 2024

To install and configure Nginx on Ubuntu 24.04, follow these steps:

Step 1 : Ensure your system is up to date by running:

sudo apt update

Step 2 : Install Nginx using the following command:

sudo apt install nginx

Step 3 : Check available application profiles for ufw:

sudo ufw app list

Allow Nginx Full profile:

sudo ufw allow 'Nginx Full'

Step 4 : Check the status of Nginx to ensure it's running:

sudo systemctl status nginx

Step 5 : Create a directory for your website, for example, example.com, and add an index file:

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

- Add some content to the index.html file. For example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome to example.com</title>
</head>
<body>
    <h1>Welcome to example.com</h1>
    <p>This is the default landing page for your domain.</p>
</body>
</html>

Step 6 : Create a new server block configuration file for your domain:

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

Add configuration details for your domain. Here's a basic example:

server {
    listen 80;
    listen [::]:80;

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

    server_name example.com www.example.com;

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

Step 7 : Link the configuration file from sites-available to sites-enabled to enable the site:

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

Step 8 : Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 9 : Visit your domain (example.com) in a web browser to ensure Nginx is serving your website correctly.

Congratulations! You have successfully installed and configured Nginx on Ubuntu 24.04.

Recommended