Howtos Apache Tomcat with Mod JK
From 5dollarwhitebox.org Media Wiki
The following is a basic howto on configuring Apache Tomcat, Sun JDK, and the Mod_JK connector. This is not very advanced, but is a good starting point to get you going.
This howto assumes that you have a working Apache installation already.
- Author: BJ Dierkes
- Contact: wdierkes [at] 5dollarwhitebox [dot] org
- Created: May 23, 2006
- Last Edited: Aug 21, 2006
Contents |
New RPMs for Tomcat/Mod_JK
I have built new RPMs for Tomcat and Mod_JK which makes this howto virtually unnecessary. Please try the RPMs first:
- Tomcat-5.5.17: http://rpms.5dollarwhitebox.org/clean/tomcat5
- Mod_JK-1.2.18: http://rpms.5dollarwhitebox.org/clean/mod_jk
Tomcat Quick Reference
There might be some other stuff you are looking for here: Tomcat_Quick_Reference.
Tomcat installation
Download Tomcat
Download the latest release from tomcat.apache.org. I am using 5.5.17. Get the CORE and Admin pachages:
linuxbox /]# tar -zxvf apache-tomcat-5.5.17.tar.gz linuxbox /]# tar -zxvf apache-tomcat-5.5.17-admin.tar.gz linuxbox /]# mv apache-tomcat-5.5.17 /opt
Extracting the 'Admin' package from the same dir as you extracted the core package should automatically go into the proper directory of 'apache-tomcat-5.5.17/server/webapps/admin', if not copy the files there.
Download the Sun JDK
Get the appropriate JDK for this version of Tomcat. In this case we are using the self extracting binary of the Sun JDK version 5.0 (1.5.0):
linuxbox /]# chmod +x jdk-1_5_0_08-linux-i586.bin linuxbox /]# ./jdk-1_5_0_08-linux-i586.bin
NOTE: If you are using an RPM based distribution such as RedHat/CentOS/Fedora you should use the 'RPM' version of the self extracting binary. The binary extracts the files after you hit 'yes' to the agreement.
Move the JDK to '/opt':
linuxbox /]# mv jdk1.5.0_08 /opt
This is only if you are not using the RPM version. The RPM version will install the JDK to '/usr/java/jdk1.5.0_08'.
You should now have the following:
- /opt/apache-tomcat-5.5.17
- /opt/jdk1.5.0_08
OR if you installed the RPM version '/opt/jdk1.5.0_08' will not exist, and you will have '/usr/java/jdk1.5.0_08'.
Add the tomcat user and set permissions
We want to run tomcat as a non-privileged user, so go ahead and add that now:
linuxbox /]# groupadd tomcat linuxbox /]# useradd -g tomcat -d /opt/apache-tomcat-5.5.17 linuxbox /]# mkdir /var/run/tomcat5 linuxbox /]# chown -R tomcat /var/run/tomcat5 linuxbox /]# chgrp -R tomcat /opt/tomcat-5.5.17 linuxbox /]# chmod -R g+r /opt/tomcat-5.5.17 linuxbox /]# chown -R tomcat /opt/tomcat-5.5.17/work linuxbox /]# chown -R tomcat /opt/tomcat-5.5.17/temp linuxbox /]# chown -R tomcat /opt/tomcat-5.5.17/logs linuxbox /]# chown -R tomcat /opt/tomcat-5.5.17/webapps
Configure The Startup Script
In order to start Tomcat, we need to properly configure the startup script. We can simply just use the catalina.sh, however because we are wrapping it to startup with the 'tomcat' user, we will be using our own INIT script.
Download the sample INIT script I've written: http://5dollarwhitebox.org/media/downloads/tomcat5.init
Then copy it to '/etc/init.d/tomcat5':
linuxbox /]# wget http://5dollarwhitebox.org/media/downloads/tomcat5.init linuxbox /]# mv tomcat5.init /etc/init.d/tomcat5
Using your favorite editor, modify the following in '/etc/init.d/tomcat5':
... export CATALINA_HOME='/opt/apache-tomcat-5.5.17' export CATALINA_BASE=$CATALINA_HOME export BASEDIR=$CATALINA_HOME export CATALINA_PID='/var/run/tomcat5/tomcat-5.5.17.pid' export JAVA_HOME='/usr/java/jdk1.5.0_08' ...
NOTE: Be certain to adjust the Environment variables to match your versions/locations etc.
Make the following scripts executable:
linuxbox /]# chmod +x /etc/init.d/tomcat5 linuxbox /]# chmod +x /opt/apache-tomcat-5.5.17/bin/catalina.sh linuxbox /]# chmod +x /opt/apache-tomcat-5.5.17/bin/setclasspath.sh
Add Tomcat to chkconfig:
linuxbox /]# chkconfig --add tomcat5 linuxbox /]# chkconfig tomcat5 on
Secure the Shutdown port, vi '/opt/apache-tomcat-5.5.17/conf/server.xml' and modify the following line:
<Server port="8005" shutdown="SHUTDOWN">
Change SHUTDOWN to something else... anything random
Start Tomcat:
linuxbox /]# /etc/init.d/tomcat5 start
Check out Tomcat
You should be able to hit Tomcat at the default IP:8080...
Tomcat Manager Panel
Tomcat includes the Manger Panel to control the service. Do the following in order to make it useful:
Create the Admin User
Using your favorite editor, open '/opt/apache-tomcat-5.5.17/conf/tomcat-users.xml' and add the following:
<role rolename="admin"/> <role rolename="manager"/> <role rolename="tomcat"/> <user username="admin" password="password" roles="tomcat,manager,admin"/>
Change 'password' to the password you want to use.
You can now access the manager at 'http://192.168.1.100:8080/manager'.
The Mod_JK connector
Install from RPM
I have recently built mod_jk RPMs which can be found here: http://rpms.5dollarwhitebox.org/clean/mod_jk
linuxbox /]# wget http://rpms.5dollarwhitebox.org/clean/mod_jk/mod_jk-1.2.18-1.rhel4.bjd.i386/mod_jk-1.2.18-1.rhel4.bjd.i386.rpm linuxbox /]# rpm -Uvh mod_jk-1.2.18-1.rhel4.bjd.i386.rpm Preparing... ########################################### [100%] 1:mod_jk ########################################### [100%]
Don't copy and past that wget line... cause you might not get the latest version.
Installing Mod_JK from source
In order to interface Apache with Tomcat, we install mod_jk. You need to get the mod_jk module and install it first:
For our example we are downloading the JK 1.2 Source Release:
linuxbox #] wget http://apache.gr-linux.com/tomcat/tomcat-connectors/jk/source/jk-1.2.15/jakarta-tomcat-connectors-1.2.15-src.tar.gz linuxbox #] tar -zxvf jakarta-tomcat-connectors-1.2.15-src.tar.gz linuxbox #] cd jakarta-tomcat-connectors-1.2.15-src/jk/native linuxbox #] ./configure --with-apxs=/usr/sbin/apxs (or where ever the apxs/apxs2 is) linuxbox #] make linuxbox #] make install
Note that if '/usr/sbin/apxs' doesn't exist, you may need to install the httpd-devel package.
Configure Mod_JK
Using your favorite editor, open '/etc/httpd/conf/workers.properties' and add the following:
worker.list=tomcat worker.tomcat.host=localhost worker.tomcat.port=8009 worker.tomcat.type=ajp13
Open '/etc/httpd/conf.d/mod_jk.conf' and add the following:
LoadModule jk_module modules/mod_jk.so JkWorkersFile "/etc/httpd/conf/workers.properties" JkLogFile "/var/log/httpd/mod_jk.log" JkLogLevel info
Mount the Tomcat WebApp
In order to access a WebApp from Tomcat, you need to mount it within an Apache Vhost. The following is an example Vhost:
<VirtualHost 192.168.1.100:80>
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot '/var/www/vhosts/domain.com'
JkMount /jsp-examples/* tomcat
</VirtualHost>
This will connect you from "http://www.domain.com/jsp-examples/<webapp_name>" to "http://www.domain.com:8080/jsp-examples/".
Conclusion
Thats it really. Please email me for corrections, and if I could add anything.
Reference
Tomcat:
Sun JDK (J2SE):
Mod_JK:
