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:

You have a domain name pointing to your public server IP. We will use
You have Apache installed by following How To Install Apache on CentOS 7.
You are logged in as a user with sudo privileges.

Creating Directory Structure

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

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


Basically we are creating a separate directory for each domain we want to host on our server inside the /var/www directory. Within each of these directories, we will create a public_html directory that will store the domain website files.

Create the root directory for the domain

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:

Welcome to

Success! home page!


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 specific website open your editor of choice and create the following basic Virtual Host configuration file:

DocumentRoot /var/www/

Options -Indexes +FollowSymLinks
AllowOverride All

ErrorLog /var/log/httpd/
CustomLog /var/log/httpd/ combined


ServerName: The domain that should match for this virtual host configuration. This should be your domain name.
ServerAlias: All other domains that should matchfor this virtual host as well, such as the www subdomain.
DocumentRoot: The directory from which Apache will serve the domain files.
Options: This directive controls which server features are available in a specific directory.
-Indexes: Prevents directory listings.
FollowSymLinks: This option tells your web server to follow the symbolic links.
AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.
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 your 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.

Leave a Reply

Your email address will not be published. Required fields are marked *