How to configure virtual hosts in HTTPD

Home > Search > How-to
  by

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 www.emample.com. As an example, if your index.html file is in the /var/www/html directory, when requesting www.example.com, 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.

ServerName www.example.com

DocumentRoot "/var/www/html"

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

 

 


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 www.example.com
# 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>
    ServerName www.site1.com
    DocumentRoot "/var/www/site1"
</VirtualHost>

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

 

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 www.site1.com and www.site2.com to the IP address of your HTTPD server.

You should now be able to navigate to www.site1.com and www.site2.com, 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.




Comments