Virtual Host Headers and ColdFusion Multi-Server Installation
Mark Kruger recently had an excellent post over at the ColdFusion Muse about Virtual Host headers. While his post showed how to do this in IIS, I wanted to show you how to do it in Apache and at the same time, combine it with a ColdFusion Multi-Server Installation.
Apache has enjoyed Virtual Host Header support for serveral years now. All that is needed is to modify your httpd.conf file to use the <VirtualHost> directive. Beginning with Apache 2.0, the recommended configuraton for these virtual hosts were put into a separate file: typically: ‘./conf/extra/httpd-vhosts.conf’. You’ll find that the main httpd.conf file has a line near the end of the file to include this extra confirution file. It works just like CFINCLUDE, so the contents of ‘httpd-vhosts.conf’ are appended in-line with the main httpd.conf file. Be default, this line is commented out, so you’ll need to un-comment it to get Virtual Hosts to work in Apache.
I add a slight twist to this general setup. I create a folder named ‘virtuals’ under my Apache Configuration directory. I then modify the main htpd.conf file to use this line:
Include virtuals/*.conf
This tells Apache to include ALL of the files in the virtuals folder that end in ‘.conf’. I can now create multiple virtual host files with explicit names. For example, in my virtuals folder, I’ll have the following lines:
dev.site1.com.conf
dev.site2.com.conf
www.site.com.conf
www.site2.com.conf
Each of these files contains a single VirtualHost directive, telling Apache how to configure each virtual site. I prefer to have the configuraton for each site in it’s own configuration file, rather than having them all bundled together in one large file. Two benefits of this approach are: 1) I can put SSL support in the sites that use the SSL certificate, 2) I can remove supprt for a site by simply renaming the configuraton file to something that ends with an extension other than ‘.conf’.
ColdFusion Multi-Server Installation
We all now by now that we can have multiple ColdFusion deployments (with a context root of /) on our server by createing multiple JRun Servers. This allows me to have a single CF deployment for ’site1′, and a separate CF deployment for ’site2′. Consider the following JRun servers defined in [jrun-root]/servers/.
site1
site2
Each of these JRun servers contains a separate copy of ColdFusion from an expanded EAR deployment. Each of these two JRun servers also use unique HTTP, Proxy, and JNDI ports.
Now, when you use the JRun Web Connector utility to configure Apache (or by using the default installation to modify Apache for you), you’ll find that the Apache Configuration file contains the following lines:
LoadModule jrun_module modules/mod_jrun20.so
<IfModule mod_jrun20.c>
JRunConfig …
JRunConfig Bootstrap 127.0.0.1:51002
JRunConfig …
AddHandler jrun-handler .jsp .jws .cfm .cfml .cfc .cfr .cfswf
</IfModule>
I’ve specifically designated the two really important configuraton items here. There are probably 5-10 line in the IfModule directive.
This tells Apache to send all requests ending in specific extensions (.jsp .jws .cfm, etc…) to another server on a specific proxy port (127.0.0.1:51005).
You can copy the <IfModule> block from the main httpd.conf file into your individual virtual hosts files in the virtuals folder. By changing the port number in the JRunConfig Bootstrap line, you can now use a different JRun Server, and by extension, the specific ColdFusion deployment to use for your virtual site.
This allows me to deploy different versions of ColdFusion for different sites, or to keep the ColdFusion configuraton settings completely unique and different for each site that I am supporting in Apache. I can now grant people access to the CF Administrator for site1, and keep them from prying into datasources and other settings that are used by a different team on site2.