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. http://java.sun.com/javase/downloads/index.jsp . 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 http://archive.apache.org/dist/tomcat/tomcat-5/ 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 startup.sh script and shutdown.sh script of tomcat. This can be done easily, open startup.sh file in vi or any text editor from %TOMCAT_HOME%/bin/startup.sh /opt/jdk is folder where actual java is installed. Or specify the java home directory path

#!/bin/sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
#
# $Id: startup.sh 385888 2006-03-14 21:04:40Z keith $
# -----------------------------------------------------------------------------

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

# resolve links - $0 may be a softlink
PRG="$0"

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

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

#!/bin/sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
#
# $Id: shutdown.sh 385888 2006-03-14 21:04:40Z keith $
# -----------------------------------------------------------------------------

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

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

PRGDIR=`dirname "$PRG"`
EXECUTABLE=catalina.sh

# 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
fi

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



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

%TOMCAT_HOME%/bin/startup.sh

e.g. /tomcatA/bin/startup.sh

And shutdown

%TOMCAT_HOME%/bin/shutdown.sh

e.g. /tomcatA/bin/shutdown.sh

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 http://archive.apache.org/dist/httpd/ . 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. mod_jk.so module
2. mod_jk2.so module

mod_jk2.so's development is stopped. So we are using mod_jk.so module. This can be downloaded from http://archive.apache.org/dist/jakarta/tomcat-connectors/.
Download binary format module.
Copy mok_jk.so 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

/usr/local/httpd/conf/httpd.conf

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/mod_foo.so
#

LoadModule jk_module modules/mod_jk.so
JkWorkersFile /usr/local/httpd/conf/workers.properties
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/workers.properties. This workers.properties file contains all information about workers (Tomcat), their ports connector Port AJP apache javaserv port. A sample file is given here.

workers.tomcat_home=/tomcat
workers.java_home=$JAVA_HOME
ps=/
worker.list=tomcat,loadbalancer

worker.tomcat.port=8009
worker.tomcat.host=192.168.1.80
worker.tomcat.type=ajp13
worker.tomcat.lbfactor=1

worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcat
worker.loadbalancer.sticky_session=1

Worker.tomcat.host=192.168.1.80 is tomcat server ipaddress or use computer name instead of ipaddress like this

Worker.tomcat.host=
gnulogsbox.

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 mod_jk.so 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/mod_jk.so into server: /usr/local/httpd/modules/mod_jk.so: 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 127.0.0.1 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. http://192.168.1.80/ this will open apache home page.


2 comments:

Anonymous said...

superb article dude!!

Unknown said...

Hi,Alternatively, some business owners might choose the least expensive option to save money, only to realize that they did in fact, get what they paid for. Doing your research and deciding on a Web Design Cochin firm based on a variety of factors is always the best bet. Thanks......