Monday, March 29, 2010

Apache Tomcat Integration

Introduction tomcat and Apache web server

Apache httpd is very fastest web server for static contents. Apache httpd web server has biggest market share of more than 60% in web server in world. Today apache httpd web server supports dynamic contents through module connectors. It supports php, cgi, perl and many more scripting languages. Apache is widely used in the world and it is freely available for download. It is open source code by apache projects. Apache's biggest advantage is platform independent, can work on any platform unix, linux and windows without any problems. Apache is not only platform independent, it is very easy to configure and very easy to use. Tomcat is another open source project from apache. Tomcat is java based web server, which support java servlet, and servlet side scripting. Java is platform independent and day by day getting popularity in market.

Java has advantage of security and more secure than other programming languages. Servlet has advantage of precompiled and need not to compile on every request send by client on browser. All servlets and JSP runs under Java virtual machine. Tomcat is freely available from apache, and java is available from sun. Apache tomcat integration achieve through mod_jk connectors. It can communicate through AJP13 apache jserv protocol 13. This mo_jk connector interacts with tomcat web server and apache httpd, and send dynamic request to tomcat web server and response back to client. All static content handled by httpd server only. Let’s start integration of tomcat and apache step by step.

Installation of JDK in linux

Tomcat doesn't run alone, it needs java runtime environment JRE or JVM for running jsp and servlet pages. JDK is freely available from sun. JDK can be downloaded from sun's website. . Download latest version of jdk or sdk and install on the machine. Sun provides different versions of jdk for different platform. There are different versions available from sun for linux, windows or sun solaris. Tomcat 5.5 is work only with JDK 1.5 version, so use at least jdk 1.5 versions. Installing JDK on linux is not difficult work; this can be done by double click on bin format file or simple RPM. If you are installing JDK from rpm then you can run this command to install.
rpm -ivh jdk-1_5_0_07-nb-5_0-linux-ml.rpm

If you are installing JDK from bin file you can run this command to install, and also change permissions to 777 to bin file.
sh jdk-1_5_0_07-nb-5_0-linux-ml.bin.

JDK installed at default path /opt directory in linux fedora core 5.

Installation Tomcat on linux
see more Installing tomcat on Windows

After successful installation of JDK, install tomcat. Tomcat is freely available from apache's website. Download latest version of tomcat from apache website. Tomcat is distributed in binary and source code. Binary code is precompiled of jar files and easy to install, and source code have to compile and install manually. Installation through source code need ant, this will compile automatically all files in sub directory or packages. Tomcat can be downloaded from Download tomcat in tar.gz format for Linux or any RPM form file and executable file or zipped file for windows platform. Unzip tar file
tar -xvf apache-tomcat-5.5.17.tar.gz

Copy this tomcat in root or anywhere. We used to install tomcat at root / in linux.

Tomcat need to know where is java installed on system, where to compile servlets and beans. Now set environment variables for java, jre and classpath for servlet-api. This can be done by export command manully. Export will keep environment setting until system is not shutdown or restarted. When system get restarted all environment setting will lost.
export JAVA_HOME=/opt/jdk

or we can call this automatically by putting export command in script and script of tomcat. This can be done easily, open file in vi or any text editor from %TOMCAT_HOME%/bin/ /opt/jdk is folder where actual java is installed. Or specify the java home directory path

# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# $Id: 385888 2006-03-14 21:04:40Z keith $
# -----------------------------------------------------------------------------

# Better OS/400 detection: see Bugzilla 31132
export JAVA_HOME=/opt/jdk/
case "`uname`" in
CYGWIN*) cygwin=true;;
OS400*) os400=true;;
Darwin*) darwin=true;;

# resolve links - $0 may be a softlink

while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG=`dirname "$PRG"`/"$link"

It should be also defined in script of tomcat. Open script in any text editor, and include export command in script.

# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# $Id: 385888 2006-03-14 21:04:40Z keith $
# -----------------------------------------------------------------------------

# resolve links - $0 may be a softlink
export JAVA_HOME=/opt/jdk

while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG=`dirname "$PRG"`/"$link"

PRGDIR=`dirname "$PRG"`

# Check that target executable exists
if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
echo "Cannot find $PRGDIR/$EXECUTABLE"
echo "This file is needed to run this program"
exit 1

exec "$PRGDIR"/"$EXECUTABLE" stop "$@"

Test tomcat is installed properly. Run this command to startup tomcat through linux terminal.


e.g. /tomcatA/bin/

And shutdown


e.g. /tomcatA/bin/

If tomcat is throwing error, check logs folder %TOMCAT_HOME%/logs/catalina.out. If your tomcat does not have logs folder make it manually and restart tomcat again.

