Ugacomp

How to Configure Nginx to host multiple websites

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Configuring Nginx to host multiple websites involves setting up server blocks, also known as virtual hosts. This guide will walk you through the process of configuring Nginx to host multiple websites on a single server.

Prerequisites

Before starting, ensure that you have:

  • Nginx installed on your server.
  • Websites’ content available in separate directories.

Step 1: Create Website Directories

Create separate directories for each website you want to host. For example, let’s create directories for website1.com and website2.com.

sudo mkdir -p /var/www/website1.com/html
sudo mkdir -p /var/www/website2.com/html

Step 2: Assign Ownership

Assign ownership of the website directories to the www-data user, which is the default Nginx user.

sudo chown -R www-data:www-data /var/www/website1.com/html
sudo chown -R www-data:www-data /var/www/website2.com/html

Step 3: Create Sample HTML Pages

Create sample HTML pages for each website. Replace the content with your actual website content.

echo "Welcome to Website 1" | sudo tee /var/www/website1.com/html/index.html
echo "Welcome to Website 2" | sudo tee /var/www/website2.com/html/index.html

Step 4: Configure Nginx Server Blocks

Open the Nginx default configuration file for editing.

sudo nano /etc/nginx/nginx.conf

Within the http block, add references to the server block configuration files.

http {
    include /etc/nginx/sites-enabled/*;
    # other configurations...
}

Step 5: Create Server Block Configuration Files

Create separate configuration files for each website in the /etc/nginx/sites-available/ directory.

For website1.com:

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

Paste the following code snippet into the website1.com file you’ve just created

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

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

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

For website2.com:

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

Similarly, paste the following code snippet into website2.com file

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

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

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

Step 6: Create Symbolic Links

Create symbolic links to enable the server blocks.

sudo ln -s /etc/nginx/sites-available/website1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/website2.com /etc/nginx/sites-enabled/

Step 7: Test Configuration and Reload Nginx

Test the Nginx configuration for syntax errors.

sudo nginx -t

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

sudo systemctl reload nginx

Step 8: Configure DNS Records

Update your domain’s DNS records to point to your server’s IP address.

Now, you should be able to access website1.com and website2.com in your web browser.

Conclusion

Configuring Nginx to host multiple websites involves creating separate server blocks for each site. By following these steps, you can efficiently manage and serve multiple websites from a single Nginx server, providing a scalable and organized hosting solution.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.