How to configure virtual hosts in HTTPD

Home > Search > How-to

Before web servers had virtual hosts, you would have had to had a physical server for each website that you wanted to produce.


Virtual hosts allow you to run multiple sites from a single physical server.


The HTTPD example index.html page is located at /usr/share/httpd/noindex/index.html. If you want to use the HTTPD example index.html page as a template for your site, copy the index.html to /var/www/html.

Be certain to use the cp (copy) command and not the mv (move) command. The mv command can create files with an improper SELinux context.

[root@server1 ~]# cp /usr/share/httpd/noindex/index.html /var/www/html


By default, the /etc/httpd/conf/httpd.conf file has the following. This markup is what tells HTTPD to serve content from the /var/www/html directory, and to serve index.html as the default document, when navigating to As an example, if your index.html file is in the /var/www/html directory, when requesting, HTTPD knows to get the index.html file from the /var/www/html directory. This is the ideal configuration if HTTPD will only be serving one website.


DocumentRoot "/var/www/html"

<IfModule dir_module>
    DirectoryIndex index.html



Virtual hosts

Create a unique directory for each site.

~]# mkdir /var/www/site1
~]# mkdir /var/www/site2


Add an index.html file for each site.

~]# touch /var/www/site1/index.html
~]# touch /var/www/site2/index.html


Add unqiue text to each index.html file.

~]# echo "Welcome to site1" > /var/www/site1/index.html
~]# echo "Welcome to site2" >/var/www/site2/index.html


In the /etc/httpd/conf/httpd.conf file, comment out or remove the ServerName and DocumentRoot lines. Also, add IncludeOptional sites-enabled/*.conf to the very end of the file.

# ServerName
# DocumentRoot "/var/www/html"
IncludeOptional sites-enabled/*.conf


Create the sites-enabled directory.

[root@server1 ~]# mkdir /etc/httpd/sites-enabled


Create a file named vhosts.conf in the sites-enabled directory.

[root@server1 ~]# touch /etc/httpd/sites-enabled/vhosts.conf


Add the following to /etc/httpd/sites-enabled/vhosts.conf file.

<VirtualHost *:80>
    DocumentRoot "/var/www/site1"

<VirtualHost *:80>
    DocumentRoot "/var/www/site2"


Restart HTTPD, and ensure HTTPD is active and running.

[root@server1 ~]# systemctl start httpd
[root@server1 ~]# systemctl status httpd


Configure your DNS server to resolve and to the IP address of your HTTPD server.

You should now be able to navigate to and, and get unique .html files.


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.