Home  »  ArticlesGuidesHow ToSoftwareTechnologyTools   »   How To Run Multiple Websites Using Nginx Webserver On Ubuntu 24.04 Noble Numbat

How To Run Multiple Websites Using Nginx Webserver On Ubuntu 24.04 Noble Numbat

Running multiple websites on a single Nginx web server is a common requirement for web administrators. Nginx uses server blocks to manage multiple websites on the same server. This guide will show you how to set up multiple websites on an Nginx web server running Ubuntu 24.04 Noble Numbat.

Step 1: Update Your Package List

Before setting up your server, ensure that your package list is up to date. Open your terminal and run:

$ sudo apt update

This command refreshes the package list to ensure you have the latest information on available packages.

Step 2: Install Nginx

If you haven’t installed Nginx yet, you can do so by running:

$ sudo apt install nginx

Step 3: Configure DNS for Each Domain

Make sure that each domain you plan to host is pointed to your server’s IP address. You can verify this with tools like dig:

$ dig yourdomain.com +short

Repeat this for each domain to ensure they all resolve to your server’s IP address.

Step 4: Set Up Directory Structure

Create a directory for each website you plan to host. For example, to set up directories for example1.com and example2.com, run:

$ sudo mkdir -p /var/www/example1.com/html
$ sudo mkdir -p /var/www/example2.com/html

Set the correct permissions for these directories:

$ sudo chown -R $USER:$USER /var/www/example1.com/html
$ sudo chown -R $USER:$USER /var/www/example2.com/html
$ sudo chmod -R 755 /var/www

Step 5: Create Sample Pages

Create a simple index.html file for each website to test the configuration. For example, for example1.com:

$ nano /var/www/example1.com/html/index.html

Add the following content:

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

Repeat the process for example2.com:

$ nano /var/www/example2.com/html/index.html

Add the following content:

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

Step 6: Configure Nginx Server Blocks

Create a server block configuration file for each website. For example1.com:

$ sudo nano /etc/nginx/sites-available/example1.com

Add the following configuration:

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

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

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

Repeat the process for example2.com:

$ sudo nano /etc/nginx/sites-available/example2.com

Add the following configuration:

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

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

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

Step 7: Enable the Server Blocks

Enable each server block by creating a symbolic link from the sites-available directory to the sites-enabled directory. For example1.com:

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

For example2.com:

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

Step 8: Test Nginx Configuration

Test the Nginx configuration for syntax errors:

$ sudo nginx -t

If the output indicates that the syntax is OK, reload Nginx to apply the changes:

$ sudo systemctl reload nginx

Step 9: Verify the Setup

Open your web browser and navigate to http://example1.com and http://example2.com. You should see the respective sample pages for each domain, confirming that both sites are being served correctly by Nginx.

Step 10: Secure Your Websites with SSL (Optional)

For better security, you should secure your websites with SSL certificates. You can use Certbot to obtain free SSL certificates from Let’s Encrypt. Install Certbot and the Nginx plugin:

$ sudo apt install certbot python3-certbot-nginx

Obtain and install certificates for each domain:

$ sudo certbot --nginx -d example1.com -d www.example1.com
$ sudo certbot --nginx -d example2.com -d www.example2.com

Certbot will automatically configure Nginx to use the SSL certificates and set up redirection from HTTP to HTTPS.

Conclusion

You have successfully configured Nginx to host multiple websites on a single server running Ubuntu 24.04 Noble Numbat. By following these steps, you can manage multiple domains efficiently and ensure your sites are secure and performant.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.