Creating Apache Virtual Hosts with Enable/Disable Vhosts Options in RHEL/CentOS 7.0

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.

Apache Virtual Hosting in CentOS

Apache Virtual Hosting with Enable/Disable Options


  1. LAMP Basic Installation on RHEL/CentOS 7.0

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
Create Apache Vhost Directories

Create Apache Vhost Directories

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
Enable Apache Directories

Enable Apache Directories

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
        <IfModule mpm_peruser_module>
                ServerEnvironment apache apache
        ErrorLog  /var/log/httpd/rheltest.lan-error.log
        CustomLog /var/log/httpd/rheltest.lan-access.log combined
Create Apache Vhosts

Create Apache Vhosts

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.

if test -d /etc/httpd/sites-available && test -d /etc/httpd/sites-enabled  ; then
echo "-----------------------------------------------"
mkdir /etc/httpd/sites-available
mkdir /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

if test -e $avail; then
sudo ln -s $avail $enabled

echo -e "$avail virtual host does not exist! Please create one!\n$site"
exit 0
if test -e $enabled/$1.conf; then

echo "Success!! Now restart Apache server: sudo systemctl restart httpd"
echo  -e "Virtual host $avail does not exist!\nPlease see available virtual hosts:\n$site"
exit 0
Create a2ensite Script

Create a2ensite Script

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.

site=`ls /etc/httpd/sites-enabled/`

if [ "$#" != "1" ]; then
                echo "Use script: a2dissite virtual_site"
                echo -e "\nAvailable virtual hosts: \n$site"
                exit 0

if test -e $avail; then
sudo rm  $avail
echo -e "$avail virtual host does not exist! Exiting!"
exit 0

if test -e $enabled/$1.conf; then
echo "Error!! Could not remove $avail virtual host!"
echo  -e "Success! $avail has been removed!\nPlease restart Apache: sudo systemctl restart httpd"
exit 0
Create a2dissite Script

Create a2dissite Script

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
Enable Apache Virtualhost

Enable Apache Virtualhost

Verify Apache Virtualhost

Verify Apache Virtualhost

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.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Matei Cezar

I'am a computer addicted guy, a fan of open source and linux based system software, have about 4 years experience with Linux distributions desktop, servers and bash scripting.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide
The Complete Linux System Administrator Bundle
Become an Ethical Hacker Bonus Bundle

You may also like...

24 Responses

  1. Dinesh says:

    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?

  2. Dinesh says:

    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.

  3. alam says:

    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

  4. Flo says:

    Works as described! Perfect. Thanks!

  5. br4v37 says:

    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 )

  6. John says:

    Hello, I have followed all steps, but my issue is that my second website uses my first websites directory!

  7. mstone says:

    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


    Two “ss” later when you execute the script you only have one “s”

  8. anchorite says:

    This is great, thank you very much!

  9. Matei Cezar says:

    I’m trying to simplify Vhost management process ;)

  10. c0m0 says:

    you are trying to replicate the debian apache management tool :(

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.