Logo

How to Set Up Nginx Server Blocks on Ubuntu 24.04

Mar 15, 2024

To set up multiple websites on an Nginx server using server blocks on Ubuntu 24.04, follow these steps:

Step 1 : Ensure your system is up to date:

sudo apt update

Step 2 : Install Nginx:

sudo apt install nginx

Step 3 : Create a directory for your website:

sudo mkdir -p /var/www/example.devtutorial.io/html

Create an index.html file:

sudo nano /var/www/example.devtutorial.io/html/index.html

Add some content to the file and save it. 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.devtutorial.io</title>
</head>
<body>
    <h1>Welcome to example.devtutorial.io!</h1>
    <p>This is the default index page for your website. You can replace this content with your own.</p>
</body>
</html>

Step 4 : Adjust permissions to ensure Nginx can serve the content:

sudo chown -R www-data: /var/www/example.devtutorial.io
sudo chmod -R 755 /var/www/example.devtutorial.io

Step 5 : Create a new server block configuration file:

sudo nano /etc/nginx/sites-available/example.devtutorial.io

Configure the server block. Example:

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

    server_name example.devtutorial.io;

    root /var/www/example.devtutorial.io/html;
    index index.html;

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

Step 6 : Create a symbolic link to enable the configuration:

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

Step 7 : Check Nginx configuration for syntax errors:

sudo nginx -t

Step 8 : Restart Nginx to apply changes:

sudo systemctl restart nginx

Step 9 : Open a web browser and navigate to your domain (e.g., example.com) to verify that your website is serving correctly.

Congratulations! You have successfully set up Nginx server blocks on Ubuntu 24.04, allowing you to host multiple websites on a single server.

Recommended