How to create virtual hosts on Apache (httpd) on CentOs 7

Published: August 20, 2017 (Updated: Aug 20, 2017)

Enjoying this content? Subscribe to the Channel!

Host Multiple Websites on One Server: A Step-by-Step Guide to Apache Virtual Hosts

Welcome back to Darren’s Tech Tutorials!

If you’re running a web server, efficiency is key. Why dedicate an entire virtual machine or physical server to just one website when you can run dozens? That’s where Apache Virtual Hosts come in.

Virtual hosting is a powerful feature that allows a single server to host multiple domains, each with its own unique configuration, document root, and logging. Whether you are setting up a complex testing environment or hosting several client sites, mastering Virtual Hosts is a crucial skill for any system administrator.

Ready to unlock the true potential of your Apache server? Let’s dive into the setup!


Essential Prerequisites for Virtual Hosting

Before we start configuring Apache, we need to ensure two critical components are in place. These steps are often overlooked but are essential for a working setup:

  1. Apache Installation: If you haven’t installed Apache on your CentOS or RHEL server yet, you’ll need to do that first.

    • Need help? Reference our installation tutorial here: [https://youtu.be/rs0tCdkd5Bk]
  2. DNS Configuration: For a real-world scenario, your domains must correctly point to your server’s IP address. If you are just setting up a local testing environment, you must manually edit your local machine’s DNS (typically the /etc/hosts file on Linux/Mac or C:\Windows\System32\drivers\etc\hosts on Windows) or configure local DNS on your server.

    • Setting up local DNS? Check out this video: [https://youtu.be/g0LeE8NFipM]

Step 1: Prepare the Document Roots

Every virtual host requires its own unique document root—the directory where its website files will reside. It’s best practice to keep these separate and organized.

For this example, let’s assume we are setting up two domains: site1.example.com and site2.example.com.

We will create the directories and set the necessary ownership (assuming apache is your web server user):

# Create the root directories for the sites
sudo mkdir -p /var/www/html/site1/public_html
sudo mkdir -p /var/www/html/site2/public_html

# Create directories for logs (optional but highly recommended)
sudo mkdir -p /var/www/html/site1/logs
sudo mkdir -p /var/www/html/site2/logs

# Set ownership to the Apache user (often 'apache' on CentOS/RHEL)
sudo chown -R apache:apache /var/www/html/site1
sudo chown -R apache:apache /var/www/html/site2

Pro-Tip: To quickly test your setup, create a simple index.html file inside each public_html folder (e.g., /var/www/html/site1/public_html/index.html) containing the site’s name. This confirms which virtual host is being served later.

Step 2: Create the Virtual Host Configuration Files

On CentOS/RHEL systems, the best way to manage individual site configurations is by creating separate .conf files inside the /etc/httpd/conf.d/ directory.

Configuration for site1.example.com

Create the configuration file: sudo nano /etc/httpd/conf.d/site1.conf

Paste the following structure. Pay close attention to the DocumentRoot and ServerName directives:

<VirtualHost *:80>
    # The primary domain name
    ServerName site1.example.com

    # Optional: Other domain names or aliases that should resolve to this site
    ServerAlias www.site1.example.com

    # The location of the website files
    DocumentRoot /var/www/html/site1/public_html

    # Standard log file locations
    ErrorLog /var/www/html/site1/logs/error.log
    CustomLog /var/www/html/site1/logs/access.log combined

    <Directory /var/www/html/site1/public_html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Configuration for site2.example.com

Create the second configuration file: sudo nano /etc/httpd/conf.d/site2.conf

<VirtualHost *:80>
    ServerName site2.example.com
    ServerAlias www.site2.example.com
    DocumentRoot /var/www/html/site2/public_html
    ErrorLog /var/www/html/site2/logs/error.log
    CustomLog /var/www/html/site2/logs/access.log combined

    <Directory /var/www/html/site2/public_html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Step 3: Test and Apply the Changes

Once both configuration files are saved, we must verify the syntax and then restart the Apache service to load the new settings.

3.1 Check Configuration Syntax

Always run a configuration test before restarting to catch any typing errors:

sudo httpd -t

You should see the message: Syntax OK. If you get an error, Apache will usually tell you exactly which line and file caused the issue. Fix the error before proceeding!

3.2 Restart Apache

If the syntax is OK, restart the service to make your new Virtual Hosts live:

sudo systemctl restart httpd

3.3 Firewall Configuration (Quick Check)

Ensure that your server’s firewall (if enabled) is allowing traffic on port 80 (HTTP) or port 443 (HTTPS):

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

Step 4: Verify Functionality

If your DNS settings are configured correctly (either locally via your hosts file or globally), you can now test the setup in your web browser:

  1. Navigate to http://site1.example.com
  2. Navigate to http://site2.example.com

Each URL should load the unique content you placed in the respective site’s public_html directory, proving that your single server is successfully hosting multiple distinct websites!


Summary and Next Steps

Congratulations! You’ve successfully configured Apache Virtual Hosts, dramatically increasing the capability and efficiency of your web server. This foundational skill allows you to scale your hosting environment cleanly and securely.

If you found this guide helpful, be sure to hit that Like button and Subscribe to Darren’s Tech Tutorials for more clear, practical technical breakdowns.

Want to keep all your Linux commands handy? Don’t forget to grab your free copy of our Linux Cheat Sheet!

➡️ Free Linux Cheat Sheet Download: http://eepurl.com/dkRNM9

All commands referenced in this guide can also be found on the channel’s website here: https://darrenoneill.eu/?p=306

Happy hosting!