Published by admin on October 2, 2007 07:47 pm under Linux
It is possible to use Apache as the web listener for Tomcat. I have heard several reasons why people do this. The reason I did this is so that I can use Apache to handle security for my web application. This entry will describe the process of connecting Apache to Tomcat using mod_jk.
First you will need to download the latest source for the following applications:
Apache 2.2 (2.2.4 at the time of writing)
Tomcat 6 (6.0.26 at the time of writing)
mod_jk (1.2.22 at the time of writing)
Set the following environment variables. I would recommend placing them in your .bash_profile or .profile. If you place them in either of these files source the file immediately.
# Install Java
apt-get install sun-java6-jdk openjdk-6-jdk
sudo apt-get install sun-java6-jdk openjdk-6-jdk
add-apt-repository "deb http://archive.canonical.com/ubuntu lucid partner"
sudo su
java -version
# Tomcat env settings
export JAVA_OPTS="-Xms512m -Xmx1024m"
export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.26
export CATALINA_HOME=/home/davidq/tmp/apache-tomcat-6.0.32
JAVA_OPTS sets the minimum and maximum memory that will be available to the JVM running Tomcat. JAVA_HOME and CATALINA_HOME are used by several Tomcat scripts. This configuration will use the Sun JDK version 1.6. You can see that JAVA_HOME points to jdk1.6.0.
Extract the contents of each of the previous in a directory.
cd /home/yourusername/tmp
tar zxvf httpd-2.2.4.tar.gz
tar zxvf apache-tomcat-6.0.13.tar.gz -C /home/davidq/tmp
tar zxvf tomcat-connectors-1.2.22-src.tar.gz
The apache-tomcat-6.0.13 directory will be extracted to /home/davidq/tmp. You can extract it wherever you would like by changing the argument for -C. There is a tiny bit of configuration for Tomcat. vi /home/davidq/tmp/apache-tomcat-6.0.13/conf/tomcat-users.xml and place the following between the tomcat-users tags:
<role rolename="manager"/>
<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager,manager-gui"/>
cd to the httpd-2.2.4 directory
./configure –"prefix=/home/davidq/tmp/apache" –"with-included-apr" –"with-mpm=worker"
make
make install
Notice the –with-mpm=worker? We need this for mod_jk. There are some entries that need to be added to the httpd.conf for mpm worker. vi /home/davidq/tmp/apache/conf/httpd.conf and add the following beneath the “ServerRoot" block:
# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule worker.c>
ServerLimit 16
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
</IfModule>
You will want to change “Listen 80″ to “Listen 8079″ because you wont be able to start Apache on port 80 unless you are root. By default Tomcat listens on 8080.
Also in the httpd.conf file look for the LoadModule section and append the following to that section:
LoadModule jk_module modules/mod_jk.so
Include “/home/davidq/tmp/apache/conf/mod_jk.conf"
You will also need to configure Apache for your web application. Here are just some examples. They will be referenced again in the mod_jk section.
Alias /jktest /home/davidq/tmp/jktest
<Directory /home/davidq/tmp/jktest>
Options FollowSymLinks Includes
DirectoryIndex index.html
AddHandler server-parsed shtml
order allow,deny
allow from all
</Directory>
<Location /jktest/TestServlet>
<Limit POST>
Order deny,allow
Deny from all
Allow from localhost
Satisfy any
</Limit>
</Location>
The mod_jk connector needs to be installed now. Here are the steps:
cd to the tomcat-connectors-1.2.22-src/native directory
./configure –"with-apxs=/home/davidq/tmp/apache/bin/apxs"
make
make install
vi /home/davidq/tmp/apache/conf/mod_jk.conf and add the following:
<IfModule mod_jk.c>
JkShmFile /home/davidq/tmp/apache/logs/jk-runtime-status
JkLogFile /home/davidq/tmp/apache/logs/mod_jk.log
JkLogLevel info
JkWorkersFile /home/davidq/tmp/apache/conf/workers.properties
JkMount /jktest/*.jsp wkr01
JkMount /jktest/TestServlet wkr01
</IfModule>
vi /home/davidq/tmp/apache/conf/workers.properties and add the following:
# workers.properties – ajp13
#
# List workers
worker.list=wkr01
#
# Define ajp13 worker
worker.wkr01.type=ajp13
worker.wkr01.host=localhost
worker.wkr01.port=8009
worker.wkr01.connection_pool_size=25
worker.wkr01.connection_pool_minsize=13
We need to create a test jsp file so that we can ensure the connector is working. First make a directory /home/davidq/tmp/jktest. After the directory is created, vi /home/davidq/tmp/jktest/testfile.jsp and paste the following in it:
<HTML>
<HEAD>
<TITLE>mod_jk test</TITLE>
</HEAD>
<BODY>
<%out.println("Hello World");%> !
</BODY>
</HTML>
Now we need to create a context file for our application. vi /home/davidq/tmp/jktest/jktest.xml and paste the following in it:
<?xml version=’1.0′ encoding=’utf-8′?>
<Context displayName="JkTest" docBase="/home/davidq/tmp/jktest" path="/jktest" workDir="work/Catalina/localhost/jktest">
</Context>
To start Tomcat and Apache run the following:
/home/davidq/tmp/apache-tomcat-6.0.13/bin/startup.sh
/home/davidq/tmp/apache/bin/apachectl start
With Tomcat started you will need to configure the jktest context. Enter the following URL in your browser. When you are prompted for a username and password, enter tomcat for the username and s3cret for the password.
http://yourmachine:8080/manager/html
After logging in look to the bottom of the page for a section named “Deploy". Fill in the following fields and click Deploy.
Context Path (optional): /jktest
XML Configuration file URL: file:/home/davidq/tmp/jktest/jktest.xml
WAR or Directory URL: LEAVE BLANK
Now all you need to do is test. You should be able to access testfile.jsp via the following URL:
http://yourservername.domain.com:8079/jktest/testfile.jsp