Note |
---|
TBD - This article can also be improved to use the newer conventions in Tomcat server names. |
Table of Contents |
---|
Introduction
It is often useful to have Apache front Tomcat.
...
In our Enterprise environments we currently only use the tried and true Apache Tomcate Tomcat 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,
...
- /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.
...
- /etc/apache2/mods-available/jk.conf
...
Code Block | ||
---|---|---|
| ||
<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> |
...
- (defines how mod jk should run - if using Ubuntu 11 or lower you must manually create this file)
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.
Enable mod_jk
In Ubuntu it is very easy to enable a moduleThe installer will also enable the modules in Apache essentially running below command for you,
Code Block | ||
---|---|---|
| ||
sudo a2enmod jk
sudo /etc/init.d/apache2 restart # enable the change |
This basically creates symbolic links for you in the /etc/apache2/mods-enabled directory. The two symbolic links will be,
...
Code Block | ||
---|---|---|
| ||
sudo apache2ctl -lM Compiled| ingrep modules:jk # result, core.c mod_log_config.c mod_logio.c worker.c http_core.c mod_so.c |
...
jk_module (shared) |
jk_module 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.
...
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,
...
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 Tomcat0 and Tomcat1 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,
Note |
---|
Ubuntu 12.04.3 LTS no longer has this line. |
Code Block | ||
---|---|---|
| ||
# Tin: This directory does not exist. # # workers.java_home=/usr/lib/jvm/java-gcj |
Note |
---|
If someone can let me know why we would want to have these properties let me know. |
Configure Tomcat AJP
Tomcat out of the box already has AJP enabled and listening on port 8009 with default parameters in server.xml.
the The default parameters are generally sufficient for most environments. For more advanced environments you should read the AJP Connector documentation at Apache Tomcat Configuration Reference.
Configure Map Points in Apache
This last step will be different depending on if you are using virtual hosts or not.
...
Note |
---|
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. |
...
If you wanted to use the load balancer you would change Tomcat0 to loadbalancer.
Restart Apache
Finally you must restart Apache for the changes to take effect.
...
Now you should be able to view the examples site without specifying port 8080, http://www.krypton.com/examples/
Excluding Mod_jk
In the situation where jkmount is using a wildcard as a context root (I don't presonally recommend this approach) an exclude can be added to stop jkmount from affecting the directory.
First edit the virtual host file in /etc/apache2/sites-available/ with your favorite editor and add the following line after the jkmount
Code Block | ||
---|---|---|
| ||
SetEnvIf Request_URI "/directory/*" no-jk |
Make sure to restart your apache for this change to take affect.
Now all files and folders under "directory" will not be affected by mod_jk
Note |
---|
This option is not recommended but in some situations is required |
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.
...