Back to Blog

How to Host Multiple Websites on One Server

How to Host Multiple Websites on One Server
Command of the Day
sudo ln -s /etc/nginx/sites-available/site1.com /etc/nginx/sites-enabled/

What it does: Creates a symbolic link to activate an Nginx virtual host configuration without copying or duplicating configuration files.

You don't need a separate VPS for every website you build. A single cloud server can effortlessly host dozens of distinct websites, blogs, APIs, and client projects simultaneously using Virtual Hosting (referred to as Server Blocks in Nginx and VirtualHosts in Apache).

In this tutorial, we will configure Nginx on a RivinCloud VPS to host two separate domains—siteone.com and sitetwo.com—on a single server IP address.


1. How Virtual Hosting Works

When a visitor types siteone.com into their browser:

  1. DNS resolves siteone.com to your VPS IP address.
  2. The browser sends an HTTP request containing the Host: siteone.com header.
  3. Nginx inspects the Host header and matches it against the server_name directive in your configuration files, routing the visitor to the corresponding directory.

2. Step 1: Create Separate Directory Structures

Organize your websites cleanly under /var/www/:

# Create directory structure for site 1
sudo mkdir -p /var/www/siteone.com/html

# Create directory structure for site 2
sudo mkdir -p /var/www/sitetwo.com/html

# Assign ownership to your standard user
sudo chown -R $USER:$USER /var/www/siteone.com/html
sudo chown -R $USER:$USER /var/www/sitetwo.com/html

# Set directory permissions
sudo chmod -R 755 /var/www

3. Step 2: Create Demo Pages for Each Website

Sample Page for Site 1:

nano /var/www/siteone.com/html/index.html
<!DOCTYPE html>
<html>
<head><title>Site One</title></head>
<body style="font-family: sans-serif; text-align: center; padding: 40px; background: #1e293b; color: #fff;">
    <h1>🎉 Welcome to Site One!</h1>
    <p>Hosted on RivinCloud VPS.</p>
</body>
</html>

Sample Page for Site 2:

nano /var/www/sitetwo.com/html/index.html
<!DOCTYPE html>
<html>
<head><title>Site Two</title></head>
<body style="font-family: sans-serif; text-align: center; padding: 40px; background: #0f172a; color: #38bdf8;">
    <h1>🚀 Welcome to Site Two!</h1>
    <p>Running independently on the same server.</p>
</body>
</html>

4. Step 3: Create Nginx Server Blocks

Configuration for Site 1:

sudo nano /etc/nginx/sites-available/siteone.com
server {
    listen 80;
    listen [::]:80;

    server_name siteone.com www.siteone.com;
    root /var/www/siteone.com/html;
    index index.html index.php;

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

    access_log /var/log/nginx/siteone.com_access.log;
    error_log /var/log/nginx/siteone.com_error.log;
}

Configuration for Site 2:

sudo nano /etc/nginx/sites-available/sitetwo.com
server {
    listen 80;
    listen [::]:80;

    server_name sitetwo.com www.sitetwo.com;
    root /var/www/sitetwo.com/html;
    index index.html index.php;

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

    access_log /var/log/nginx/sitetwo.com_access.log;
    error_log /var/log/nginx/sitetwo.com_error.log;
}

5. Step 4: Enable Both Sites & Test

Enable both virtual host configurations by symlinking them into sites-enabled:

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

Test configuration syntax and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

6. Step 5: Issue Individual SSL Certificates

Certbot seamlessly manages multiple SSL certificates on the same server using SNI (Server Name Indication):

sudo certbot --nginx -d siteone.com -d www.siteone.com
sudo certbot --nginx -d sitetwo.com -d www.sitetwo.com

Common Mistakes

  • Conflicting default_server directives: Only ONE server block per port should have the default_server flag.
  • Server names hash bucket memory limit: If you host dozens of long domain names, Nginx might throw could not build server_names_hash. Fix by uncommenting server_names_hash_bucket_size 64; in /etc/nginx/nginx.conf.

Troubleshooting

  • Both domains load the same website: This indicates a typo in server_name or that DNS for one domain is pointing to a default fallback block. Check with nginx -T | grep server_name.

Best Practices

  • Maintain distinct access and error logs for every domain to isolate troubleshooting.
  • Monitor server RAM and CPU as you add more websites using htop.

Frequently Asked Questions

How many websites can a 4GB RAM VPS host?

A 4GB RAM VPS with Nginx and PHP-FPM can comfortably host 20 to 50 low-to-medium traffic websites when caching is properly implemented!


Conclusion

Virtual hosting unlocks the true financial and computational efficiency of cloud computing. Host multiple domains with complete isolation on your RivinCloud VPS!