How to Host Multiple Sites Using Nginx on a VPS

Hosting multiple websites on a single VPS using Nginx is a practical and efficient way to manage your web projects. In this guide, I’ll walk you through the steps to set up multiple sites on your VPS using Nginx, based on my experience and reliable sources.


Understanding the Basics

Nginx is a powerful web server that can handle multiple websites through a feature called “server blocks” (similar to virtual hosts in Apache). Each server block allows you to define settings for a specific domain, enabling you to host multiple sites on a single server.


Prerequisites

Before we begin, ensure you have the following:

  • A VPS with Ubuntu installed.
  • Nginx installed on your VPS.
  • Root or sudo access to your server.
  • Two domain names pointing to your server’s IP address.

Step-by-Step Guide

1. Update Your System

Start by updating your package list to ensure all packages are up to date:

sudo apt update

2. Install Nginx

If Nginx isn’t already installed, you can install it using:

sudo apt install nginx

3. Create Directory Structure for Each Website

For each website, create a separate directory to store its files. For example, for example1.com and example2.com🙁Webdock)

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

Set the appropriate permissions:(Medium)

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

Create a sample index.html for each site:(Webdock)

echo "<html><head><title>Welcome to example1.com</title></head><body><h1>Success! The example1.com server block is working!</h1></body></html>" | sudo tee /var/www/example1.com/html/index.html

echo "<html><head><title>Welcome to example2.com</title></head><body><h1>Success! The example2.com server block is working!</h1></body></html>" | sudo tee /var/www/example2.com/html/index.html

4. Create Server Block Configuration Files

Create a separate configuration file for each site in /etc/nginx/sites-available/:

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

Add the following configuration:

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

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

    server_name example1.com www.example1.com;

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

Repeat the process for example2.com🙁Stack Overflow)

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

Add the corresponding configuration:

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

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

    server_name example2.com www.example2.com;

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

5. Enable the Server Blocks

Create symbolic links in the sites-enabled directory to enable the configurations:(Liquid Web)

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

6. Test Nginx Configuration

Check for syntax errors in your Nginx configurations:(Webdock)

sudo nginx -t

If the test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx

7. Update DNS Records

Ensure that both example1.com and example2.com have DNS records pointing to your server’s IP address. This step is crucial for the domains to resolve correctly.(Liquid Web)


Benefits of Hosting Multiple Sites on One VPS

  • Cost Efficiency: Running multiple sites on a single VPS reduces hosting costs.
  • Resource Optimization: Efficiently utilize server resources by consolidating websites.
  • Simplified Management: Centralize the management of your websites, making updates and maintenance more straightforward.

Considerations and Best Practices

  • Security: Ensure each site is secured, especially if they handle sensitive data.
  • Performance: Monitor server performance to prevent any one site from consuming excessive resources.
  • Isolation: While hosting multiple sites on one VPS is efficient, be cautious. If one site is compromised, others may be at risk. Consider using containers or virtual environments for better isolation if needed.

Conclusion

Hosting multiple websites on a single VPS using Nginx is a practical solution for many developers and businesses. By following the steps outlined above, you can efficiently manage multiple sites, optimize resources, and reduce costs. Always ensure you follow best practices for security and performance to maintain a reliable hosting environment.


Leave a Reply