Integrating Tomcat and Apache Together

Introduction


Here are quick and easy instructions to integrating Tomcat and Apache together in Linux that allows the :8080 (or other port number) to not show in the URL. I have seen other how-to's about this, but they always over complicate the instructions or they're missing vital steps.

Prerequisites


The steps in this article have been tested with the following versions:

  • Tomcat 5.5.27
  • Apache 2.2.8
  • mod_jk 1.2.27
  • Java 6

The following are assumed:

  • You have tomcat and apache already installed on your system.
  • Apache is running on port 80.
  • Tomcat is running on port 8080.
  • You have java installed.

mod_jk

  • Download mod_jk. You can either build it with the source code or you can just get the binary file. I chose to get the binary file.
       wget http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.27/i386/mod_jk-1.2.27-httpd-2.2.6.so
    
  • I've renamed mod_jk-1.2.27-httpd-2.2.6.so to mod_jk-1.2.27.so for the purposes of this article. You can rename it to whatever you want.

  • Put mod_jk-1.2.27.so in the apache modules directory on your system. Mine is located in /usr/lib/apache2/modules.
       mv mod_jk-1.2.27.so /usr/lib/apache2/modules
    

workers.properties File

  • Create the workers.properties in your apache configuration directory. Example:
       touch /etc/apache2/workers.properties
    
  • Using your favorite editor (I like to use vi), edit the file and add the following information (tailor it to suit your system):

       workers.tomcat_home=/opt/tomcat5
       workers.java_home=/opt/java
       ps=/
       worker.list=worker1
    
       worker.worker1.port=8009
       worker.worker1.host=127.0.0.1
       worker.worker1.type=ajp13
       worker.worker1.lbfactor=1
    
  • Detailed explanation of each of the mod_jk properties in the workers.properties file can be found here: workers.properties.

tomcat.conf File


I like to keep specific configurations of components of apache separated. Here, I have created a file called tomcat.conf which is loaded by apache2.conf (or httpd.conf) file during initial startup. Here is the contents of tomcat.conf (tailor it to suit your system):

    ######################################################################
    # Allows apache and tomcat to work together.
    #
    # Load mod_jk module.
    LoadModule jk_module /usr/lib/apache2/modules/mod_jk-1.2.27.so
    
    # Where to find workers.properties
    JkWorkersFile /etc/apache2/workers.properties
    
    # Where to put jk logs
    JkLogFile /var/log/apache2/mod_jk.log
    
    # Set the jk log level [debug/error/info]
    JkLogLevel error
    
    # Select the log format
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
    
    # JkRequestLogFormat set the request format
    JkRequestLogFormat "%w %V %T"
    
    # JkOptions 
    JkOptions +ForwardKeySize -ForwardDirectories
    
    ######################################################################
    

An explanation of each of the jk options can be found here: Apache jk Options.

Virtual Host File


This is probably the most vital step that is missing in a lot of how-to's. The JkMount directive must be added to the virtual host file containing port 80. The virtual host definition may also be present in your apache2.conf or httpd.conf file. If not, create a new file called something like default.conf, add the contents below, and put default.conf somewhere in which it can be properly loaded by apache2.conf (or httpd.conf).

    <VirtualHost *:80>
       ServerName your.hostname.com
    
       # Send servlet for context /jsp-examples to worker named worker1
       JkMount /jsp-examples worker1
    
       # Send JSPs for context /jsp-examples/* to worker named worker1
       JkMount /jsp-examples/* worker1
    </VirtualHost>
    

Test It


To test your setup, type http://localhost/jsp-examples. It should also work with http://localhost:8080/jsp-examples.