Monday, October 17, 2011

Using mod_jk with Apache 2.2 How to setup a reverse proxy



Advice on mod_jk versus mod_proxy and mod_proxy_ajp
Do not use mod_proxy and mod_proxy_ajp on Apache 2.2 since they are not stable and have caused me some problems. Use mod_jk instead.

Prerequisites & Assumptions Before you continue
I assume you have:
- You already have Apache Webserver 2.2 installed
- You already have Tomcat installed
- You already have a web application on Tomcat with context "jobsapp"
- You are using Redhat Enterprise or CentOS and therefore have yum on your system.

How to get the mod_jk binary for Apache 2.2 on Linux
You wont find it anywhere, you have to compile it, now stop being lazy and start working.

The following steps detail the process:

// Create a temporary source directory, I use /opt/src
mkdir /opt/src
cd /opt/src 


// Get the source package
wget http://mirrors.isu.net.sa/pub/apache//tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.32-src.tar.gz


// Install packages needed to compile the binary
yum install httpd-devel
yum install gcc-c++


// Extract the tar.gz file
tar -zxvf /opt/src/tomcat-connectors-1.2.32-src.tar.gz


// Configure, make, make install
./configure --with-apxs=/usr/sbin/apxs
make
su -c 'make install'

How to configure Apache Webserver with Mod JK

1. Edit the file httpd.conf
vim /etc/httpd/conf/

2. Add this line to load the module:
...
LoadModule jk_module modules/mod_jk.so
...

3. Add also the following lines:

JkWorkersFile   conf/worker.properties
JkLogFile       /var/log/httpd/mod_jk.log
JkLogLevel      debug
JkRequestLogFormat      "%w %U %T"
JkOptions +ForwardURIEscaped +ForwardURICompatUnparsed

After finishing, change JkLogLevel from debug to "error" or "info" so that the log file doesn't grow too much in size.

4. Now create a file names worker.properties inside /etc/httpd/conf/ and add the following lines to it:
vim /etc/httpd/conf/worker.properties

# Worker Properties file for mod_jk
worker.list=tomcat
worker.tomcat.host=localhost
worker.tomcat.port=8009
worker.tomcat.type=ajp13
worker.tomcat.connection_pool_timeout=300




4.5. Make sure to read the mod_jk attributes in this link:
http://tomcat.apache.org/connectors-doc/reference/workers.html

5. Now open the httpd.conf or your virtual host configuration file and add the following lines:

JkMount     /jobsapp tomcat
JkMount     /jobsapp/* tomcat

6. Also open the ssl httpd conf file which has the following line at the top,
and add the same two lines inside them:

<VirtualHost _default_:443>
...
JkMount     /jobsapp tomcat
JkMount     /jobsapp/* tomcat
...
</VirtualHost>


7. Now restart apache

/etc/init.d/httpd restart

No comments: