Script to automate adding virtual hosts to OpenBD (using Apache)

As mentioned in my post from yesterday, adding virtual hosts to OpenBD is slightly awkward in that it not only requires adding them in the Apache config but also in the Tomcat’s server.xml file.

The following shell script works for a standard Centos installation where the webserver used is Apache with the following default paths used:

Apache vhosts file -> /etc/httpd/conf/vhosts
Tomcat server.xml file -> /opt/openbd/tomcat/conf/server.xml

Assuming all virtual hosts live inside the same parent directory then this directory can be set in the script’s configuration on line 5. Once set, adding a new virtual host is as simple as saving it as something meaningful (in this case ‘’) and calling it with the full domain name of the virtual host passed as a parameter like so:

$ su
# ./

One important point: this script relies on Apache’s usage of a separate vhosts file in the /etc/httpd/conf directory. This file can be pulled in and read by the main httpd.conf file by adding the following line to it.

Include /etc/httpd/conf/vhosts

What the script does?

1. creates a new document root folder for the virtual host and places an index.cfm test page inside it.
2. adds the virtual host entry to the httpd.conf file
3. adds the virtual host entry to the server.xml file
4. restarts Apache
5. restarts OpenBD

Once the script has run then if all goes well you should be able to browse to your new virtual host and check the CFML parsing by typing http://<new virtual host domain>/index.cfm

Here’s the script:

# Configure the script
email=''		# email address of administrator
vhroot='/www' 				# no trailing slash 
# Create the new document root folder
if [ "$1" = "" ]
	echo "No arguments passed. Usage: addvh <vhostName> where <vhostName> is the full domain"
	echo "(or subdomain) of the virtual host. E.g,"
# Try to create the new document root
elif ! mkdir $vhroot/$1 
	echo "ERROR: "$vhroot"/"$1" could not be created."
	echo "Virtual host document root created in "$vhroot"/"$1
	# Add the virtual host to the vhosts file
	if ! echo "
<VirtualHost *:80>
	ServerAdmin $email
    	DocumentRoot $vhroot/$1
	ServerName $1
	ErrorLog logs/$1_log
</VirtualHost>" >> /etc/httpd/conf/vhosts
		echo "ERROR: the virtual host could not be added to the Apache vhosts file"
		echo "New virtual host added to the Apache vhosts file"
		# Update the Tomcat server.xml file with the new virtual host
		rs='	<Host name="'$1'" appBase="webapps"\
		unpackWARs="true" autoDeploy="true"\
		xmlValidation="false" xmlNamespaceAware="false">\
		<Context path="" docBase="'$vhroot'/'$1'/" />\
		sed 's|</Engine>|'"$rs"'|' </opt/openbd/tomcat/conf/server.xml >tmp 	
		mv /opt/openbd/tomcat/conf/server.xml /opt/openbd/tomcat/conf/server.xml.BAK
		mv tmp /opt/openbd/tomcat/conf/server.xml
		echo 'New virtual host added to the Tomcat server.xml file'
		# restart Apache
		service httpd restart
		# restart Tomcat/OpenBD
		/etc/init.d/openbd_ctl restart
		# create a demo script for testing the CFML works in the new vhost
		echo '<cfdump var="#CGI#">' > $vhroot/$1/index.cfm
		# show the finished message
		echo "Complete! The new virtual host has been created.
To check the CFML functionality browse http://"$1"/index.cfm
Document root is "$vhroot"/"$1