Read in 4 minutes

last updated 

How to Set Up Apache Virtual Hosts on CentOS 7

Apache Virtual Hosts allows multiple websites to run on one Web server. With virtual hosts, you can specify the site document root (the directory which contains the website files), create a separate security policy for each site, use different SSL certificates for each site and much more.

In this tutorial, we’ll provide a step by step instructions about how to set up Apache Virtual Hosts on a CentOS 7 server.


Make sure you met the following prerequisites before continuing with this tutorial:

Creating Directory Structure

DocumentRoot is the directory where the website files for a domain name are stored and served in response to requests. You can set the document root to any location you want, this example we will use the following directory structure:

│   └── public_html
│   └── public_html
│   └── public_html


Create the root directory for the domain using the mkdir command:

sudo mkdir -p /var/www/

For testing purposes we will create an index.html file inside the domain document root directory. Open your editor and create a HTML file with the following contents:

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Welcome to</title>
    <h1>Success! home page!</h1>

All commands are executed as sudo user, so the newly created files and directories are owned by the root user. To make sure there are no permission issues, change the ownership of the domain document root directory to the apache user :

sudo chown -R apache: /var/www/

Creating Virtual Host File

There are a few ways to set up a virtual host. You can either add all Virtual Host Directives in a single file or create a new configuration file for each Virtual Host Directive. Personally, I prefer the second approach because it is more maintainable.

By default, Apache is configured to load all configuration files that ends with .conf from the /etc/httpd/conf.d/ directory.

To create a virtual host for a specific website open your editor of choice and create the following basic Virtual Host configuration file:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/

    <Directory /var/www/>
        Options -Indexes +FollowSymLinks
        AllowOverride All

    ErrorLog /var/log/httpd/
    CustomLog /var/log/httpd/ combined
    • -Indexes: Prevents directory listings.
  • ErrorLog, CustomLog: Specifies the location for log files.

Edit the file according to your needs and save it.

It is important that the configuration file name ends with .conf. You can name the configuration file as you like but the best practice is to use the domain name as the name of the virtual host configuration file.

Test the configuration file syntax with:

sudo apachectl configtest

If there are no errors you will see the following output:

Syntax OK

To activate a newly created virtual host, restart the Apache service with:

sudo systemctl restart httpd

Verify that everything is working as expected, by opening


In this tutorial, we have shown you how to create an Apache virtual host configuration. You can repeat the steps we outlined above and create additional virtual hosts for all your domains.


This post is a part of the Install LAMP Stack on CentOS 7 series.
Other posts in this series:

How to Set Up Apache Virtual Hosts on CentOS 7