Open web browser and in address bar write http://localhost:8080/. If it opens apache's page, means tomcat is working properly.

Now you need to configure servlets and application context path. Servlet invoker is default invoker to load servlet into tomcat memory. No need to describe, any parameters in web.xml. Servlet and port configuration setting go through Servlet invoker
Apache installation

see more Installing Apache on Windows

Apache httpd also open source project by apache group. Apache easily available from . Download stable tar.gz versions of apache httpd. Apache httpd 2.0 more stable version then other versions.

First you have to remove previous installed httpd from your system. Otherwise it will conflict with new installed httpd services.

Commands to remove default rpm of httpd(apache).

Find your installed rpm packages in system by
rpm -qa | grep -i httpd

uninstall rpm
rpm -e packageName

e.g. rpm -e httpd-2.2.0-5.1.2
Uncompress downloaded httpd-2.0.55.tar.gz by tar command
tar -xvf httpd-2.0.55.tar.gz

Command to install apache manually.
Go in directory where apache is unzipped

[root@clustering ~] cd /httpd
[root@ clustering httpd]./configure --prefix=/usr/local/httpd
[root@ clustering httpd]make
[root@ clustering httpd] make install

Apache is now installed if in last it doesn't show any error.
Test apache by starting apache
[root@ clustering httpd] /usr/local/httpd/bin/apachect1 start

If it is started correctly you can check it on browser by opening localhost address bar.
Mod_jk Connector

This is very important step of tomcat apache integration. Mod_jk is connector module which helps to make connection between tomcat and apache web server. The request come from apache and apache web server send this request to tomcat web server through mod_jk. There are two type of mod_jk available

1. module
2. module's development is stopped. So we are using module. This can be downloaded from
Download binary format module.
Copy module in modules directory of apache web server
e.g. C:\apache\Apache2\modules
or in linux

e.g. /usr/local/httpd/modules
Configuration Tomcat and Apache

Apache Tomcat Configuration, Apache web server use a configuration file, httpd.conf. This file is in folder


Or %httpd%/conf/httpd.conf. Open this file in any text editor notepad or vi editor and do editing as show in this file.

Listen 80

# Dynamic Shared Object (DSO) Support
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
# Example:
# LoadModule foo_module modules/

LoadModule jk_module modules/
JkWorkersFile /usr/local/httpd/conf/
JkLogFile "logs/mod_jk.log"
JkLogLevel error
JkMount /gnulogsbox loadbalancer
JkMount /gnulogsbox/* loadbalancer

# ExtendedStatus controls whether Apache will generate "full" status
# information (ExtendedStatus On) or just basic information (ExtendedStatus
# Off) when the "server-status" handler is called. The default is Off.
#ExtendedStatus On

### Section 2: 'Main' server configuration
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
# definition. These values also provide defaults for
# any containers you may define later in the file.
# All of these directives may appear inside containers,
# in which case these default settings will be overridden for the
# virtual host being defined.

# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.

Add this highlighted line in httpd.conf file. Create a file in /usr/local/httpd/log/ mod_jk.log. This log file refers to error thrown by apache web server. Errors can be read from this log file.

Create new file in /usr/local/httpd/conf/ This file contains all information about workers (Tomcat), their ports connector Port AJP apache javaserv port. A sample file is given here.



worker.loadbalancer.sticky_session=1 is tomcat server ipaddress or use computer name instead of ipaddress like this

Now It is almost done. Just stop apache web server and again start. To start apache web server manually, should use command.
[root@ clustering httpd] /usr/local/httpd/bin/apachect1 stop

And start apache web server now
[root@ clustering httpd] /usr/local/httpd/bin/apachect1 start

If apache starts again without error, this means all things are going in right way. If apache doesn't start and throw an error then you need to change connector according to your server hardware configuration. Download another mod_jk connector and again copy to module folder and stop and start apache httpd server. Otherwise, again and again find suitable connector for your hardware and server configuration, test it again apache. Error may be this kind of

[root@clustering httpd]# /usr/local/httpd/bin/apachectl start
Syntax error on line 234 of /usr/local/httpd/conf/httpd.conf:
Cannot load /usr/local/httpd/modules/ into server: /usr/local/httpd/modules/ invalid ELF header

When apache httpd server start without any error,

[root@clustering httpd]# /usr/local/httpd/bin/apachectl start
httpd: Could not determine the server's fully qualified domain name, using for ServerName

This is error free startup of apache.
Run Jsp and servlet

Now start tomcat server. After startup of tomcat server, you can test simple jsp page. this will open apache home page.