macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Integrating Tomcat with Apache Via the mod_jk Module
Pages: 1, 2

With the module library now compiled, we need to install the module and instruct Apache how to load the mod_jk libraries. To do so, we'll have to do a few tasks as the "superuser." (I hope you paid attention to the warnings about using the superuser account!)



First, copy our newly-created module to the directory that holds the other Apache module files:

sudo cp mod_jk.so /usr/libexec/httpd/ 

Second, we'll have to modify the configuration file that controls the operation of the Apache Web Server, httpd.conf. Use your favorite text editor (the author's preference is the emacs text editor).

emacs /etc/httpd/httpd.conf 

Add the following to the end of the httpd.conf file, just a few simple instructions instructing Apache to load the module.

# Add and load the mod_jk module
LoadModule jk_module libexec/httpd/mod_jk.so
AddModule mod_jk.c

We'll have to add some additional lines in a few minutes, but this is enough to instruct Apache to load the module upon startup.

Configuring mod_jk

Now that we have built and loaded mod_jk, we need to tell Apache and Tomcat how to enable the communication. Fortunately, the Tomcat developers already have part of the configuration set up, so we'll just have to take advantage of that and set up some very simple files.

Configuration of the Tomcat connector is done via "workers" that will enable the connection. Change directory to the Tomcat conf/ directory and create a new file (with your favorite text editor) called workers.properties. In this example, we will set up a simple worker -- please consult the Tomcat documentation for further instructions and examples. Here is the example properties file:

# Setup for Mac OS X
workers.tomcat_home=/usr/local/tomcat
workers.java_home=/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home

 ps=/
worker.list=ajp12, ajp13

# Definition for Ajp13 worker
#
worker.ajp13.port=8009
worker.ajp13.host=127.0.0.1
worker.ajp12.type=ajp13

Apache will need to find this file to load the resources and create the connection with the running version of Tomcat. To do this, we'll have to edit the same http.conf file that we edited earlier. This time, add the following lines to the end of the file (adjust the paths to something appropriate for your Tomcat installation):

JkWorkersFile /usr/local/tomcat/conf/jk/workers.properties JkLogFile /usr/local/tomcat/logs/mod_jk.log JkLogLevel debug

Before continuing, we can test the configuration to see if everything has been set up correctly. First, restart Tomcat, then Apache. You can restart Apache through the "Web Sharing" option in the System Preferences, or by opening a command line and entering:

sudo apachectl restart

If everything has been installed correctly, the tail of the Tomcat logs/catalina.out file should read:

Starting service Tomcat-Standalone Apache Tomcat/4.0.4 Starting service Tomcat-Apache Apache Tomcat/4.0.4

With this last step, Tomcat is now able to serve requests for servlets and JSPs from the Apache Web Server. In order to define how an external browser uses an application, we'll have to define a "Virtual Host" in the httpd.conf file. Virtual host configurations can be somewhat complicated, and often reflect how you want users to refer to your application. For example purposes, we'll define our virtual host to be the default "localhost," which is by default recognized at the IP address 127.0.0.1. (For further reading on creating virtual hosts in a real-world deployment environment, consult the book Apache: The Definitive Guide.)

To enable the virtual host, first check that the ServerName is set to the localhost "loopback" address. Alter the definition of ServerName to read:

ServerName 127.0.0.1 

(There is a bit of documentation in the httpd.conf file about how the ServerName works, and the prerequisites for defining server names.)

With the server name defined, we can define a simple virtual host that will serve as an entry to the "examples" application included with Tomcat. This virtual host can be added to the end of the file:

<VirtualHost 127.0.0.1> DocumentRoot
/usr/local/tomcat/webapps JkMount /*.jsp ajp12 JkMount
/examples/servlet/* ajp12 </VirtualHost> 

Related articles

Installing Tomcat on Mac OS X -- The Tomcat server is ideal for deploying Web applications and Web services. It's also a snap to install on Mac OS X. Here's how.

Using Tomcat -- a series of articles on ONJava.com by James Goodwill that features introductory Web application development issues, Tomcat installation and configuration, deploying Web applications onto Tomcat, Struts, and much more.

The only "trick" involved is using the JkMount directive, which instructs the mod_jk module to use a worker defined previously in the workers.properties file.

After defining the virtual host, restart Apache. The examples should now be accessible at the addresses http://localhost/examples/jsp/ and http://localhost/examples/servlets/, respectively. Be sure to follow the links to verify that Tomcat is answering requests. As a second test, you may wish to check the access logs for both Apache and Tomcat. The Apache server should be serving the requests for the static front pages for the examples, while Tomcat registers the access requests for the JSP and servlet applications. After this final check, we've now enabled our Mac to serve Java-based applications using the built in Apache Web Server!

Chad Thompson is a developer based in Des Moines, IA. His primary interests are creating Java and XML based software, development methodologies (esp. Extreme Programming), and a recent rabid fascination with Mac OS X.


Return to the Mac DevCenter.