Virtual Hosting allows Apache Weberver to serve different content based on IP Address, hostname or used port number. This guide will use a Debian like approach on enabling and managing Virtual Hosts on Red Hat Enterprise Linux/CentOS 7.0 by creating two directories on /etc/httpd/ path, which will keep all enabled and disabled website file configurations – sites-available and sites-enabled, and two types of scripts to act as commands, one that enables and other that disables specified virtual hosts – a2ensite and a2dissite. This approach has some advantages because you done have to mess with httpd configuration file and every virtual host has its own configuration file that can be found on a single location – enabled hosts are just symlinks – which make the process of enabling, disabling, creating or deleting them very manageable.

Requirements
Create and Manage Apache Virtual Hosts in RHEL/CentOS 7
1. To begin, start by entering on /etc/httpd/ path, create sites-available and sites-enabled directories and edit Apache httpd.conf file to apply the new enabled websites location.
# cd /etc/httpd/ # mkdir sites-available sites-enabled # nano conf/httpd.conf

2. On httpd.conf file add the following directive line at the bottom of the file, which will make Apache read and parse all files located on /etc/httpd/sites-enabled/ ended in .conf extension.
IncludeOptional sites-enabled/*.conf

3. On next step create a new Virtual Host on sites-available location using a descriptive name – in this case I’ve used rheltest.lan.conf – and use the following file as a template.
# nano /etc/httpd/sites-available/rheltest.lan.conf
Use this configuration as a guide.
<VirtualHost *:80> ServerName rheltest.lan DocumentRoot "/var/www/rheltest.lan" <Directory "/var/www/rheltest.lan"> Options Indexes FollowSymLinks MultiViews # AllowOverride controls what directives may be placed in .htaccess files. AllowOverride All # Controls who can get stuff from this server file Order allow,deny Allow from all </Directory> <IfModule mpm_peruser_module> ServerEnvironment apache apache </IfModule> ErrorLog /var/log/httpd/rheltest.lan-error.log CustomLog /var/log/httpd/rheltest.lan-access.log combined </VirtualHost>

4. If you changed DocumentRoot location on your virtual host from default /var/www/html to other path make sure you also create this path.
# mkdir -p /var/www/rheltest.lan
NOTE: Also assure that ServerName host is a valid DNS record or is added to your local machines hosts file, from where you are planning to visit the website.
5. Now it’s time to create a2ensite and a2dissite bash scripts on a executable system path – in this case is /usr/local/bin/ – but
you can use any executable path that $PATH system variable outputs.
Create a2ensite Script
Create a following file with your choice of editor.
# nano /usr/local/bin/a2ensite
Add the following script to it.
#!/bin/bash if test -d /etc/httpd/sites-available && test -d /etc/httpd/sites-enabled ; then echo "-----------------------------------------------" else mkdir /etc/httpd/sites-available mkdir /etc/httpd/sites-enabled fi avail=/etc/httpd/sites-available/$1.conf enabled=/etc/httpd/sites-enabled/ site=`ls /etc/httpd/sites-available/` if [ "$#" != "1" ]; then echo "Use script: a2ensite virtual_site" echo -e "\nAvailable virtual hosts:\n$site" exit 0 else if test -e $avail; then sudo ln -s $avail $enabled else echo -e "$avail virtual host does not exist! Please create one!\n$site" exit 0 fi if test -e $enabled/$1.conf; then echo "Success!! Now restart Apache server: sudo systemctl restart httpd" else echo -e "Virtual host $avail does not exist!\nPlease see available virtual hosts:\n$site" exit 0 fi fi

Create a2dissite Script
Create a following file with your choice of editor.
# nano /usr/local/bin/a2dissite
Add the whole following script to the file.
#!/bin/bash avail=/etc/httpd/sites-enabled/$1.conf enabled=/etc/httpd/sites-enabled site=`ls /etc/httpd/sites-enabled/` if [ "$#" != "1" ]; then echo "Use script: a2dissite virtual_site" echo -e "\nAvailable virtual hosts: \n$site" exit 0 else if test -e $avail; then sudo rm $avail else echo -e "$avail virtual host does not exist! Exiting!" exit 0 fi if test -e $enabled/$1.conf; then echo "Error!! Could not remove $avail virtual host!" else echo -e "Success! $avail has been removed!\nPlease restart Apache: sudo systemctl restart httpd" exit 0 fi fi

6. After both script files had been created, make sure they are executable and start using them to enable or disable virtual hosts by appending vhost name as command parameter.
# chmod +x /usr/local/bin/a2* # a2ensite vhost_name # a2disite vhost_name
7. To test it, enable the virtual host created earlier, restart Apache service and direct browser to the new virtual host – in this case http://rheltest.lan.
# a2ensite rheltest.lan # systemctl restart httpd


That’s it! Now you can use a2eniste and a2dissite bash scripts as system commands to manage Apache Vhosts file on RHEL/CentOS 7.0.
Hi,
I get the same error as alam above:
when i adding IncludeOptional sites-enabled/*.conf line in httpd.conf then in restart i got error Invalid command ‘IncludeOptional’, perhaps misspelled or defined by a module not included in the server configuration
Has anyone been able to resolve this?
Hi, I am getting the error:
sudo apachectl restart
apachectl: Configuration syntax error, will not run “restart”:
Syntax error on line 410 of /usr/local/apache/conf/httpd.conf:
Invalid command ‘IncludeOptional’, perhaps misspelled or defined by a module not included in the server configuration
Any help you can provide how to overcome this? I searched the net and have seen others get this error as well but did not find any solution.
when i adding IncludeOptional sites-enabled/*.conf line in httpd.conf then in restart i got error Invalid command ‘IncludeOptional’, perhaps misspelled or defined by a module not included in the server configuration
Hi did you get an answer to this one? I have the same issue.
Works as described! Perfect. Thanks!
hello, thank u for the tutorial. can u put a tutorial about run vhost with separate uid ( i tried httpd-itk, suPHP but nothing works with me )
Hello, I have followed all steps, but my issue is that my second website uses my first websites directory!
Interesting approach however I usually don’t turn sites on and off so it is actually easier for me to include the virtualhost right in the httpd.conf, but just wanted to let you know there is a typo in the instructions
/usr/local/bin/a2dissite
Two “ss” later when you execute the script you only have one “s”
This is great, thank you very much!
I’m trying to simplify Vhost management process ;)
you are trying to replicate the debian apache management tool :(