Creating Apache Virtual Hosts with Enable/Disable Vhosts Options in RHEL/CentOS 7.0
Abstract: echo -e "$avail virtual host does not existecho -e "$avail virtual host does not exist
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 with Enable/Disable Options Requirements- LAMP Basic Installation on RHEL/CentOS 7.0
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.confCreate 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/*.confEnable 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
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 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 fiCreate 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 fiCreate 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 httpdEnable 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.