Connect your Java App to Apache via mod_jk


If there's a need to connect a Tomcat App to Apache, so that the web application can be access directly via port 80 (instead of 8080), I always use mod_proxy_ajp as it's very easy to configure. By default it's loaded in Apache in CentOS, while in Ubuntu you still have to load manually that module.

For some reasons, mod_proxy_ajp keeps on failing. It's throwing an error message like below:

[Thu Jun 21 10:21:29 2012] [error] proxy: AJP: failed to make connection to backend: localhost
[Thu Jun 21 10:21:31 2012] [error] proxy: AJP: disabled connection for (localhost)
[Thu Jun 21 10:22:04 2012] [error] proxy: AJP: disabled connection for (localhost)
[Thu Jun 21 10:23:49 2012] [error] (111)Connection refused: proxy: AJP: attempt to connect to 127.0.0.1:8009 (localhost) failed


I just can't fix it. Weird!

So I have decided to switch to mod_jk. I have done this in Ubuntu server.

Assuming that you've already got Tomcat and Apache working in your box, to install mod_jk:
apt-get install libapache2-mod-jk

Create your workers.properties file:
vim /etc/apache2/workers.properties

Insert the following:
# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009



Configure Apache to use the worker that you have just created:
vim /etc/apache2/apache2.conf

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


Configure which site or URL is to be pass connected to Tomcat:
vim /etc/apache2/sites-enabled/000-default


For instance if you want to have the ROOT app from your Tomcat to be connected to Apache, insert this line:
JkMount / worker1

Enable connector port in Tomcat by uncommenting the following line in /etc/tomcat6/server.xml:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


Restart Apache and Tomcat, that's it!






 

1 comment:

  1. Cool! thanks for sharing this info. I will save this as my reference. Will also try to do that too. Keep posting!



    APC UPS BATTERIES

    ReplyDelete