How to Set Up Virtual Hosts for Multiple Domains

Virtual hosts allow you to host multiple websites on a single dedicated server by associating each domain name with a specific directory. This guide will walk you through setting up virtual hosts for multiple domains on your dedicated server from QuickServers.net.


Step 1: Update Your Server

Before starting, ensure your server is up to date.

  • For Ubuntu/Debian:

    sudo apt update && sudo apt upgrade -y  
    
  • For CentOS/RHEL:

    sudo yum update -y  
    

Step 2: Install the Web Server

Virtual hosts are configured in your web server software. If Apache is not already installed, follow these steps:

  • For Ubuntu/Debian:

    sudo apt install apache2 -y  
    
  • For CentOS/RHEL:

    sudo yum install httpd -y  
    

Start and enable Apache:

  • For Ubuntu/Debian:

    sudo systemctl start apache2  
    sudo systemctl enable apache2  
    
  • For CentOS/RHEL:

    sudo systemctl start httpd  
    sudo systemctl enable httpd  
    

Step 3: Create Directories for Each Domain

Each domain needs its own directory to store website files.

  • Create directories for your domains:

    sudo mkdir -p /var/www/example1.com  
    sudo mkdir -p /var/www/example2.com  
    
  • Assign ownership of these directories to your user:

    sudo chown -R $USER:$USER /var/www/example1.com  
    sudo chown -R $USER:$USER /var/www/example2.com  
    
  • Set proper permissions:

    sudo chmod -R 755 /var/www  
    
  • Add a test HTML file for each domain:

    echo "<h1>Welcome to example1.com!</h1>" | sudo tee /var/www/example1.com/index.html  
    echo "<h1>Welcome to example2.com!</h1>" | sudo tee /var/www/example2.com/index.html  
    

Step 4: Configure Virtual Host Files

Create a virtual host file for each domain.

  • For Ubuntu/Debian:

    1. Navigate to the sites-available directory:

      cd /etc/apache2/sites-available  
      
    2. Create configuration files for each domain:

      sudo nano example1.com.conf  
      sudo nano example2.com.conf  
      
    3. Add the following content to each file (replace example1.com with your actual domain name):

      <VirtualHost *:80>  
          ServerName example1.com  
          ServerAlias www.example1.com  
          DocumentRoot /var/www/example1.com  
          ErrorLog ${APACHE_LOG_DIR}/example1_error.log  
          CustomLog ${APACHE_LOG_DIR}/example1_access.log combined  
      </VirtualHost>  
      

    Repeat for example2.com.

  • For CentOS/RHEL:

    1. Navigate to the configuration directory:

      cd /etc/httpd/conf.d  
      
    2. Create configuration files for each domain:

      sudo nano example1.com.conf  
      sudo nano example2.com.conf  
      
    3. Add the same content as above to each file.


Step 5: Enable the Virtual Hosts

  • For Ubuntu/Debian:
    Enable the sites and reload Apache:

    sudo a2ensite example1.com.conf  
    sudo a2ensite example2.com.conf  
    sudo systemctl reload apache2  
    
  • For CentOS/RHEL:
    Reload Apache to apply the changes:

    sudo systemctl reload httpd  
    

Step 6: Test Your Virtual Hosts

  • Update your local machine's hosts file (optional for testing without DNS configuration):

    sudo nano /etc/hosts  
    

    Add the following lines:

    server-ip example1.com  
    server-ip example2.com  
    

    Replace server-ip with your dedicated server's IP address.

  • Open a web browser and visit http://example1.com and http://example2.com.

You should see the corresponding test pages for each domain.


Step 7: Configure DNS Records

To make your websites accessible to the public, configure DNS records for each domain with your domain registrar.

  • Create an A record for each domain, pointing to your server's IP address.
  • Wait for DNS propagation (this may take a few hours).

Step 8: Enable HTTPS (Optional but Recommended)

Secure your websites with SSL certificates. Use Let's Encrypt for free SSL certificates:

  • Install Certbot:

    sudo apt install certbot python3-certbot-apache -y  # Ubuntu/Debian  
    sudo yum install certbot python3-certbot-apache -y  # CentOS/RHEL  
    
  • Obtain certificates for your domains:

    sudo certbot --apache  
    
  • Verify that HTTPS works by visiting https://example1.com and https://example2.com.


By following these steps, you can efficiently host multiple domains on your dedicated server from QuickServers.net. If you encounter any issues, contact QuickServers.net support for assistance.

Was this answer helpful? 0 Users Found This Useful (0 Votes)