Monday, 21 March 2016

How to Connect Tomcat 6 to Apache HTTP Server 2 ?


Tomcat can be run as a standalone server. Tomcat can also be run as an add-on to the Apache HTTP Server.
In this combination, Tomcat executes the Java servlets and JSPs, the Apache serves the static HTML pages and performs other server-side functions such as CGI, PHP, SSI, etc.

To run Tomcat together with Apache : 
  • Apache needs to load a "adapter" module, 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).
  • When Apache receives an HTTP request, it checks if the request belongs to Tomcat. If so, it lets the adapter takes the request and forwards it to Tomcat.
Prerequisites
  • Install Apache HTTP Server
  • Install Tomcat (Assume : it run on port 8080 and contains 2 web contexts: "/examples" and "/ws")
  • Download the Apache-Tomcat Connector Module


Configure Apache
We need to configure the Apache HTTP Server to load and initialize the JK module.
Create a configuration file called "mod_jk" as follows : 
# Load mod_jk module
# Update this path to match your modules location
LoadModule jk_module modules/mod_jk.so
# Where to find workers.properties
# Update this path to match your conf directory location
JkWorkersFile d:/myproject/tomcat/conf/workers.properties
# Where to put jk logs
# Update this path to match your logs directory location
JkLogFile d:/myproject/tomcat/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# JkOptions indicate to send SSL KEY SIZE
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send everything for context /ws to worker ajp13
JkMount /ws ajp13
JkMount /ws/* ajp13
# Send everything for context /examples to worker ajp13
JkMount /examples ajp13
JkMount /examples/* ajp13

For each web context that is to be forwarded from Apache to Tomcat, include two JKMount statements as shown.
In the above configuration, Apache forwards all requests to web contexts "/examples" and "/ws" to Tomcat, via a "worker" called "ajp13".
(Check the URL of the Tomcat's servlet and JSP examples from the Tomcat's welcome page! It may move!)

Include the above configuration directives into the Apache's configuration

Next, observe that the configuration refers to a worker file called "workers.properties", and forward certain requests to a JK worker called "ajp13".
# Define 1 real worker named ajp13
worker.list=ajp13
# Set properties for worker named ajp13 to use ajp13 protocol, and run on port 8009
worker.ajp13.type=ajp13
worker.ajp13.host=localhost
worker.ajp13.port=8009
worker.ajp13.lbfactor=50
worker.ajp13.cachesize=10
worker.ajp13.cache_timeout=600
worker.ajp13.socket_keepalive=1
worker.ajp13.socket_timeout=300

Note: The JKMount statements forward the requests to a worker called "ajp13", which is defined in this "workers.properties".


Configure Tomcat
The default configuration in Tomcat's "conf\server.xml" starts the AJP1.3 service via the following configuration, on TCP port 8009
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />


Start the Apache with the JK module
# service httpd start
Check the Apache's log "logs\errors.log" to confirm that JK module was started


Start the Tomcat server
Observe that AJP1.3 service is initiated and the ajp13 worker is listening at port 8009.
....Oct 1, xxxx 9:44:05 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009Oct 1, xxxx 9:44:05 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/18 config=null....

Note : The order of starting up Tomcat and Apache is NOT important. Either apache or tomcat can be restarted at any time.


Verify the Installation
Issue the following URLs to access the web contexts "/examples" and "/ws", that are defined in Tomcat (running in port 8080), but accessed via the Apache (running in port 7000).

http://hostname:7000/examples/servlets  ---- Try Tomcat's servlet examples via Apache
http://hostname:7000/examples/jsp   ---- Try Tomcat's JSP examples via Apache
http://hostname:7000/examples  ---- Try Tomcat's examples via Apache
http://hostname:8080/ws  ---- Access ws from Tomcat directly
http://hostname:7000/ws   ----- Access ws via Apache

No comments:

Post a Comment

Note: only a member of this blog may post a comment.