1. What is a Virtual Host? Why Use It?

On a Linux server, a single physical server can run multiple websites simultaneously using “virtual host” technology. Each website acts like an independent “small server” to provide services externally. The benefit is saving server resources (no need to purchase a separate server for each website). This is ideal for individual developers, small teams, or startup projects to quickly build websites.

Virtual hosts are mainly divided into two types: domain-based (most common) and IP-based (less common). This article focuses on domain-based virtual host configuration, suitable for scenarios with a domain name (or local testing environments).

2. Prerequisite: Install the Apache Server

First, install Apache (web server software) on your Linux server. Installation commands vary by Linux distribution. Below are steps for common systems:

CentOS/RHEL System

# Install Apache
sudo yum install httpd -y  

# Start Apache and set it to start on boot
sudo systemctl start httpd  
sudo systemctl enable httpd  

# Check status (to confirm the service is running)
sudo systemctl status httpd  

Ubuntu/Debian System

# Update package list and install Apache
sudo apt update  
sudo apt install apache2 -y  

# Start Apache and set it to start on boot
sudo systemctl start apache2  
sudo systemctl enable apache2  

# Check status
sudo systemctl status apache2  

Verify Installation Success

After installation, open a browser and visit the server’s IP address (e.g., http://192.168.1.100). If you see the default Apache page, the installation is successful.

3. Prepare Website Files and Directories

Virtual hosts require independent directories to store website files for each site. Using two example websites (site1.com and site2.com):

# Create root directories for site1 and site2
sudo mkdir -p /var/www/site1/public  
sudo mkdir -p /var/www/site2/public  

# Create a simple test homepage for site1
echo "Welcome to Site1" | sudo tee /var/www/site1/public/index.html  

# Create a homepage for site2
echo "Welcome to Site2" | sudo tee /var/www/site2/public/index.html  

4. Configure Virtual Hosts (Core Step)

Apache manages virtual hosts through configuration files. For Ubuntu, the configuration files are in /etc/apache2/sites-available/; for CentOS, they are in /etc/httpd/conf.d/. The following uses Ubuntu as an example (steps are generalizable).

Step 1: Create Virtual Host Configuration Files

Navigate to the Apache configuration directory and create separate files for each website (e.g., site1.conf and site2.conf):

# Enter Apache configuration directory
cd /etc/apache2/sites-available  

# Create site1 configuration file
sudo nano site1.conf  

# Create site2 configuration file
sudo nano site2.conf  

Step 2: Configure site1 Virtual Host

In site1.conf, add the following content (replace domain and directory paths with actual values):

<VirtualHost *:80>
    # Website admin email (customizable)
    ServerAdmin webmaster@site1.com  

    # Primary domain name (required)
    ServerName www.site1.com  
    # Support domain without www (optional, e.g., site1.com)
    ServerAlias site1.com  

    # Root directory for website files (the directory created earlier)
    DocumentRoot /var/www/site1/public  

    # Directory permissions (allow access to website files)
    <Directory /var/www/site1/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Paths for error logs and access logs
    ErrorLog ${APACHE_LOG_DIR}/site1_error.log
    CustomLog ${APACHE_LOG_DIR}/site1_access.log combined
</VirtualHost>

Step 3: Configure site2 Virtual Host

In site2.conf, repeat the above steps, only modifying the domain name and directory path:

<VirtualHost *:80>
    ServerAdmin webmaster@site2.com
    ServerName www.site2.com
    ServerAlias site2.com
    DocumentRoot /var/www/site2/public

    <Directory /var/www/site2/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/site2_error.log
    CustomLog ${APACHE_LOG_DIR}/site2_access.log combined
</VirtualHost>

Step 4: Enable Virtual Host Configuration

Ubuntu requires manual enabling of configuration files (CentOS restarts directly):

# Enable site1 and site2 configurations
sudo a2ensite site1.conf  
sudo a2ensite site2.conf  

# Check for configuration errors (to avoid syntax issues)
sudo apache2ctl configtest  

# Restart Apache to apply changes
sudo systemctl restart apache2  

5. Test Virtual Hosts (Local/Public Network Access)

After configuration, verify that both websites can be accessed successfully.

Local Testing (Without a Real Domain)

If you don’t have a domain name, simulate domain access by modifying the local hosts file:

  • Windows System: Open C:\Windows\System32\drivers\etc\hosts and add:
  127.0.0.1 www.site1.com
  127.0.0.1 www.site2.com
  • Linux/Mac System: Open the terminal and run:
  sudo nano /etc/hosts  
  # Add the following line:
  127.0.0.1 www.site1.com www.site2.com

Save and exit. Then visit http://www.site1.com and http://www.site2.com in a browser to check if the test pages display correctly.

Public Network Testing (With a Real Domain)

If you have a domain name, point its DNS record (e.g., A record) to the server’s IP address. Then visit http://www.site1.com in a browser to access the site.

6. Common Issues and Solutions

  • 403 Forbidden Error: Usually due to insufficient directory permissions.
    Run: sudo chown -R www-data:www-data /var/www/site1/public (Ubuntu) or sudo chown -R apache:apache /var/www/site1/public (CentOS).

  • Apache Fails to Start: Check configuration syntax errors:
    Run sudo apache2ctl configtest (Ubuntu) or sudo httpd -t (CentOS).

  • Domain Name Not Accessible: Verify DNS resolution with ping www.site1.com, or confirm the hosts file is correctly configured.

7. Summary

You have now completed setting up multi-site hosting on an Apache server:
1. Install and start Apache;
2. Create independent website directories and home page files;
3. Configure domain-based virtual hosts;
4. Test access via local hosts or public domain names.

The core of virtual hosts is isolated configuration, where each website’s settings do not interfere with others, making it easy to scale the number of websites. For further optimization, you can add SSL certificates (HTTPS), reverse proxies, etc., to enhance server performance.

Xiaoye