CentOS

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

Creating Apache Virtual Hosts with Enable/Disable Vhosts Options in RHEL/CentOS 7.0 &-8211; this Article or News was published on this date:2019-05-28 19:04:51 kindly share it with friends if you find it helpful

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 CentOSApache 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
Apache Virtual Hosting in CentOSCreate 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
Apache Virtual Hosting in CentOSEnable 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>
Apache Virtual Hosting in CentOSCreate 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
Apache Virtual Hosting in CentOSCreate 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
Apache Virtual Hosting in CentOSCreate 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
Apache Virtual Hosting in CentOSEnable Apache Virtualhost

Enable Apache Virtualhost

Apache Virtual Hosting in CentOSVerify 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.