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

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. Use our Linode referral link if you plan to buy VPS (it starts at only $10/month).
  4. Support us via PayPal donate - Make a Donation
  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...

16 Responses

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

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

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

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

  4. 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 *

Join Over 300K+ Linux Users
  1. 177,942
  2. 8,310
  3. 37,548

Are you subscribed?