Have you ever wondered how hosting companies manage to run hundreds of different websites on a single physical server? Or perhaps you own a powerful VPS from providers like Vultr but feel like you are wasting resources by running just one simple blog?
If you are looking to optimize costs and manage multiple web projects simultaneously, the answer lies in Nginx Virtual Hosts.

As a System Engineer who has deployed thousands of servers, Five Coupon can assure you that mastering Virtual Hosts is a “survival skill” that transitions you from a casual user to a true System Administrator. This guide will walk you through configuring Nginx step-by-step to run multiple domains on a single Ubuntu or CentOS server, using real-world data and precise technical standards .
Let’s begin your journey to server optimization!
Prerequisites & Environment Setup
Before diving into the command line, we need to ensure you have the necessary “ingredients” for this tutorial. This guide is optimized for the following environments:
- Operating System: Ubuntu Server 18.04 (Ubuntu / Debian / Linux Mint) or CentOS 7/8 (RHEL / Fedora) .
- Infrastructure: You can practice locally using VirtualBox or VMWare, or ideally, use a Cloud VPS .
- Provider Recommendation: If you don’t have a server yet, you can refer to the famous Cloud VPS services provided by Vultr .
The Scenario: We will simulate configuring two separate websites on the same Nginx Web Server with the following details :
| Website | Domain Name | Document Root |
| Website 1 | fivecoupon-1.com | /var/www/ fivecoupon_1 |
| Website 2 | fivecoupon-2.com | /var/www/ fivecoupon_2 |
To get started, ensure that Nginx is already installed on your server. Installing Nginx is extremely simple and is the prerequisite for all subsequent configurations .
Step 1: Establish a Standard Directory Structure
A good System Admin knows how to organize files neatly. Instead of a cluttered root, we will create separate directories for each website. This makes managing access permissions and security much easier later on.
Open your terminal (SSH) and run the following command to create the source code directories for both websites simultaneously:
mkdir -p /var/www/fivecoupon{1,2} Engineer’s Note: The -p flag ensures parent directories are created if they don’t exist. The {1,2} structure is a Bash shell trick to quickly create both fivecoupon_1 and fivecoupon_2 folders in a single command line.
Step 2: Create Dummy Pages for Verification
To verify if the Virtual Host configuration is working correctly (meaning the correct domain displays the correct content), we need to create distinct index.html files.
Create the index file for Website 1: File path: /var/www/fivecoupon_1/index.html .
Content:
<html>  <head>  <title>fivecoupon.com</title>  </head>  <body>     <p>Website: fivecoupon-1.com</p>  </body> </html>
Create the index file for Website 2:
Similarly, create a file at /var/www/fivecoupon_2/index.html with distinguishing content :
<html>  <head>  <title>fivecoupon.com</title>  </head>  <body>     <p>Website: fivecoupon-2.com</p>  </body> </html>
Step 3: Configure Nginx Server Blocks (The Core)
This is the most critical part. Each website requires its own configuration file (often called a Server Block) so Nginx knows how to handle requests.
We will create these configuration files in the /etc/nginx/sites-available/ directory.
Configuration for Website 1
Create the file /etc/nginx/sites-available/fivecoupon-1.conf with the following content:
server {
 listen 80;
 listen [::]:80;
 server_name fivecoupon-1.com;
 root /var/www/fivecoupon_1;
 index index.html;
 location / {
         try_files $uri $uri/ =404;
 }
}
Configuration for Website 2
Next, create the file /etc/nginx/sites-available/fivecoupon-2.conf:
server {
 listen 80;
 listen [::]:80;
 server_name fivecoupon-2.com;
 root /var/www/fivecoupon_2;
 index index.html;
 location / {
         try_files $uri $uri/ =404;
 }
}
Decoding the Parameters
To ensure you truly understand what you are configuring, here is the detailed meaning of each parameter:
- listen 80: Tells Nginx to listen for connections on port 80 (HTTP Port) .
- listen [::]:80: Similar to the above but enables support for IPv6 addresses .
- server_name: This is the domain address. Nginx checks the Host header of the request against this name to decide which block to use .
- root: The path to the folder containing the website source code .
- index: The file that will be called first when the domain is accessed .
- location /: Defines how resources are returned on the server when a request is made to a URL .
Step 4: Enable Virtual Hosts (Symlink)
Although we have created the configuration files, Nginx on Ubuntu/Debian typically does not automatically read files in sites-available. We need to create a symbolic link to the sites-enabled directory.
Run the following two commands:
sudo ln -s /etc/nginx/sites-available/fivecoupon-1.conf /etc/nginx/sites-enabled/fivecoupon-1.conf
sudo ln -s /etc/nginx/sites-available/fivecoupon-2.conf /etc/nginx/sites-enabled/fivecoupon-2.conf
Pro Tip: Using symbolic links allows you to easily enable or disable a website without deleting the original configuration file. Simply removing the link in sites-enabled stops the site, while the original file remains safe in sites-available.
Step 5: Test and Reload Nginx
The final step is to apply the changes. However, before reloading, always check the syntax to ensure no typos will crash the entire web server.
Test the configuration:
sudo nginx -t
If the output shows syntax is ok and test is successful, you are ready to reload the service to apply the two config files:
sudo systemctl reload nginx
Conclusion
Congratulations! You have successfully completed the setup of Nginx Virtual Hosts to run two websites, fivecoupon-1.com and fivecoupon-2.com, in parallel on a single VPS .
Mastering this technique not only helps you significantly save on infrastructure costs but also provides excellent flexibility in managing and scaling your website systems. From now on, deploying a new landing page or project takes just a few minutes of configuration without costing a single extra cent in hosting fees.


