![]() |
![]() |
By Gal Shachor <shachor@il.ibm.com>
mod_jk is a replacement to the elderly mod_jserv. It is a completely new Tomcat-Apache plugin that handles the communication between Tomcat and Apache
Several reasons:
You will need to get to know a new simplified configuration mechanism. The advantage is that learning this mechanism will give you a head start if you want to deploy Tomcat on other web servers such as IIS and Netscape (oops, iPlanet).
During this document I am going to use a few terms, so lets define them:
Term |
Meaning |
---|---|
Worker process |
A worker is a tomcat instance that is running to serve servlet requests coming from the web server. In most cases there is only a single worker (the one and only tomcat process) but sometimes you will run multiple workers to achieve load balancing or site partitioning. Each worker is identified to the web server by the host were it is located, the port where it listens and the communication protocol used to exchange messages. |
In process worker |
This is a special worker. Instead of working with a Tomcat process residing on another process, the web server opens a JVM and executes Tomcat inside the web server process address space. Our discussion in this document is not going to get into this special worker. |
Web server plugin/tomcat redirector |
For Tomcat to cooperate with any web server it needs an "agent" to reside in the web server and send him servlet requests. This is the web server plugin, and in our case the web server plugin is mod_jk. The redirector usually comes in the shape of a DLL/shared object module that you should plug into the web server. |
Plugin configuration |
We need to configure the web server plugin so that it will know where are the different Tomcat workers and to which of them it should forward requests. This information accompanied with some internal parameter such as the log level comprises the plugin configuration. |
Web server configuration |
Each web server has some configuration that defines how behave, e.g. on which port to listen, what files to serve, what web server plugins to load, etc. You will need to modify your web server configuration to instruct it to load the tomcat redirector. |
The configuration includes the following steps:
If you've already configured Apache to use mod_jserv, remove any ApJServMount directives from your httpd.conf. If you're including tomcat-apache.conf or tomcat.conf, you'll want to remove them as well - they are specific to mod_jserv.
The redirector was developed using Visual C++ Ver.6.0, so having this environment is a prereq if you want to perform a custom build.
The steps that you need to take are:
This will build both release and debug versions of the redirector plugin (mod_jk).
An alternative will be to open mod_jk.dsp in msdev and build it using the build menu.
apxs -o mod_jk.so -DSOLARIS -I../jk -I/usr/java/include -I/usr/java/include/solaris -c *.c ../jk/*.cOn some systems, this will build the module correctly, but will fail at runtime with a "symbol "fdatasync" not found". To fix, add -lposix4 just before the -c in the above command.
apxs -o mod_jk.so -I../jk -I/usr/local/jdk/include -I/usr/local/jdk/include/linux -c *.c ../jk/*.cYour build may fail because the object files from the ../jk directory have been compiled to the current directory, rather than their source directory. Running gcc -shared -o mod_jk.so *.o should finish the build.
The servlet.xml file already has a block similar to this for Ajp12 connections on port 8007 (as delivered by mod_jserv). Even if you think you're only using Ajp13, you probably don't want to delete this connector - it's required to shut down Tomcat.<Connector className="org.apache.tomcat.service.PoolTcpConnector"> <Parameter name="handler" value="org.apache.tomcat.service.connector.Ajp13ConnectionHandler"/> <Parameter name="port" value="8009"/> </Connector>
In most of simple cases Tomcat can generate the needed Apache configuration. When Tomcat starts up it will automatically generate a configuration file for Apache in TOMCAT_HOME/conf/mod_jk.conf-auto. Most of the time you don't need to do anything but include this file (appending "Include TOMCAT_HOME/conf/mod_jk.conf-auto") in your httpd.conf. That's it, you can now start Tomcat and Apache and access Tomcat from the Apache server.
If you have special needs, for example mounting URL prefixes that are not the default, you can use this file as a base for your customized configuration and save the results in another file. If you manage the Apache configuration yourself you'll need to update it whenever you add a new context.
Tomcat 3.2: you must restart tomcat and apache after adding a new context; Apache doesn't support configuration changes without a restart. Also the file TOMCAT_HOME/conf/mod_jk.conf-auto is generated when tomcat starts, so you'll need to start Tomcat before Apache. Tomcat will overwrite TOMCAT_HOME/conf/mod_jk.conf-auto each startup so customized configuration should be kept elsewhere.
Workers are configured using the file TOMCAT_HOME/conf/workers.properties. There is a great deal of information in the workers.properties howto document, and you should really look at that first. If you're in a hurry however, you can probably get away with editing the file workers.properties and setting the workers.tomcat_home, workers.java_home and ps variables to the correct values for your system.
Configuring Apache to use mod_jk is done using the Apache server configuration directives; to get you started, look at the auto-generated mod_jk.conf-auto available in Tomcat's conf directory.
LoadModule jk_module libexec/mod_jk.so AddModule mod_jk.c JkWorkersFile /usr/local/jakarta-tomcat/conf/workers.properties JkLogFile /usr/local/apache/logs/mod_jk.log JkLogLevel warn
Use mod_jk's JkMount directive to assign specific URLs to Tomcat. In general the structure of a JkMount directive is:
JkMount <URL prefix> <Worker name>
For example the following directives will send all requests ending in .jsp or beginning with /servlet to the "ajp13" worker, but jsp requests to files located in /otherworker will go to "remoteworker".
You can use the JkMount directive at the top level or inside <VirtualHost> sections of your httpd.conf file.JkMount /*.jsp ajp13 JkMount /servlet/* ajp13 JkMount /otherworker/*.jsp remoteworker
. . <Connector className="org.apache.tomcat.service.PoolTcpConnector"> <Parameter name="handler" value="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"/> <Parameter name="port" value="8007"/> </Connector> <Connector className="org.apache.tomcat.service.PoolTcpConnector"> <Parameter name="handler" value="org.apache.tomcat.service.connector.Ajp13ConnectionHandler"/> <Parameter name="port" value="8009"/> </Connector> <Host name="host1.apache.org"> <Context path="" docBase="/web/host1" debug="0"/> </Host> <Host name="host2.apache.org"> <Context path="" docBase="/web/host2" debug="0"/> </Host> . . |
# Setup for Solaris system # workers.tomcat_home=/usr/local/jakarta-tomcat workers.java_home=/usr/java ps=/ worker.list=ajp12, ajp13 # Definition for Ajp13 worker (Ajp12 left to readers imagination) # worker.ajp13.port=8009 worker.ajp13.host=localhost worker.ajp13.type=ajp13 |
# Load mod_jk # LoadModule jk_module libexec/mod_jk.so AddModule mod_jk.c # Configure mod_jk # JkWorkersFile /usr/local/jakarta-tomcat/conf/workers.properties JkLogFile /usr/local/apache/logs/mod_jk.log JkLogLevel warn # First Virtual Host. # <VirtualHost 10.0.0.1:80> DocumentRoot /web/host1 ServerName host1.apache.org JkMount /*.jsp ajp13 JkMount /servlet/* ajp13 </VirtualHost> # Second Virtual Host. Also accessible via HTTPS # <VirtualHost 10.0.0.2:80> DocumentRoot /web/host2 ServerName host2.apache.org JkMount /*.jsp ajp13 JkMount /servlet/* ajp13 </VirtualHost> <VirtualHost 10.0.0.2:443> DocumentRoot /web/host2 ServerName host2.apache.org SSLEngine On JkMount /*.jsp ajp13 JkMount /servlet/* ajp13 </VirtualHost> |
This document was created by Gal Shachor, and was revised by Mike Bremford with help from the countless many on the tomcat-dev and tomcat-user lists!
Copyright ©1999-2000 The Apache Software Foundation |