Hi All.

Tonight I found I needed to write yet another Virtual Host definition 
for Apache.  So, seeing as this is a redundant process, I decided to 
automate it with a script, and thought I'd share.

In the script below, you would only need to change the DIR_VHOST, 
DIR_WWW, and DIR_LOG directories to match your setup.  DIR_VHOST is 
where the virtual host config file should be stored (the filename is 
created automagically).  The DIR_WWW is where you want the root 
directory for this host (where the html files go).  The DIR_LOG is where 
to store the log files for this host.

The only nagging point for me is how to handle if someone enters one or 
more spaces for the domain name...  But, seeing as that is not proper 
anyways, this works for me.  Of course, this only creates a basic vhost 
config, but it's a starting point...

Also, I'm sure there's lots that can be done to make this script better. 
  But my initial test works fine - the virtual host is responding properly.

Hope this helps someone.

Shawn

-------------------------

#!/bin/bash
#Created By: Shawn Grover
#Created On: 11 Oct 2006
#This file will add a new configuration file for an Apache Virtual Host

#get the name of the new host
read -p "Enter the virtual host domain: "

#make sure it is not an empty response
if [[ $REPLY == "" ]]
then
         echo "Virtual Host required."
         exit 1
fi

#define our variables
VHOST=$REPLY
DIR_VHOST=/etc/apache2/vhosts.d
DIR_WWW=/home/www/$VHOST
DIR_LOG=/var/log/apache2/$VHOST.log

#determine the filename
VFILE="$DIR_VHOST/$VHOST.conf"

#If the file exists, we are done.
if [ -f $VFILE ]
then
         echo "Virtual Host file already exists."
         exit 0
fi

#create the working directory
if [ -d $DIR_WWW ]
then
         echo "Web directory already exists."
else
         echo "Creating web directory: $DIR_WWW"
         mkdir $DIR_WWW
fi

#Create the file, then add the VHOST directives
touch $VFILE
echo "<VirtualHost *:80>" 1>>$VFILE
echo "  ServerName $VHOST" 1>>$VFILE
echo "  DocumentRoot $DIR_WWW" 1>>$VFILE
echo "  CustomLog $DIR_LOG combined" 1>>$VFILE
echo "  <Directory $DIR_WWW>" 1>>$VFILE
echo "    Options All" 1>>$VFILE
echo "    Order Allow,Deny" 1>>$VFILE
echo "    Allow from All" 1>>$VFILE
echo "  </Directory>" 1>>$VFILE
echo "</VirtualHost>" 1>>$VFILE


#Tell the user what has been done
echo "Virtual Host $VHOST created."
echo "- vhost config file:      $VFILE"
echo "- Log File:               $DIR_LOG"
echo "- Web Directory:  $DIR_WWW"

echo "\nRemember to restart the Apache services to make this VHost 
available"
exit 0


_______________________________________________
clug-talk mailing list
[email protected]
http://clug.ca/mailman/listinfo/clug-talk_clug.ca
Mailing List Guidelines (http://clug.ca/ml_guidelines.php)
**Please remove these lines when replying

Reply via email to