Hosting Multiple Websites on a VPS with Nginx: A Complete Tutorial

Ravi MehraRavi Mehra
2 min read

To host multiple websites or applications on a VPS using Nginx, you can create separate server blocks (virtual hosts) for each site. Here’s a step-by-step guide to setting this up:

1. Update and Install Nginx

First, ensure your VPS is up-to-date and that Nginx is installed.

sudo apt update
sudo apt upgrade
sudo apt install nginx

2. Configure DNS

Ensure each domain name points to your VPS's IP address by updating the DNS records at your domain registrar.

3. Create Directory Structure

Create directories for each site. For example:

sudo mkdir -p /var/www/site1.com/html
sudo mkdir -p /var/www/site2.com/html

sudo chown -R $USER:$USER /var/www/site1.com/html
sudo chown -R $USER:$USER /var/www/site2.com/html

sudo chmod -R 755 /var/www

4. Create Sample HTML Files

Create a simple HTML file for each site to test the setup.

echo "<html><body><h1>Welcome to Site1</h1></body></html>" > /var/www/site1.com/html/index.html
echo "<html><body><h1>Welcome to Site2</h1></body></html>" > /var/www/site2.com/html/index.html|

5. Create Nginx Server Blocks

Create server block configuration files for each site.

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

Add the following content to the site1.com configuration file:

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

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

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

Repeat the above steps for site2.com.

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

Add the following content to the site2.com configuration file:

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

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

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

6. Enable the Server Blocks

Create symbolic links to enable the new server blocks.

sudo ln -s /etc/nginx/sites-available/site1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site2.com /etc/nginx/sites-enabled/

7. Test and Restart Nginx

Test the Nginx configuration for syntax errors and then restart Nginx.

sudo nginx -t
sudo systemctl restart nginx

8. Firewall Configuration (Optional)

If you have a firewall enabled, allow HTTP and HTTPS traffic.

sudo ufw allow 'Nginx Full'

For HTTPS, you can use Certbot to obtain and install free SSL certificates.

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d site1.com -d www.site1.com
sudo certbot --nginx -d site2.com -d www.site2.com

Certbot will automatically configure SSL for your Nginx server blocks.

Conclusion

You should now have multiple websites running on your VPS, each served by Nginx with its own server block configuration. Ensure your DNS settings are correct and propagate, and you should be able to visit each site by its domain name.

0
Subscribe to my newsletter

Read articles from Ravi Mehra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ravi Mehra
Ravi Mehra

Hello! I'm a versatile professional with expertise in MERN stack development, WordPress customisation, graphic design, and server management. With a keen eye for design and proficiency in tools like Photoshop and Figma, I create engaging user experiences and stunning visuals. I excel in project management, ensuring timely and efficient delivery of projects. My background in server setup and management guarantees that web applications run smoothly and securely. I thrive on collaboration and innovation, always eager to take on new challenges and bring creative ideas to life. Let's connect and make something amazing together!