Friday, February 26, 2010

Connecting Apache's Web Server to Multiple Instances of Tomcat

I was tasked today with connecting an apache web server to multiple instances of Tomcat. In my search for resources to do this I found this, an excellent article in the Linux Journal by Daniel McCarthy on how to do just what I was looking to do. The only problem was that it was slightly outdated. McCarthy assumes the following setup:
* J2sdk1.4.2_09
* Tomcat 5.0.28
* Apache 2.0.54
* mod_jk 1.2.14

Whereas I had the following setup
- java1.6.0_0
- Tomcat5.5.27
- Apache 2.2.12
- mod_jk1.2.14
- Ubuntu9.10

I did note that there were some differences in the set up and also try to cover some issues I was unsure about when following the tutorial. This is not meant to be a tutorial - it is merely a few pointers if you have the same setup as me (and also to ensure I remember how to do this in future). If you are trying to connect apache to multiple tomcats firstly consult the tutorial in Linux Journal and if you are having trouble then consult this post - maybe you are having one or two of the same problems...

The first part of the article outlines how to install mod_jk1. This involved downloading and compiling it from source and then loading the module. I found that this was not necessary as I was able to use apt-get to install mod_jk1 using the following command:
sudo apt-get install libapache2-mod-jk

I am aware in some cases you may not want to use apt-get to install this module into apache and install it manually. If so I would like to point out that the command is incorrect in the original Linux Journal article - should be:

#Load the mod_jk connector
LoadModule jk_module /usr/lib/httpd/modules/

In the original article a tomcat startup script is defined. I could not get this to work. Firstly at the start of the script it tries to import /etc/init.d/functions - but in my version of ubuntu functions did not exist in this directory. The script also tried to start the tomcat server with the following call:
/bin/su tomcat $CATALINA_HOME/bin/

When I used this I got an error that the id tomcat was not recognised. - unknown id: tomcat. To fix this I edited the script and removed the import of the functions directory and also simplified the startup script as follows:

# tomcat
# chkconfig:
# description: Start up the Tomcat servlet engine.
# Source function library.
#. /etc/init.d/functions

export CATALINA_BASE="/opt/tomcat_instance1"
export CATALINA_HOME="/opt/tomcat_instance1"
export JAVA_HOME="/usr/lib/jvm/java-6-openjdk"

case "$1" in
if [ -f $CATALINA_HOME/bin/ ];
echo $"Starting Tomcat"
if [ -f $CATALINA_HOME/bin/ ];
echo $"Stopping Tomcat"
echo $"Usage: $0 {start|stop}"
exit 1
exit $RETVAL

Now on to the single item that took me a VERY long time to figure out. The original article outlines how you should set up workers so that apache can redirect some requests to other servers. This is all defined in a file. The article also tells you that you need to tell apache about the file and this is done by adding the following to apache's httpd.conf file:

JkWorkersFile "/etc/httpd/conf/"
JkLogFile "/var/logs/www/mod_jk.log"
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"

I did this and then restarted the apache server. It failed. It wouldnt start and whats more I got absolutely no error. At this point the obvious thing to think is that Jk_mod isn't installed properly on Apache - so I checked and rechecked and reinstalled - no joy. Eventually, as those of you that are more observant than me have figured out, I saw that the log file was pointing to /var/logs - which doesnt exist on the version of ubuntu I am running. I changed this to /var/log/www/mod_jk.log and all was good in the world again.