The first two categories represent fairly similar approaches. Which approach appeals most to you may largely be determined by your comfort level with Apache HTTP and Tomcat, respectively. The third category, although a temptingly simple solution, is undesirable because Apache handles PHP/CGI content so efficiently and it offers a more secure front end to Web applications.
The approach this solution takes is the second category: Tomcat serves as the primary request processor while Apache serves as the secondary supporting processor. (Figure 1 presents a simple visualization of this approach.) If your preference is more along the lines of the first category outlined above, it is simple enough to adapt this approach accordingly.
Figure 1. Tomcat as Primary Request Processor and Apache as Secondary Supporting Processor |
Configuring Apache
The first step in this whole process may very well be the most daunting. You need to install the Apache HTTP server and configure it to talk to Apache Tomcat. Google searches will quickly turn up a slew of tutorials, wikis, and mailing list entries on the subject. However you configure it, you will likely need to use an Apache Tomcat connector, either mod_jk
or mod_jk2
. I highly recommend that you use mod_jk
, as the mod_jk2
branch was abandoned in 2004.
At the bottom of Apache’s HTTP config file (/$APACHE_INSTALL/conf/httpd.conf), you should find the following block of code:
#
# Use name-based virtual hosting.
#
#NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
Below this section, create a VirtualHost
element to capture all HTTP requests on port 80 and map them according to the methodology described previously (Tomcat is default, Apache only handles PHP and CGI requests):
<VirtualHost *:80>
# Set DocumentRoot equal to Tomcat ROOT context
DocumentRoot /www/webapps/ROOT
# Exclude PHP & CGI (let Apache handle)
SetEnvIf Request_URI \.php no-jk
SetEnvIf Request_URI \.cgi no-jk
# Tell mod_jk to handle everything else (non PHP and CGI content)
# Assign the worker from worker.properties (‘myWorker’) to broker
# the hand-off from Apache to Tomcat
JkMount /* myWorker
</VirtualHost>
As previously described, you could easily tailor the above approach to fit a variant methodology by changing the JkMount
element to pass only certain requests (i.e. *.jsp
or /*/servlet/*
) to Tomcat.