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

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

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

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

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

You may also like...

23 Responses

  1. Daniel says:

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

  2. ritu says:

    Hi, the httpd services not restart using systemctl restart httpd command.

  3. Galathilion says:

    Getting this when I run a2ensite

    -bash: a2ensite: command not found

    Follwed everything above. Am on RHEL 7

    • Matei Cezar says:

      I don’t think you followed each step! make sure the scripts have executable bit set.

    • Sir Oliver Chatsalott says:

      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.

  4. Kaio says:

    Hello good afternoon!

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

    Thank you!

    • Matei Cezar says:

      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.

  5. opterons says:

    Here is my vhostdel script all I did was allow the script to restart apache after it removes the conf file from 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

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

  6. opterons says:

    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” the script will check the vhost directory to see if “/var/www/vhosts/” 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/” 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 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/ 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.


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

    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

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

    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”
    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 restarting the Apache server…”
    sudo systemctl restart httpd
    echo “All done!”
    echo -e “Virtual host $avail does not exist!\nPlease see available virtual hosts:\n$site”
    exit 0


  7. Brusse James says:

    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?

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.