Apache webserver integration with tomcat

Apache webserver integration with tomcat

How to integrate tomcat in apache webserver and load balance the requests to two tomcat instances.
_____________________________________________________________________
Configure the JK Module in httpd.conf
Download the mod_jk.so for apache website suitable for your apache installed and tomcat as well.

Edit the Apache server’s configuration file httpd.conf which is located in the /usr/apache2 /conf directory.

# Load mod_jk

#

LoadModule jk_module modules/mod_jk.so

#

# Configure mod_jk

#

JkWorkersFile conf/workers.properties

JkLogFile logs/mod_jk.log

JkLogLevel info

NOTE : You will need to change mod_jk.so to mod_jk.dll for Windows.

Below the “DocumentRoot” line, insert the following two lines:

JkMount /*.jsp loadbalancer

JkMount /servlet/* loadbalancer

Create the workers.properties file

Now we will create a file called workers.properties, and we will place it under /usr/apache2/conf. The workers.properties file tells Apache about the various Tomcat servers that are running, and on which port they are listening.

In my setup, I installed the two Tomcat servers in different directories, on the same machine as Apache. Feel free to put your Tomcat servers on different machines.

I made the first Tomcat server’s AJP13 connector listen on port 8801 instead of the default port which is 8009, and the second one listens on port 8802.

I have decided to name my tomcat servers tomcat1 and tomcat2.

Create the file exactly like this:

#

# workers.properties

#

# list the workers by name

worker.list=tomcat1, tomcat2, loadbalancer

# ————————

# First tomcat server

# ————————

worker.tomcat1.port=8801

worker.tomcat1.host=localhost

worker.tomcat1.type=ajp13

# Specify the size of the open connection cache.

#worker.tomcat1.cachesize

#

# Specifies the load balance factor when used with

# a load balancing worker.

# Note:

#  —-> lbfactor must be > 0

#  —-> Low lbfactor means less work done by the worker.

worker.tomcat1.lbfactor=100

# ————————

# Second tomcat server

# ————————

worker.tomcat2.port=8802

worker.tomcat2.host=localhost

worker.tomcat2.type=ajp13

# Specify the size of the open connection cache.

#worker.tomcat2.cachesize

#

# Specifies the load balance factor when used with

# a load balancing worker.

# Note:

#  —-> lbfactor must be > 0

#  —-> Low lbfactor means less work done by the worker.

worker.tomcat2.lbfactor=100

# ————————

# Load Balancer worker

# ————————

#

# The loadbalancer (type lb) worker performs weighted round-robin

# load balancing with sticky sessions.

# Note:

#  —-> If a worker dies, the load balancer will check its state

#        once in a while. Until then all work is redirected to peer

#        worker.

worker.loadbalancer.type=lb

worker.loadbalancer.balanced_workers=tomcat1, tomcat2

#

# END workers.properties

#

That’s it, we’re done with Apache.

Install and Configure the Tomcat Servers

Now let’s suppose that Java 1.4.x is installed under /usr/local/jdk1.4.x/. Create two Tomcat 4.x servers and install them under /usr/local/:

   tar fvxz jakarta-tomcat-4.x.tar.gz

   mv jakarta-tomcat-4.x /usr/local/tomcat1

   cp -R /usr/local/tomcat1 /usr/local/tomcat2

In both /usr/local/tomcat1 and /usr/local/tomcat2, the same files will be modified. I here by present the modifications made to the files contained in the /usr/local/tomcat1 directory tree structure. You should also apply the same changes to the corresponding files located under the /usr/local/tomcat2 directory tree structure.

Modify catalina.sh

We can set the JAVA_HOME and CATALINA_HOME envoirment variables directly in the catalina.sh file.

At line 32, before the “# —– Verify and Set Required Environment Variables ” line, insert the following two lines:

    JAVA_HOME=/usr/local/jdk1.4 ; export JAVA_HOME

    CATALINA_HOME=/usr/local/tomcat1 ; export CATALINA_HOME

(Set CATALINA_HOME  to /usr/local/tomcat2 in  /usr/local/tomcat2/conf/catalina.sh)

Modify conf/server.xml

Add a unique jvmRoute to the Catalina engine

Near line 100, replace:

    <Engine defaultHost=”localhost” debug=”0″>

with:

    <EnginejvmRoute=”tomcat1″name=”Standalone” defaultHost=”localhost” debug=”0″>

For tomcat2, put jvmRoute=”tomcat2″.

Change the control port

At line 13, replace:

    <Server port=”8005″

with:

    <Server port=”8811″

For the tomcat2 server, replace port 8005 with 8812. This will prevent the two servers from conflicting.

Change the AJP13 port

At line 75, in the AJP 13 connector definition, replace:

port=”8009″

with:

    port=”8801″

For the tomcat2 server, replace port 8009 with 8802.

Disable the standalone HTTP port

We don’t want or need our tomcat servers to directly respond to HTTP requests. So we comment out the Http Connector section between lines and 58 in the server.xml file.

Example:

<!– Define a non-SSL HTTP/1.1 Connector on port 8080 –>

<!–

    <Connector className=”org.apache.catalina.connector.http.HttpConnector”

               port=”8080″ minProcessors=”5″ maxProcessors=”75″

               enableLookups=”true” redirectPort=”8443″

               acceptCount=”10″ debug=”0″ connectionTimeout=”60000″/>

–>  

NOTE: If you don’t comment this out, you will need to change the port numbers so they don’t conflict between tomcat instances.

Disable the WARP connector

At line 314, comment out the <Connector…WarpConnector…> tag.

Example:

<Service>

<!–

    <Connector className=”org.apache.catalina.connector.warp.WarpConnector”

     port=”8008″ minProcessors=”5″ maxProcessors=”75″

     enableLookups=”true” appBase=”webapps”

     acceptCount=”10″ debug=”0″/>

–>

Do not forget to do the same thing to tomcat2′s server.xml file.

NOTE: You might want to comment out the entire <Service> element. If so, make sure and remove the comments within it – XML doesn’t like comments within comments.

Create test JSP pages ( index.jsp )

Create a file named index.jsp and put it in the /usr/local/tomcat1/webapps/ROOT directory:

<html>

<body bgcolor=”green”>

<center>

<%= request.getSession().getId() %>

<h1>Tomcat 1</h1>

</body>

</html>

Create a file named index.jsp and put it in the /usr/local/tomcat2/webapps/ROOT directory:

<html>

<body bgcolor=”yellow”>

<center>

<%= request.getSession().getId() %>

<h1>Tomcat 2</h1>

</body>

</html>

Start Tomcat1, Tomcat2 and Apache

    /usr/local/tomcat1/bin/startup.sh

    /usr/local/tomcat2/bin/startup.sh

    /usr/local/apache2/bin/apachectl start

Test your Installation

Now is the time to test your setup. First, verify that Apache serves static content.

Click on: http://localhost/ . You should see the default Apache index.html page.

Now test that tomcat (either Tomcat 1 or Tomcat 2) is serving Java Server Pages.

Click on: http://localhost/index.jsp

If you get a green page, the page was served by the tomcat1 server, and if you get a yellow page, it was served by the tomcat2 server.

Now test that session affinity – also known as sticky sessions – works within the load balancer. Hit the reload button of your web browser several times and verify that the index.jsp page you get is always received from the same tomcat server.

Configuring Private JVMs

If you don’t need load-balancing, but you are interested in configuring Apache/Tomcat for private Tomcat instances, you can add one of the following near the end of httpd.conf:

Name-based (1 IP address or NIC).

NameVirtualHost *

<VirtualHost *>

ServerName localhost1

JkMount /*.jsp tomcat1

JkMount /servlet/* tomcat1

</VirtualHost>

<VirtualHost *>

ServerName localhost2

JkMount /*.jsp tomcat2

JkMount /servlet/* tomcat2

</VirtualHost>

IP-based (different IP for each site).

# First  Virtual Host.

#

<VirtualHost 172.16.19.56:80>

ServerName localhost

JkMount /*.jsp tomcat1

JkMount /servlet/* tomcat1

</VirtualHost>

# Second  Virtual Host.

#

<VirtualHost 172.16.19.58:80>

ServerName localhost2

JkMount /*.jsp tomcat2

JkMount /servlet/* tomcat2

</VirtualHost>

Where the  serverNames are fully-qualified host names in a DNS Server. More information can be found at http://httpd.apache.org/docs-2.0/vhosts/ .

NOTE:

When using SSL with multiple Virtual Hosts, you must use an ip-based configuration. This is because SSL requires you to configure a specific port (443), whereas name-based specifies all ports (*). You might the following error if you try to mix name-based virtual hosts with SSL.

[error] VirtualHost _default_:443 — mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined result.