Friday, December 23, 2005

Apache, Tomcat, and mod_jk

Last evening I spent a couple of hours configuring Tomcat to work with Apache, with the help of mod_jk in a windows environment. mod_jk is a module that passes specific requests from Apache to Tomcat. Tomcat processes a request, and sends the response back through Apache. It's a simple enough concept, and fortunately, it's relatively simple to configure as well.

The first thing I did was install Tomcat. In a windows environment, the best way to do this is to install with the executable installer. This hooks into the windows services which control the startup and stop of Tomcat. Apache should be installed the same way. I chose to install Apache 2.0.45 and Tomcat 5.5.12. The Apache installation is a bit old, but the Tomcat version is the latest that's available. Installing these in a default way is the best way to start.

At this point, you can install mod_jk. This is available as a binary download, as well as in source for custom building. Simply place the mod_jk.so binary module file that you download in the modules folder for Apache to find. Then, in the httpd.conf for Apache, point to the module as such

LoadModule    jk_module  modules/mod_jk.so
I then followed the instructions layed out in the quick start guide for configuring mod_jk to work with Apache and Tomcat. The workers.properties file is very basic, so be forewarned. This should work pretty well for development purposes, but for a production environment, you'll probably want to set up some sort of load balancing.

OK, back to the httpd.conf. The entire relevant configuration for mod_jk looks like this:

LoadModule      jk_module  "C:/Program Files/Apache Group/Apache2/modules/mod_jk.so"
JkWorkersFile "C:/Program Files/Apache Group/Apache2/conf/workers.properties"
JkLogFile "C:/Program Files/Apache Group/Apache2/logs/mod_jk.log"
JkLogLevel info
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
But that's just for me. I simply placed this in a seperate file called mod_jk.conf, and included it in the httpd.conf file.

Then I set up a virtualhost, where I set the all important Jkmount directive. The virtualhost looks like this:

<virtualhost>                                               
DocumentRoot "C:/tomcat/webapps"
ServerName localhost
ErrorLog logs/localhost-error_log
CustomLog logs/localhost-access_log common

JkMount /jsp-examples/* worker1

DirectoryIndex index.jsp index.php index.html
ErrorDocument 404 /404.html
<directory>
Options MultiViews Indexes FollowSymlinks
AllowOverride All
</directory>

</virtualhost>

The place to note is the Jkmount directive. Now, when I go to http://localhost/jsp-examples, it looks the same as http://localhost:8080/jsp-examples. I am successfully serving jsp's and servlets through port 80.

3 comments:

Anonymous said...

Seems like all these tutorials are flawed in one way or another. I have a site called www.mysite.com. When I hit that site, I want it to forward on the Tomcat folder and render it. I don't want to type in localhost anywhere. I want the DNS to resolve my name to my Apache installation and hit the Tomcat app. This does not do that. I am sure you know how to do it but if you could provide more insight on this, we would all be better off. Thank you.

Eric said...

If I understand your problem correctly, typing in your site in the url doesn't work because of 2 elements. DNS resolution and virtualhost configuration of your Apache and Tomcat servers.

The easiest way to simulate proper dns resolution is to edit your hosts file so the domain name points to the proper ip address (/etc/hosts in *nix, C:\WINDOWS\System32\drivers\hosts in Windows). There should be proper examples in those files

You'll also need to get your virtualhost configuration set up correctly. Tomcat has a server.xml file, which you can edit by adding a host entry. It will look something like this:

<Host name="www.domain.com"
appBase="webapps/myApp"
unpackWARs="true"
autoDeploy="true"
xmlValidation="false"
>
<Alias>domain.com</Alias>
<Context path="" docBase="" debug="0" />
<Logger className="org.apache.catalina.logger.FileLogger"
directory="logs" prefix="domain_log." suffix=".txt"
timestamp="true"/>
</Host>

Finally, you should make sure your Apache virtualhost is configured correctly. I don't have one handy, but some searching should help you. It's a simple thing to do, and quite common.

Anonymous said...

That's my comment below. I didn't expect a reply otherwise I'd have been more specific. :-) I already have an Apache installation and a web site set up. I only used www.mysite.com as a example.

Right now, when I type in www.mysite.com in the url address bar out on the internet, I can get static pages off of my c:/vhosts folder. Works great.

Now, I want to add dynamic content. I already have a webapp written that works great on localhost under Tomcat. I type in http://localhost:8080/mysite and it pops up my index.jsp page for mysite webapp. Great!

Problem for me is hooking up the two together. So when I type in www.mysite.com, I want the index.jsp page served by the webapp. Simple enough request. The documentation on Apache and other places is outdated. I have Tomcat 6, Apache 2.2.

Right now, I have downloaded mod_jk into the modules folder. I read about workers.properties and so on but am not sure what I need to do next. In other words, I need to create a workers.properties file and modify httpd.conf file. Is that all I need to do? What mods do I make to these files? Thanks in advance.