Monday, December 20, 2010

Forwarding apache requests to Tomcat servlet container

Most web applications todays are using favorite Apache web server for its proven architecture and free apache license.
Tomcat is same as a very famous servlet container or java application server.
 Although tomcat itself can be access as a simple web server, for production environments it may lack some features like load-balancing, redundancy, etc.

So the best way to use is to integrate those two application, and finally its a great combination for enterprise applications for excess performance and ease of maintenance.

The theory behind is to use a middle tier connector called mod_jk.so module which communicate using AJP(Apache JSer Protocol ) version 1.3, best of currently available stable version.See the following figure for graphical view.



Here in this small steps guide i configure an Apache2 to a single instance of Tomcat6.Any comments and improvements are welcome at the end.

installations:





install apache2;Web server who listen on port 80
sudo apt-get install apache2






install tomcat6 and its applications>Application server/Web Container who translate application code in to results listen on port 8080 by default

sudo apt-get install tomcat6
sudo apt-get install tomcat6-common
sudo apt-get install tomcat6-examples
sudo apt-get install tomcat6-admin
sudo apt-get install tomcat6-docs
sudo apt-get install tomcat6-user

install libapache2-mod-jk>Connector module to forward web request to redirect in to tomcat's port 8009,8005 etc
sudo apt-get install libapache2-mod-jk

Configurations:

ask Apache to forward default pages to ajp13_worker through installed jk module by adding following two lines to this file by 'gedit /etc/apache2/sites-enabled/000-default'
<.....>
ServerAdmin chand@engineering.com  
JkMount / ajp13_worker JkMount /* ajp13_worker


....... file continues................

to make above to work, verify that following line is there on '/etc/apache2/mods-enabled/jk.load' 
LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so

Now you have to tell jk_mod to find its configuration file path and how it should log errors and other informations.So add the following lines after LoadModule as above


JkWorkersFile /etc/libapache2-mod-jk/workers.properties 
JkLogFile /var/log/apache2/mod_jk.log 
JkShmFile /var/log/apache2/mod_jk.shm
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "


Now Apache will forward the requests to mod_jk module.Now its time to configure mod_jk to forward those request to Tomcat.So goto its configuration file as you defined on the above:

/etc/libapache2-mod-jk/workers.properties 


Here you have to explicitly and correctly define 3 main parameters for jk module:

  • Tomcat Home
  • Java Home
  • Exact parameters of the Worker(ex: ajp13_worker )

in my server its with following configurations.But you can goto relevant folders and find out what are the available folders/java VMs you can configure for.

workers.tomcat_home=/usr/share/tomcat6


workers.java_home=/usr/lib/jvm/java-6-openjdk

worker.list=ajp13_worker

worker.ajp13_worker.port=8009
worker.ajp13_worker.host=localhost
worker.ajp13_worker.type=ajp13
worker.loadbalancer.balance_workers=ajp13_worker


OK! now jk module knows how to forward requests to port 8009, then we have to configure someone to listen on port 8009, and its tomcat.

Goto /etc/tomcat6/server.xml
By default tomcat don't listen to ajp13 protocol on port 8009, only on 8080 fot http requests.
you can uncomment the following line



'< Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /  >'


Now to all those changes to take effect you have to restart both services, but tomcat6 1st and then apache.

When you browse to http://localhost  you must be shown with tomcat's default page as you seen by http://localhost:8080

if both are same... now all your web requests on port 80 that were listen by apache web server are forwarding to Tomcat Servlet container.

In the same way as above, you can configure apache to request PHP sites as well.Lets see it in some other article.

Good Luck!!!!

No comments:

Post a Comment