Apache VirtualHost CentOS

Legacy Content

This is Legacy Content. For up-to-date tutorials and information, see our current knowledge base.

Unmanaged Hosting Tutorial

We have made these tutorials available for your convenience. However, this tutorial cannot be completed on any of the current hosting packages from Pair Networks.



To host multiple sites on the same server, VirtualHost entries can be used in Apache to route the Web traffic to the proper files.

In this tutorial, we'll assume that the domain we are trying to add is
named "example.com". You'll want to replace "example.com" with the name of the domain that you are actually configuring as you run the steps.

First, a new directory for the domain should be created. It is recommended to create separate directories per domain under the main /var/www/ document
root. That way, the files for sites won't reside under one another and potentially interfere with the settings for other sites. For this example, we'll create a sub-directory with the same name as the domain (the name can be anything you like so long as the Apache configuration is set the same).
The directory should also be changed to the username that you wish to administer it as we'll be using "centos" as the username:

sudo mkdir /var/www/example.com/

sudo chown centos /var/www/example.com/

We'll now place a file in that directory to use for testing later:

echo "Hello world from example.com" > /var/www/example.com/index.html

We can now proceed with adding the VirtualHost entry into Apache to associate the example.com name with the content above. To do so, create a file at:


In that file, place the following:

<VirtualHost *:80>

ServerName www.example.com
ServerAlias example.com

ServerAdmin user@example.com

DocumentRoot /var/www/example.com/


Using both the ServerName and ServerAlias lines will allow the server to respond to requests for either http://example.com or
http://www.example.com. The ServerAdmin line should have the email address entered that you wish to use for the default Webmaster address for the domain.

Apache now needs to be restarted to update to the new configuration. That can be done with:

sudo service httpd reload

The new domain can now be tested in your browser. Going to
http://www.example.com should load the "Hello world from example.com" message entered into the index.html file earlier.

Additional Documentation

For more advanced setup of Apache, the following resources can be consulted.

Apache configuration documentation:


The Apache Getting Started Guide:


Updated on March 3, 2020

Was this article helpful?

Related Articles