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 ‘addvh.sh’) and calling it with the full domain name of the virtual host passed as a parameter like so:

$ su
# ./addvh.sh my.domain.com

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:

#!/bin/bash
 
# Configure the script
email='me@mydomain.com'		# email address of administrator
vhroot='/www' 				# no trailing slash 
 
# Create the new document root folder
 
if [ "$1" = "" ]
then 
	echo "No arguments passed. Usage: addvh <vhostName> where <vhostName> is the full domain"
	echo "(or subdomain) of the virtual host. E.g test.com, mypoject.test.com"
	exit
 
 
# Try to create the new document root
 
elif ! mkdir $vhroot/$1 
then
	echo "ERROR: "$vhroot"/"$1" could not be created."
else
	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
	then
		echo "ERROR: the virtual host could not be added to the Apache vhosts file"
	else
		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'/" />\
	</Host>\
    </Engine>'
 
 
		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
 
	fi
 
fi

Setting up OpenBD on Centos with support for virtual hosts

I needed to set up a development platform that could serve different dev projects on separate subdomains with several of them using openBD to handle .CFM templates. My chosen platform is Centos 5.5 using the Apache 2.2 web server.

I first tried doing this over a year ago and spent quite a while reading through the various documentation provided by the openBD community but never managed to get it to work. Recently my hosting company (Vivio Technologies) changed their pricing and suggested that I switch from BlueDragon Server JX to OpenBD so I decided to have another go setting up my dev platform so I can test the CFML apps I have running there with openBD before making the switch on the production server.

Since my last attempt the installation process has been made a lot easier due to the OpenBD installer packages created by Jordan Michaels.

I started by installing a fresh VM with Centos 5.5, configuring a couple of subdomains as virtual hosts and creating their respective document root folders under /www. Then, by just running the installer (openbd-1.3-pl0-linux-installer.bin) and accepting all of the default options, I was able to get openBD to successfully serve its demo pages which are contained in the Tomcat webapps/ROOT folder by browsing <CentosVM IP address>/demo/cfdump.cfm.

OpenBD demo page <cfdump>

I also got the openBD administrator by browsing <CentosVM IP address>:8888/bluedragon/administrator/index.cfm

openBD administrator

The next stage, getting virtual hosts was not quite so straightforward. There are some really good instructions on the OpenBD Wiki. The salient point here is that the server.xml file in the tomcat’s conf folder needs to have entries made for each virtual host in much the same way as the httpd.conf file does. But on my first attempt at doing this, something went wrong and I kept getting ‘Service Temporarily Unavailable’ errors from the browser when trying to browse a CFM page from a virtual host. I ended up rolling back my VM to a previous snapshot and reinstalling openBD. This time I re-added the virtual host to the server.xml file and it worked. Now, finally after over a year of trying I have a working dev server that uses OpenBD to parse CFM files and I can get on with the next steps….testing my CFML apps and then deploying them to an OpenBD production server and in the process save myself $10 a month for the BlueDragon Server JX license fee. :)

Update: I wrote this shell script that automates the addition of new virtual hosts

A Windows Icon for OpenBD

openbd-button
Now that I’m using openBD for CFML development, in order to start it I have a batch file containing the command

cd c:\openBD
java -jar start.jar

Using the openBD logo I made a Windows XP/Vista icon that I’ve assigned to this start script so now it lives in my quick launch bar. I thought I’ds share this with anyone else who would like a proper icon for their openBD server instance.

You can download the icon here:

http://blog.xoundboy.com/shared/openbd.ico