Posted by & filed under Developer Blog.

I’ve spent the last few days trying to figure out how to get the mod_jk connector working so that I can use tomcat and httpd together. The problem, like most linux operations, is that it’s very poorly documented. Not only that, but the decent documentation you find is either outdated or is very specific to the writers desired configuration, and things don’t always work right when you try to revert those instructions back to a “default” configuration for your own use. I hope to document my finding before I forget them, and in a manner that is easy for anyone to understand.

First, you need to install 4 things: Apache, Tomcat, JDK, and Apache2-devel. As of this writing, the latest versions are Apache 2.0, Tomcat 5.5, J2EE SDK 1.4, and Apache2-devel is the development tool for Apache2 that will be needed to create the mod_jk connector. I was able to install Apache2 and Apache2-devel as RPMs in Mandriva (formerly Mandrake) LE 2005 (10.2). For instructions on how to install Tomcat and JDK, check out Integrating Tomcat and Apache on Red Hat Linux <–this site is an excellent resource, but after following it’s instructions to a “T”, I was never able to get things working. I’ll refer to this site often, and let you know what WAS useful from it. Hopefully if you are to the point where connecting Tomcat and Apache is important to you, you have already figured out enough about Linux that installing these 4 things isn’t too big of a problem for you.

The next thing you should do is build the mod_jk connector. The Apache website will try to convince you that you should just download a binary rather than build it from source, but I was much more successful building my own than using the binaries from Apache. Once again the Apache website’s documentation is a freaking mess! I’ll refer to it occasionally and let you know what WAS useful. To build the mod_jk connector, refer to section 4.3 on the Integrating Tomcat and Apache on Red Hat Linux page. These instructions are clear and precise so I won’t bother re-writing them here.* One thing I will mention that those instructions were unclear about, is that you need to be inside the CONN_SRC_HOME/jk/native/ directory in order to run ./ and ./configure, otherwise you will get errors. I also had to copy to /usr/lib/apache2/ instead of where he put it.

Next you need to create a file and place it in the /etc/httpd/conf directory. Using the Minimum from Apache’s documentation worked for me. Just copy and paste everything in the blue box into your file.

Next follow the instructions on the Apache website under the subsection titled Using Tomcat auto-configure. These instructions are written backwards, so remember that you need to do the second part before you even know how to do the first part because depending on your flavor of Linux, the mod_jk.conf file will end up in different places and with different names. I also added workersConfig=”/etc/httpd/conf/” to my server.xml file to get this to work, like this:

<Listener className=”org.apache.jk.config.ApacheConfig” modJk=”/usr/lib/apache2/” workersConfig=”/etc/httpd/conf/” />

In tomcat’s default server.xml file, I put this <Listener> tag just between </Host> and </Engine> at the bottom of the document.

Next, copy the Simple configuration example into the bottom of your httpd.conf file. If you are indeed using Apache 2.0, be sure to remove the line “Add Module mod_jk.c”. If you are using Tomcat 5.x, change the /examples/ directories to /servlet-examples/ and /jsp-examples/ respectively, since they are now broken out like that instead of being lumped together.

Now restart both Apache and Tomcat and you should be good to go. The way I tested was by going to localhost:8080 and in the left hand side of the default tomcat screen there is a section titled “Examples”. Click on either JSP Examples or Servlet Examples and then choose an example listed there (preferably the “Hello World” examples, of course!). Now just replace localhost:8080 in your address bar with localhost (port 80) and you should see the exact same examples but without the static content (such as images). The configuration above assumes that your application’s static html files will be served from your Apache directory and dynamic files (servlets and jsp’s) will be served from your tomcat directory.


*If for some reason a site goes down and a link is broken, please let me know! Even if you are reading this 3 years from now! A lot of my frustrations from getting this to work were broken links and I’d hate for this documentation to become one of those.