In production server we let apache handle the static HTML content and other server-side fuctions PHP so that tomcat could process dynamic content(Java servlets and JSPs). Also by connecting apache with tomcat we can also do clustring that is apache will act as entry point for multiple tomcat servers For this to be possible apache need to load a “adapter” module(mod_jk), which uses a certain protocol, such as Apache JServ Protocol (AJP), to communicate with the Tomcat, via another TCP port (port 8009 in the default configuration)
Lets see the steps to configure apache with tomcat. I assume you have previously installed apache and tomcat(if these are not installed,check my previous posts).
I have a app deployed on tomcat under webapps folder, which i could access through following url:
As you can see port tomcat is serving the page on port 8080 in the URL, this is default tomcat port.
Installation and configuration of the mod_jk connector:
The mod_jk connector is an Apache HTTPD module that allows apache to communicate with Apache Tomcat instances over the AJP protocol. The module is used in conjunction with Tomcat’s AJP Connector component.
Download appropriate version of mod_jk , I have Apache2.2 so i am installing mod_jk-apache-2.2.4 from https://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.23/mod_jk-apache-2.2.4.so
Copy this module to /etc/httpd/modules, this is where all apache modules are present(The location of this directory can vary from platform to platform. Rename to mod_jk.so before putting it in your Apache2.2/modules directory. ).
Below is the config that is required on apache httpd.conf file ,ofcourse you can create separate mod_jk.conf file and inlcude that.
# Load the mod_jk module.
LoadModule jk_module path/to/mod_jk.so
# Set path to workers.properties.
# Set path to jk shared memory. Generally, you'll want this to point to your local state or logs directory.
# Set path to jk logs. This path should point to the same logs directory as the HTTPD access_log.
# Set the jk log level.Valid values for this setting are 'debug', 'error', or 'info'.
# Set timestamp log format. Use provided variables to customize.
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# Map a worker to a namespace. Workers represent Tomcat instances that are listening for requests.
JkMount /shoppingcart/* worker1
Next is to create a workers.properties in apache.The workers.properties file defines a list of Tomcat ‘workers’ to which Apache HTTPD can pass requests.In Centos its inside /etc/httpd/conf/.
# Define 1 real worker using ajp13
# Set properties for worker (ajp13)
# Set the host for the worker
# AJP connector port
After doing apache configuration next thing to do is to change tomcat server.xml. You can find this file under tomcat_home/conf/server.xml. Default configuration in tomcat server.xml starts the AJP1.3 service via the following configuration, on TCP port 8009, uncomment this if this is commented:
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
After configuring both Apache and Tomcat we need to do restart them. If there’s no error in the configuration you will be able to access tomcat through apache. In below image, i am able to access the tomcat app from apache.
A number of other modules have been created in the course of Tomcat’s life that provide mod_jk-like functionality. Many of these are now deprecated.Using the following modules should be avoided if at all possible: