Friday, September 17, 2004

Configuration of Apache-Tomcat connector



Well up til now I've always tried to avoid doing this. I've got away with just using the default Tomcat installation to serve up static as well as dynamic content. But now I need to really get to grips with integrating Tomcat with Apache, so that my static content can continue to be served up as normal, but any JSP/servlet-type content that I write (and I really want to write some! I'm missing coding Java like mad in this new job!) should be handled by Tomcat.

Configuration of the Connector at first sight looks a right nightmare. There are various connectors available, some quite old, some new, some replacements, and they have different requirement of code level etc. Which to choose?? No idea, so I'll go with what I found first, which is JK2. The docs appear to indicate this is the way to go. I will try to note down if this proves to not be the case.

My key source of info is the HOW-TO, here: http://jakarta.apache.org/tomcat/connectors-doc/jk2/jk/aphowto.html. This blabbers on about Tomcat 3 and other stuff, so I hope it's not hideously out of date.
NB there is also this "QuickStart" HOW-TO http://jakarta.apache.org/tomcat/connectors-doc/jk2/jk/quickhowto.html, which seems to give a bit more general info compared to the one mentioned above. Need to use both in conjunction with each other...

First thing, I need to get the connector modules for my Linux distro. This is Fedora core 1, and a version appears to exist already: http://apache.rmplc.co.uk/jakarta/tomcat-connectors/jk2/binaries/. Choose the right distro!
I got this link from the Jakarta download site at http://jakarta.apache.org/site/binindex.cgi/.

The downloaded tarball contains a directory structure which includes the mod_jk2.so module, which is the thing I want. The next things on the HOW-TO are to get the mod_jk2.so and workers.properties. So I copy the tarball to my Linux box, uncompress it (gunzip) and extract the TAR. Following the HOW-TO, I now put the usr/lib/httpd/modules/mod_jk2.so from the extracted directory structure into the Apache directory (which is /etc/httpd/modules for my system). I also copied libjkjni.so from the same modules directory (/home/paul/Fedora-Core-1-i386/usr/lib/httpd/modules) - I don't know if I'll need it...

I now copy the workers.properties to the /etc/httpd/conf directory. The HOW-TO isn't clear about where to put this file, so I'm not sure if this is the right place yet... Also for some reason in the tarball it is called workers2.properties. I have renamed it to workers.properties... I don't know if this is a good idea yet...

The HOW-TO now mentions some way of setting an auto-configure thing called mod_jk.conf-auto, but I cannot find this file, so I guess I have to update httpd.conf manually... The next section in the HOW-TO is a bunch of sample directives for the Apache file (I hope!). So I now insert those directives into my httpd.conf and do a syntax check on the Apache config ("apachectl -t").

Of course, apachectl -t now starts throwing errors. So now I have to figure out what I've done wrong, or why it can't find stuff. The error is:
[root@faro conf]# apachectl -t
Syntax error on line 215 of /etc/httpd/conf/httpd.conf:
Invalid command 'JkWorkersFile', perhaps mis-spelled or defined by a module not included in the server configuration

Having now read this FAQ: http://jakarta.apache.org/tomcat/faq/connectors.html, I'm thinking maybe I should be using JK instead of JK2. So... will now try to download that instead and see what happens. Except, surprise surprise, there's no binaries built for Fedora core 1, unless I can just use the general Linux binary at http://apache.rmplc.co.uk/jakarta/tomcat-connectors/jk/binaries/linux/ which mentions "fc2" in the name - I wonder if this is Fedora Core 2?? OK will have a go at downloading that...

So now I downloaded the JK1 version of the .so file, and just renamed it (assuming it's really mod_jk.so!!) - which is yet to be proved. Although I did copy it to /etc/httpd/modules, updated the httpd.conf to point to the mod_jk.so file, and apachectl -t now says "Syntax OK".

OK I restarted Apache. Just to see what happens, I put a test.jsp in the Apache root directory. I've no idea if Tomcat is going to serve it up. But I don't see any dynamic content in there (I just put in the obvious basic scriptlet out.println("Hello World"); but I don't get anything back... I have a feeling there's a shedload of configuration still to do though... mustn't speak too soon!

I forgot that I had a JK2 version of workers.properties. Using the sample one from http://jakarta.apache.org/tomcat/connectors-doc/jk2/jk/quickhowto.html, I restarted Apache, and having started Tomcat, hitting http://localhost/example now elicits a 404 response from Tomcat, so something is obviously happening. I think I need to get the directory mappings that Apache and Tomcat are using, sorted.

So it's back to the Apache-specific HOW-TO mentioned at the top.
First thing I did was to make sure any .jsps and servlets (from any URL context) are being passed to Tomcat, with the following updated directives:

# send all requests ending in .jsp to worker1
JkMount /*.jsp worker1
# send all requests ending /servlet to worker1
JkMount /*/servlet/ worker1

So next I think I have to start configuring the directories.
(Some time later...)
And sure enough there's quite a lot of figuring-things-out to be done.
I tried to hit http://myhost/test.jsp to see if Apache would pass that to Tomcat on the basis of the JkMount rule above (i.e. send all URIs in the form of *.jsp to the Tomcat connector. I think it did, but of course Tomcat can't find that file in its ROOT context. So I copied my test.jsp file (which I had in the Apache root /var/www/html) to /usr/java/tomcat/webapps/ROOT, and it found it straight away! Yeah, I'm onta something!

I also put the Apache JK debug on - so don't forget to switch that off when you've got it all figured... It definitely seems to be hardcoded in Tomcat to always look in the context called ROOT, coz I can't find that configured specifically anywhere.

OK I think that's enough for this week - before my brain blows a fuse...

Labels: