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

Requirements

  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
           </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>
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.

#!/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 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.

#!/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
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.

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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

24 thoughts on “Creating Apache Virtual Hosts with Enable/Disable Vhosts Options in RHEL/CentOS 7.0”

  1. httpd: Syntax error on line 356 of /etc/httpd/conf/httpd.conf: Syntax error on line 13 of /etc/httpd/sites-enabled/test1.conf: Expected but saw

    Error i see when i try to start apache.

    Reply
  2. There is an error in your helpful article. You create “ad2dissite”, but later on you call it with “ad2disite” .. with one “s”

    Reply
    • I know it’s an old post but just chiming in for anybody who might be searching for an answer to this: I had the same issue. The problem was that only root had permissions to run the script but the directory /usr/local/bin isn’t in root’s $PATH environment variable.

      You probably need to put /usr/local/bin in root’s $PATH variable or give your standard user permissions on the script.

      Reply
  3. Hello good afternoon!

    I wonder would with the file /etc/hosts file to allow access to the virtual host across network lan ?

    Thank you!

    Reply
    • Yes it would! But you must manually edit every hosts file from each workstation in your LAN or WAN and add the proper DNS record in order to access apache virtual hosts across your network. For just a few number of machines the hosts file method can be appropriate but for a large number of machines he simplest solution would be to setup a DNS server on your premises.

      Reply
  4. Here is my vhostdel script all I did was allow the script to restart apache after it removes the conf file from sites-enabled.

    #!/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: vhostdel 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!\nNow restarting Apache….”
    sudo systemctl restart httpd
    echo -e “All done!”
    exit 0
    fi
    fi

    Reply
  5. Great article no problems. I did make some modifications to your add vhost script. I also renamed them to something that makes a little more sense IMHO (vhostadd and vhostdel). I use the following script on my development server. The script does the same as yours plus the following:

    When you run the command “vhostadd test1.example.com” the script will check the vhost directory to see if “/var/www/vhosts/test1.example.com” directory exists if it doesn’t it creates it for you. Then it will copy all the files in the “/var/www/vhosts/_vhost_skel” directory and places them in the “/var/www/vhosts/test1.example.com” directory for you! The vhost skeleton directory is helpful for doing redundant folder and file creation such as public_html, index.php, logs. To test to make sure the vhost is setup right I personally add a index.php file to the public_html directory with the following which will echo out the current working directory. So with the example command above your should be able to visit test1.example.com and it should display the directory that the index.php is located in which would look something like this on my setup: /var/www/vhosts/test1.example.com/public_html. Once the vhost skeleton files have been moved the script will know chown the directory recursively and chmod the directory recursively. Then the system will restart apache for you. All of the directories, chown user and group, and chmod permissions are all vars at the top of the script, modify them as you see fit.

    #!/bin/bash

    avail=/etc/httpd/sites-available/$1.conf
    enabled=/etc/httpd/sites-enabled/
    site=`ls /etc/httpd/sites-available/`
    vhostdir=/var/www/vhosts
    sitevhostdir=$vhostdir/$1
    sitevhostskel=$vhostdir/_vhost_skel
    chownusergroup=opterons:dev
    chmodperms=2755

    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

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

    if test -e $avail; then
    if [ ! -d “$sitevhostdir” ]; then
    echo -e “VHost directory: $sitevhostdir does not exist, no worries.\nCreating directory: $sitevhostdir now….”
    mkdir -p “$sitevhostdir”
    cp -rf “$sitevhostskel”/* “$sitevhostdir”
    chown -R “$chownusergroup” “$sitevhostdir”
    chmod -R “$chmodperms” “$sitevhostdir”
    fi
    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 restarting the Apache server…”
    sudo systemctl restart httpd
    echo “All done!”
    else
    echo -e “Virtual host $avail does not exist!\nPlease see available virtual hosts:\n$site”
    exit 0
    fi

    fi

    Reply
  6. I went step by step on this, configured DNS and even gave the whole directory more permissions. When I go to the VirtualHost site I get only the default Apache page.

    Did I miss a step?

    Reply
  7. 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?

    Reply
  8. 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.

    Reply
  9. 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

    Reply
  10. 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 )

    Reply
  11. 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”

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.