# Apache Virtual Hosts

### Virtual Hosts Folder Structure

1. Entering on /etc/httpd/ path and create sites-available and sites-enabled directories

```
Enter httpd folder
==================
cd /etc/httpd/

Create necessary directories
============================

mkdir sites-available sites-enabled
```

### Httpd.conf Changes

1. Edit http.conf file and add the below 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

```
Open httpd.conf
===============

vi /etc/conf/httpd.conf

Add the below line
==================

IncludeOptional sites-enabled/*.conf
```

### Create Enable/Disable Site Scripts

#### Enable Site Script

1. Create the following file, a2ensite, in /usr/local/bin and add the script to it

```
Create file
===========

vi /usr/local/bin/a2ensite

Add the script
==============

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

#### Disable Site Script

1. Create the following file, a2dissite, in /usr/local/bin and add the script to it

```
Create file
===========

vi /usr/local/bin/a2dissite

Add the script
==============

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

#### Make The Scripts Executable

1. Ensure both scripts are executable

```
chmod +x /usr/local/bin/a2*
```

### Create Virtual Host Configs

#### The Virtual Host Config File

1. Create a new Virtual Host on sites-available location using a descriptive name  
      
    `/etc/httpd/sites-available/test.lan.conf`
2. Use the below file as a template (replacing `test.lan` with appropriate details

<p class="callout info">The template enforce the use of SSL (https connections to the site)</p>

```
<VirtualHost test.lan:80>
        ServerName test.lan

        Redirect permanent / https://test.lan

</VirtualHost>

<VirtualHost test.lan:443>
        ServerName test.lan

        DocumentRoot "/var/www/test.lan"
        <Directory "/var/www/test.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>

</VirtualHost>
```

#### Folder Structure

1. Create the corresponding folder if the default path was changed

```
mkdir -p /var/www/test.lan
```

<p class="callout danger">Remember to create a corresponding DNS record to access the site in the Xneelo config console  
  
Change folder and file ownership `chown -R apache:apache /folder location/folder`</p>

### Enable a Site

1. Run the below commands one-after-the-other to enable a site

```
a2ensite rheltest.lan

systemctl restart httpd
```

<p class="callout warning">Remember to issue a certificate for the site or else the site will be inaccessible</p>