Performing a Custom Tomcat Installation

This posting assumes that you've already installed a Java Development Kit (not just a Java Runtime Environment) and that your system is ready to compile and run Java applications. It also assumes that you will be working with Tomcat 6 or a later version. To install Tomcat, download the latest core binary release from http://tomcat.apache.org. I suggest unpacking it into your server's /opt directory, and then create a symbolic link that points /opt/tomcat to the unpacked Tomcat directory.
cd /opt
tar -xzvf /<path to downloaded file>/apache-tomcat-6.0.20.tar.gz
ln -s /opt/apache-tomcat-6.0.20 /opt/tomcat
Next we create a local configuration and execution environment for your Tomcat install, so that any future Tomcat upgrades will not overwrite your existing setup.
for DIR in logs temp webapps work conf; do mkdir -p /opt/localhost/${DIR}; done
cp -a /opt/tomcat/conf/* /opt/localhost/conf/
Web applications that connect to a database will need access to JAR files that provide drivers for that database. The best place to put these JAR files is in Tomcat's $CATALINA_HOME/lib directory, which makes them accessible to all web applications. If you've already installed the MySQL and PostgreSQL JDBC drivers on a Debian-compatible system, the procedure to create symbolic links to these JARs is:
cd /opt/tomcat/lib
ln -s /usr/share/java/mysql.jar
ln -s /usr/share/java/postgresql.jar
Keep in mind that whenever you upgrade your Tomcat installation to a new release, each of these symbolic links will need to be rebuilt within the upgraded system. Our next step is to create the /etc/init.d/tomcat initscript and populate it with the following code:
#!/bin/sh
 
### BEGIN INIT INFO
# Provides:             tomcat
# Required-Start:
# Required-Stop:
# Default-Start:        3 4 5
# Default-Stop:         0 1 2 6
# Short-Description:    Tomcat Java application server
### END INIT INFO
 
set -e
 
export CATALINA_HOME="/opt/tomcat"
export CATALINA_BASE="/opt/localhost"
 
# Check that the startup and shutdown scripts are installed
[ -x $CATALINA_HOME/bin/startup.sh ] || exit 0
[ -x $CATALINA_HOME/bin/shutdown.sh ] || exit 0
 
# Get LSB functions
. /lib/lsb/init-functions
. /etc/default/rcS
 
case "$1" in
  start)
         echo $"Starting Tomcat"
         $CATALINA_HOME/bin/startup.sh
         ;;
  stop)
         echo $"Stopping Tomcat"
         $CATALINA_HOME/bin/shutdown.sh
         ;;
  *)
         echo $"Usage: $0 {start|stop}"
         exit 1
         ;;
esac
 
exit $RETVAL
You can then add Tomcat to the initscripts so that it will be started upon bootup.
update-rc.d tomcat defaults
Now your /opt/localhost tree is ready to be configured and populated with web applications. More on this in a future posting. When a Tomcat upgrade is released, upgrading this setup is simply a matter of stopping Tomcat, unpacking the new tarball into /opt, pointing the /opt/tomcat symlink to the newly unpacked directory, recreating the symbolic links in /opt/tomcat/lib that provide access to database drivers and other JARs, and then restarting Tomcat.

Tags: