Tomcat Native Libraries

The Tomcat Native Libraries improve the performance of Tomcat by leveraging the faster I/O capabilities of the operating system. This is a step-by-step procedure to install them with the least amount of difficulty. The native libraries will be built from source, so we need to make sure we have the required dependencies.

1. Install the following dependencies:

# yum install apr-devel gcc java-1.6.0-openjdk-devel openssl-devel

Alternately, you can use the Sun/Oracle JDK. In either case you will need to set the environment variable JAVA_HOME.
2. Download the latest native libraries from http://tomcat.apache.org
3. Untar the source tarball

# tar -zxvf tomcat-native-1.1.24-src.tar.gz

4. Navigate inside the extracted tarball

cd tomcat-native-1.1.24-src/jni/native

5. Configure the package for your host

# ./configure \
--prefix=/usr/local/tomcat-native \
--with-apr=/usr/bin/apr-1-config \
--with-ssl=yes

6. Make the package and install it

make && make install

If the make fails, it maybe because it cannot find your installation of java. If you used my java.profile (look in my github repo for tomcat), it should have set the necessary environment variable. If it did not, your java install maybe in a location that the profile script does not search. Update your copy of the java.profile to reflect your location and log out and back in to refresh your environment variables. If JAVA_HOME is set and make still complains, then JAVA_HOME maybe pointing at an incomplete installation.

Once the native libraries are installed, you can use the commented lines in my setenv.sh to enable the tomcat server to locate these libraries. A restart of your tomcat instance should pickup the changes and when you review catalina.out, you should see that the server identified the libraries and used them.

The reason I override the prefix, is to keep the tomcat install clean, you can upgrade the native libraries without touching the tomcat instance and vice-versa.

Tomcat with a segregated configuration

Here’s a step-by-step guide to deploy Tomcat (6 or 7). I’ll use 7 for the rest of this guide but the procedure is the same regardless of the version. I try to avoid modifying the Tomcat install itself once deployed, so I redirect the configuration to an alternate location. This makes upgrades very simple and keeps all the important pieces in one place.

Start by cloning my github tomcat repo to get the files you need:

git clone git://github.com/breauxaj/model-tomcat.git

Among the files in the repo, tomcat.sh and java.sh, are intended to make things easier by setting some environment variables. Copy both files into /etc/profile.d/

# cp tomcat.sh /etc/profile.d/tomcat.sh
# cp java.sh /etc/profile.d/java.sh

In order for them to take effect, log out and back in. You should now see a TOMCAT_HOME, CATALINA_HOME and JAVA_HOME set. If JAVA_HOME is not set, you might not have java installed. You can use the openjdk version available with most RHEL-distros. If you want to use the Sun (Oracle) version, the java.profile should be able to find that one once it’s installed.

Let’s deploy Tomcat 7 on the host:
1. Download the tomcat source tarball from http://tomcat.apache.org/
2. Untar the tarball (usually best to do this in your home folder)

# tar -zxvf apache-tomcat-7.0.28

3. Move the resulting folder into /usr/local

# mv apache-tomcat-7.0.28 /usr/local

4. Navigate to /usr/local
5. Create a symlink to simplify the path

# ln -s /usr/local/apache-tomcat-7.0.28 tomcat

6. Change the owner and group for this install to root:root

# chown -R root:root /usr/local/apache-tomcat-7.0.28

Now we need to setup the segregated instance:
1. Create the alternate location tree:

# mkdir -p /var/tomcat/{bin,conf,shared,temp,webapps,work}
# mkdir -p /var/tomcat/shared/{classes,lib}
# mkdir -p /var/log/tomcat

2. Stage the critical files to run this redirected instance (using /var/tomcat):

# cp $TOMCAT_HOME/conf/catalina.properties /var/tomcat/conf/
# cp $TOMCAT_HOME/conf/logging.properties /var/tomcat/conf/
# cp $TOMCAT_HOME/conf/server.xml /var/tomcat/conf/
# cp $TOMCAT_HOME/conf/tomcat-users.xml /var/tomcat/conf/
# cp $TOMCAT_HOME/conf/web.xml /var/tomcat/conf/

3. Navigate to /var/tomcat
4. Symlink the logs folder into the redirected instance:

# ln -s /var/log/tomcat logs

5. Determine the user your tomcat install should run as (Ex: tomcat).
6. Change the owner of /var/log/tomcat and /var/tomcat to that user (otherwise tomcat will not start).

# chown tomcat:tomcat /var/log/tomcat
# chown -R tomcat:tomcat /var/tomcat

7. Set file and folder permissions for the redirected instance:

# find /var/tomcat -type d -exec chmod 775 {} \;
# find /var/tomcat -type f -exec chmod 664 {} \;

8. Copy the init script from my git repo.

# cp tomcat.init /etc/init.d/tomcat

9. Change the owner and file permissions:

# chown root:root /etc/init.d/tomcat
# chmod 755 /etc/init.d/tomcat

10. To force the installed tomcat to use our redirected instance, use the setenv.sh from my repo, copy it into /usr/local/tomcat/bin

# cp setenv.sh /usr/local/tomcat/bin

11. Modify the setenv.sh as necessary.
12. Start tomcat:

# /etc/init.d/tomcat start

13. Check the folder /var/log/tomcat for catalina.out (among other files).

If there is no catalina.out in /var/log/tomcat, check folder permissions and ownership in /var/tomcat as well as /var/log/tomcat.

Look for my post on adding the Tomcat native libraries for enhanced performance here.