After having created the template, we can now use this to create Virtual Host configurations. In the simplest terms, we need to replace the dummy-host.example.com
URL with sales.example.com
or marketing.example.com
URL. Of course we have to also create the DocumentRoot
directory, the directory where the web pages will be and also add some basic content. When we use a script to run through the process nothing will be forgotten and the edits will be accurate every time. The basics of the script will be as follows:
#!/bin/bash WEBDIR=/www/docs CONFDIR=/etc/httpd/conf.d TEMPLATE=$HOME/template.txt [ -d $CONFDIR ] || mkdir -p $CONFDIR sed s/dummy-host.example.com/$1/ $TEMPLATE > $CONFDIR/$1.conf mkdir -p $WEBDIR/$1 echo "New site for $1" > $WEBDIR/$1/index.html
If we ignore the shebang as the first line, we should know this by now. We can start our explanation in line 2 of the script:
Line |
Meaning |
---|---|
|
We initialize the |
|
We initialize the |
|
We initialize the variable that we will use for the template. This should point to the path of your template. |
|
On a working EL6 host, this directory will exist and is included in the main configuration. If we are running this as a pure test, then we can create a directory to prove that we can create the correct configurations within the target directory. |
|
The |
|
Here, we create the correct subdirectory to house the websites for the new Virtual Host. |
|
In this final step, we create a basic holding page for the website. |
We can create this script as $HOME/bin/vhost.sh
. This is illustrated in the following screenshot. Don't forget to add the execute permission:
To create the sales Virtual Host and web page, we can run the script as shown in the following example. We will be running the script directly as the root user. Alternatively, you may choose to make use of the sudo
command within the script:
# vhost.sh sales.example.com
We can now see how easily we can create Virtual Hosts using a well-crafted script. The configuration file for the Virtual Host will be created in the /etc/httpd/conf.d/
directory and will be named sales.example.com.conf
. The file will look similar to the following screenshot:
The website content must have been created in the /www/docs/sales.example.com
directory. This will be a simple holding page that proves the point that we can do this from the script. Using the following command, we can list the content or the base directory that we use to house each site:
$ ls -R /www/docs
The -R
option allows for the recursive listing. We have used the /www/docs
directory purely as this is set in the original Virtual Host definition that we extracted. You may prefer to use /var/www
or something similar if working in a live environment rather than creating the new directory at the root of your file system. It will be a simple matter of editing the template that we created and that too could be done with sed
at the time of template creation.
We can now use the script to create the Virtual Hosts and the content but we have not allowed for any customization other than the Virtual Host name. Of course, this is important. After all it is this Virtual Host name that is used in the configuration itself as well as in setting the website directory and the configuration file name.
It is possible that we could allow additional options to be specified during the Virtual Host creation. We will use sed
to insert the data as required. The sed
command i
is to insert data before the selection and a
to append after the selection.
For our example, we will add a host restriction to allow only the local network access to the website. We are more interested in inserting data into the file rather than what we are doing with the specific HTTP configuration file. Within the script that we will be adding read
prompts in the script and inserting a Directory
block into the configuration.
To try and explain what we are trying to do, we should see something similar to the following when executing the script. You can see this from the text that we are creating for the marketing site and adding in restrictions as to who can access the site:
As you can see, we can ask two questions, but if needed, more of them can be added to support customization; the idea being that the additional customization should be accurate and reliable in the same way as the script creation was. You may also choose to elaborate the questions with sample answers, so that the user knows how the network address should be formatted.
To aide script creation, we will copy the original vhost.sh
to vhost2.sh
. We can tidy up a few items in the script to allow for easier expansion and then add in the additional prompts. The new script will look similar to the following code:
#!/bin/bash WEBDIR=/www/docs/$1 CONFDIR=/etc/httpd/conf.d CONFFILE=$CONFDIR/$1.conf TEMPLATE=$HOME/template.txt [ -d $CONFDIR ] || mkdir -p $CONFDIR sed s/dummy-host.example.com/$1/ $TEMPLATE > $CONFFILE mkdir -p $WEBDIR echo "New site for $1" > $WEBDIR/index.html read -p "Do you want to restrict access to this site? y/n " [ $REPLY = 'n' ] && exit 0 read -p "Which network should we restrict access to: " NETWORK sed -i "/</VirtualHost>/i <Directory $WEBDIR > Order allow,deny Allow from 127.0.0.1 Allow from $NETWORK </Directory>" $CONFFILE
Please note that we are not running too many checks in the script. This is to keep our focus on the elements that we are adding rather than a robust script. In your own environment, once you have the script working the way you want you may need to implement more checks to ensure the script reliability.
As you can see, we have a few more lines. The WEBDIR
variable has been adjusted to contains the full path to the directory and in a similar way we have added a new variable CONFFILE
, so that we can make a reference to the file directly. If the answer to the first prompt is n
and the user wants no additional customization, the script will exit. If they answer anything other than n
for No, the script will continue and prompt the network to grant access. We can then use sed
to edit the existing configuration and insert the new directory block. This will default to deny access but allow from the localhost
and NETWORK
variables. We refer to the localhost
as 127.0.0.1
in the code.
To simplify the code for better understanding, the pseudo-code will look like the following example:
$ sed -i "/SearchText/i NewText <filename>
Where SearchText
represents the line in the file before which we want to insert our text. Also, NewText
represents the new line or lines that will be added before the SearchText
. The i
command directly following the SearchText
dictates that we are inserting text. Using the a
command to append will mean that the text we add will be added after the SearchText
.
We can see the resulting configuration file for marketing.example.com
, as we have created it with the additional Directory block added in the following screenshot:
We can see that we have added the new block above the closing VirtualHost
tag. In the script, this is the SearchText
that we use. The Directory block we add replaces the NewText
in the pseudo-code. When we look at it, it appears more complex as we have embedded the new lines with
and formatted the file for easier reading with the line continuation character . Again, we have to emphasize that this edit was easy and accurate once the script is created.
For completeness, we include the screenshot of the script vhost2.sh
in the following screenshot:
3.145.70.60