Hosting multiple websites on a single NGINX server is a common requirement for development, staging, and production environments. NGINX uses server blocks (virtual hosts) to serve different websites based on the requested domain name.

This guide explains how to configure multiple domains on a single Ubuntu server running NGINX.

Prerequisites

Before proceeding, ensure the following:

  • Ubuntu server with NGINX installed
  • DNS records for all domains pointing to the server IP address
  • LEMP stack (Linux, NGINX, MySQL/MariaDB, PHP)
  • Two or more domain names

Example Domains

  • domain1.tld
  • domain2.tld

Step 1: Create Website Directories

Create separate document root directories for each domain.

mkdir -p /var/www/domain1.tld/html
mkdir -p /var/www/domain2.tld/html

Step 2: Set Directory Ownership

Check which user NGINX is running as:

grep user /etc/nginx/nginx.conf

Example output:

user www-data;

Assign ownership of the website directories to the NGINX user:

chown -R www-data:www-data /var/www/domain1.tld
chown -R www-data:www-data /var/www/domain2.tld

Set appropriate permissions:

chmod -R 755 /var/www/domain1.tld
chmod -R 755 /var/www/domain2.tld

Step 3: Create NGINX Server Block Configuration

Navigate to the NGINX configuration directory:

cd /etc/nginx/sites-available

Create configuration files for each domain:

cp default domain1.tld
cp default domain2.tld

Step 4: Configure Domain 1

Edit the configuration file:

vi /etc/nginx/sites-available/domain1.tld

Add the following configuration:

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

    server_name domain1.tld www.domain1.tld;

    root /var/www/domain1.tld/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Step 5: Configure Domain 2

Create a similar configuration for the second domain:

vi /etc/nginx/sites-available/domain2.tld
server {
    listen 80;
    listen [::]:80;

    server_name domain2.tld www.domain2.tld;

    root /var/www/domain2.tld/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Note: Replace php-fpm.sock with the correct PHP-FPM socket for your PHP version (for example, php8.3-fpm.sock).


Step 6: Enable the Virtual Hosts

Create symbolic links from sites-available to sites-enabled.

ln -s /etc/nginx/sites-available/domain1.tld /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/domain2.tld /etc/nginx/sites-enabled/

Disable the default configuration if it is not required:

rm -f /etc/nginx/sites-enabled/default

Step 7: Verify NGINX Configuration

Test the NGINX configuration before applying changes:

nginx -t

Expected output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 8: Restart NGINX

Apply the new configuration:

systemctl restart nginx

Verify the service status:

systemctl status nginx

Step 9: Create Test Pages

Create a test page for each domain.

Domain 1

echo "<h1>Welcome to Domain 1</h1>" > /var/www/domain1.tld/html/index.html

Domain 2

echo "<h1>Welcome to Domain 2</h1>" > /var/www/domain2.tld/html/index.html

Step 10: Verify DNS Configuration

Ensure that both domains resolve to your server’s public IP address.

dig domain1.tld +short
dig domain2.tld +short

Both commands should return the server IP.


Conclusion

You have successfully configured multiple domains on a single NGINX server using virtual hosts (server blocks). Each domain now serves content from its own document root while sharing the same NGINX instance.

This approach is scalable and can be extended to host dozens or even hundreds of websites on a single server. For production environments, it is recommended to secure each domain with SSL certificates using Let’s Encrypt and configure automatic certificate renewal.

Leave a Reply