I’ve done a few previous articles on Apache Tomcat (one for installing it on Linux and one for installing it on OS X), but I haven’t yet mentioned installing the JK Connector (mod_jk) in any environment. For those who are wondering what on earth I’m on about, mod_jk allows the Apache web server to serve your Tomcat web apps so they appear on port 80. There are a number of reasons why you might want to do this. Firstly, it provides a simple and secure way to get Tomcat to respond to requests on port 80 without having to have it run as root. Apache is more efficient at serving static pages so it can help increase the efficiency of your web app, and finally it allows you leverage all the power of Apache’s many features for your Java web app.

[tags]Tomcat, Apache, OS X, Mac, mod_jk, Tomcat Connectors[/tags]

As I write this there is no binary version available for Apache 1.3 on Mac OS X so unless you’re using OS X server (or are running Apache2 on your regular OS X install) you’ll have to compile the code yourself. First you’ll have to grab the source from the Apache server and expand it to a folder on your machine. Then you’ll have to open up a terminal, get a root shell (sudo bash) and then change into the folder native inside the folder you got when you expanded the tar.gz file. Here you have to configure the source to prepare to build it with the following command:

./configure --with-apxs=/usr/sbin/apxs

When that completes change into the folder apache-1.3 and actually compile the connector with:

cd apache-1.3
make -f Makefile.apxs

When the compile finishes you have to copy the library that was generated to the appropriate system folder and set the correct permissions on it:

cp mod_jk.so /usr/libexec/httpd/
chmod ugo+x /usr/libexec/httpd/mod_jk.so

At this point mod_jk is installed and all you have to do now is configure it and configure apache to use it. The first thing we need to do is to create the workers property file which is generally saved as /usr/local/tomcat/conf/worker.properties and should contain:

workers.tomcat_home=/usr/local/tomcat
workers.java_home=/Library/Java/Home

worker.list=myworker

worker.myworker.type=ajp13
worker.myworker.host=localhost
worker.myworker.port=8009
worker.myworker.socket_keepalive=1

Note: depending on your configuration you may need to change workers.tomcat_home and workers.java_home.

Now we have to tell Apache to load mod_jk and what tomcat folders to map to what Apache URLs. We do this by editing the apache config file which can be found in /etc/httpd/httpd.conf. You need to add lines like the ones below after the AddModule statements. Don’t add these lines just below the other LoadModule statements above the AddModule statements because that won’t work.

LoadModule jk_module libexec/httpd/mod_jk.so

JkWorkersFile /usr/local/tomcat/conf/worker.properties
JkLogFile /private/var/log/httpd/mod_jk.log
JkLogLevel trace

JkMount /* myworker

Note: this config will map all the webapps in your Tomcat install to apache. I.e. if you have a web app at http://my.server.ie:8080/myWebApp/ then it will now be mapped to http://my.server.ie/myWebApp.

Now just restart Apache and cross your fingers!

apachectl restart