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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s