Prompting for data during site 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 hostname. Of course, this is important. After all, it is this virtual hostname 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 used 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, we will be adding read prompts 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 from the text that we are creating this 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 >\ 
  \n  Order allow,deny\ 
  \n  Allow from 127.0.0.1\ 
  \n  Allow from $NETWORK\ 
\n</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 script reliability.

As you can see, we have a few more lines. The WEBDIR variable has been adjusted to contain 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 access 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>  

Here 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 \n and formatted the file for easier reading with the line continuation character \. Again, we have to emphasize that this edit is easy and accurate once the script is created.

For completeness, we include the following screenshot of the script vhost2.sh: