This article can also be improved to use the newer conventions in Tomcat server names.
Introduction
It is often useful to have Apache front Tomcat.
There are two main methods of doing this,
- Apache Tomcat Connector - tried and true
- Reverse Proxy - relatively new (Nov 4, 2009) and part of Apache
In our Enterprise environments we currently only use the tried and true Apache Tomcate Connector.
The Apache Tomcat Connector is often called by its binary file mod_jk which I will use from this point onwards.
Install mod_jk
There are two different methods of installing mod_jk. The simper being using apt-get if you have Ubuntu.
Using apt-get
With Ubuntu you can have mod_jk almost automatically install for you via,
sudo apt-get install libapache2-mod-jk
Running this command results in the following,
- /usr/lib/apache2/modules/mod_jk.so (this will not always be the most current - you can see the current version by looking at the package info)
- /etc/libapache2-mod-jk/workers.properties (create a generic workers.properties file for generic setup of Tomcat 6.x)
- /etc/apache2/mods-available/jk.load (file which loads the mod_jk module)
- /etc/apache2/mods-enabled/jk.load (symbolic link which starts the mod_jk module)
BUT it is not quite working yet. Continue reading.
Create jk.conf
The approach I take here is to make the mod_jk available at the global level to all Apache Virtual hosts. If we wanted things to be more granular you could instead put the contents of jk.conf in a specific virtual host.
mod_jk may be installed but it still needs to be configured. Create the file /etc/apache2/mods-available/jk.conf using sudo with the following contents,
<IfModule mod_jk.c> JkWorkersFile "/etc/libapache2-mod-jk/workers.properties" JkLogFile "/var/log/apache2/mod_jk.log" # Use debug mode if you have trouble # JkLogLevel debug JkLogLevel info </IfModule>
There are much more configuration options. But this should be enough to get you started for a small to medium scale website.
Manually Installing mod_jk
I often have to do this on Solaris or alternative operating systems so the manual process of installing mod_jk is good to have handy
The manual process of installing mod_jk should be moved to this wiki.
Enable mod_jk
In Ubuntu it is very easy to enable a module,
sudo a2enmod jk
This basically creates symbolic links for you in the /etc/apache2/mods-enabled directory. The two symbolic links will be,
- jk.conf
- jk.load
You can verify that the module loaded properly,
sudo apache2ctl -l Compiled in modules: core.c mod_log_config.c mod_logio.c worker.c http_core.c mod_so.c
worker.c in the resulting output shows that the module loaded properly.
Configure Mod_jk
workers.properties Configuration File
Edit or create (in the case of a manual setup) /etc/libapache2-mod-jk/workers.properties which defines how Apache will pass the traffic to Tomcat.
This is the minimal configuration,
# Define workers using ajp13 worker.list=tomcat_a_worker # Set properties for worker worker.tomcat_a_worker.type=ajp13 worker.tomcat_a_worker.host=localhost worker.tomcat_a_worker.port=8009 worker.tomcat_a_worker.lbfactor=1
In the above case are creating a worker called tomcat_a_worker for a web application to connect to TomcatA which will run in the www.krypton.com virtualhost. Notice the host=localhost line in yellow. This should point to the server that Tomcat is running on. In this case, we are have Apache and Tomcat on the same server so we use localhost.
If you did an automatic setup a default working workers.properties was generated with the same options as above except the worker is called ajp13_worker (see line 61), and the file is interspersed with comments. Shown below is the default workers.properties as of April 7, 2011,
To keep the example simple I have not added load balance support yet. If you want load do load balancing with a second tomcat instance, a simple configuration would look like this,
# Define workers using ajp13 worker.list=loadbalancer # Set properties for worker worker.tomcat_a_worker.type=ajp13 worker.tomcat_a_worker.host=localhost worker.tomcat_a_worker.port=8009 worker.tomcat_a_worker.lbfactor=1 # Set properties for worker worker.tomcat_b_worker.type=ajp13 worker.tomcat_b_worker.host=localhost worker.tomcat_b_worker.port=8109 worker.tomcat_b_worker.lbfactor=1 # Set up load balancer using ajp13 workers work.loadbalancer.type=lb work.loadbalancer.balance_workers=tomcat_a_worker,tomcat_b_worker
The load balancing uses weighted round-robin with sticky sessions. The lower the lbfactor number the less weight and as such the less work done by the worker. In our example, since both TomcatA and TomcatB have a factor of 1 the load balance is split about 50/50.
Tin's Adjustments
What you have so far should work. However, if you want to match what I use in production I also made the following adjustments.
Look for this block and comment out.
# Tin: This directory does not exist. # # workers.tomcat_home=/usr/share/tomcat5
Look for this block and comment out.
# Tin: This directory does not exist. # # workers.java_home=/usr/lib/jvm/java-gcj
If someone can let me know why we would want to have these properties let me know.
Configure Map Points in Apache
This last step will be different depending on if you are using virtual hosts or not.
First verify that you can hit the examples application without mod_jk by directly hitting the TomcatA server running on port 8080.
If you have been following my instructions to setup Ubuntu you probably have the firewall setup so do not forget to open up port 8080 temporarily for this test.
Launch a browser and to to, http://www.krypton.com:8080/examples/. You should see the following page show up.
In our examples we will be using virtual hosts. Once you get virtual hosts working, add a reference to the worker entry to your virtual host file.
JkMount /examples tomcat_a_worker JkMount /examples/* tomcat_a_worker
The above example uses tomcat_a_worker for the name of the worker. This name depends on what value was used in workers.properties file for worker.list. In this article we had two other examples,
ajp13_worker (if you went with the default generated workers.properties file) or loadbalancer (if you opted to follow the load balancer example).
If you are not using virtual hosts you can add the above to /etc/apache2/sites-enabled/000-default.
If you wanted to use the load balancer you would change TomcatA to loadbalancer.
Restart Apache
Finally you must restart Apache for the changes to take effect.
sudo /etc/init.d/apache2 restart
Now you should be able to view the examples site without specifying port 8080, http://www.krypton.com/examples/
Resources
http://www.gustavomejia.com/blog/2008/03/02/1204455261015.html - seems to have decent instructions using ubuntu apt-get to set up mod_jk. Don't understand the java_home thing though.
http://wiki.zimbra.com/index.php?title=Zimbra_with_Apache_using_mod_jk - article from Zimbra