How to Install Apache Tomcat on Linux

Posted at Thursday, April 03, 2008
This article assumes that your O/S already has a distribution of Apache.

Download and install the Java SDK compatible with your Tomcat version, for me: JDK 5.0

# mv jdk-1_5_0_15-nb-6_0_1-linux-ml.sh /var/lib/.
# cd /var/lib
# chmod u+x jdk-1_5_0_15-nb-6_0_1-linux-ml.sh
# export DISPLAY=192.168.1.2:0.0
# ./jdk-1_5_0_15-nb-6_0_1-linux-ml.sh

I install the version that comes with the NetBeans GUI, so I simply export
my DISPLAY, execute the script, and follow the installation wizard's prompts.

Afterwards, I create symbolic links as appropriate:

# ln -sf jdk1.5.0_15 java


# cd /usr/bin
# ln -sf /var/lib/java/bin/java java
# ln -sf /var/lib/java/bin/javac javac
# ln -sf /var/lib/java/bin/javadoc javadoc
# ln -sf /var/lib/java/bin/javah javah

Download and install Apache Tomcat, I will be installing Apache Tomcat 5.5.26

Create a User for tomcat:

# useradd tomcat
# usermod -g tomcat tomcat
# passwd tomcat

Extract binaries, change ownership, create symbolic links:

# tar xvfz apache-tomcat-5.5.26.tar.gz -C /var/lib/
# chown -R tomcat:tomcat apache-tomcat-5.5.26
# ln -sf apache-tomcat-5.5.26 apache-tomcat

Edit the tomcat user's .bash_profile, adding the following lines (Note: Ant will be installed later in this section):

export JAVA_HOME=/var/lib/java
export CATALINA_HOME=/var/lib/apache-tomcat
export ANT_HOME=/var/lib/apache-ant
export PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH

Build jsvc in order to run
Apache Tomcat as a service:

# su - tomcat
# cd $CATALINA_HOME/bin
# tar xvfz jsvc.tar.gz
# autoconf
# chmod u+x configure
# ./configure
# make
# cp jsvc ..

Install the following tomcat startup file to /etc/init.d/tomcat and edit as appropriate:

# chkconfig: 345 64 36
# description: Start Tomcat
#!/bin/sh
JAVA_HOME=/var/lib/java
CATALINA_HOME=/var/lib/apache-tomcat
DAEMON_HOME=/var/lib/apache-tomcat
TOMCAT_USER=tomcat

# for multi instances adapt those lines.
TMP_DIR=/var/tmp
PID_FILE=/var/run/jsvc.pid
CATALINA_BASE=/var/lib/apache-tomcat

CATALINA_OPTS="-Xms48m -Xmx256M"
CLASSPATH=\
$JAVA_HOME/lib/tools.jar:\
$CATALINA_HOME/bin/commons-daemon.jar:\
$CATALINA_HOME/bin/bootstrap.jar

case "$1" in
start)
#
# Start Tomcat
#
$DAEMON_HOME/bin/jsvc \
-user $TOMCAT_USER \
-home $JAVA_HOME \
-Dcatalina.home=$CATALINA_HOME \
-Dcatalina.base=$CATALINA_BASE \
-Djava.io.tmpdir=$TMP_DIR \
-wait 10 \
-pidfile $PID_FILE \
-outfile $CATALINA_HOME/logs/catalina.out \
-errfile '&1' \
$CATALINA_OPTS \
-cp $CLASSPATH \
org.apache.catalina.startup.Bootstrap
#
# To get a verbose JVM
#-verbose \
# To get a debug of jsvc.
#-debug \
exit $?
;;

stop)
#
# Stop Tomcat
#
$DAEMON_HOME/bin/jsvc \
-stop \
-pidfile $PID_FILE \
org.apache.catalina.startup.Bootstrap
exit $?
;;

*)
echo "Usage tomcat.sh start/stop"
exit 1;;
esac

Configure the service via chkconfig and start:

# chkconfig --add tomcat
# chkconfig --list tomcat
tomcat 0:off 1:off 2:off 3:on 4:on 5:on 6:off


# service tomcat start


Configure Tomcat to allow use of the 'manager' application:

# vi $CATALINA_HOME/conf/tomcat-users.xml

Add the 'manager' role and a user of your choosing whom will be assigned this role:

<role rolename="manager"/>

<user username="admin" password="admin" roles="manager"/>

Test Apache Tomcat by trying any of the various test URLs, including the manager application:







Download and Install Ant from a binary distribution of Ant

# tar xvfz apache-ant-1.7.0-bin.tar.gz -C /var/lib

# cd /var/lib
# ln -sf apache-ant-1.7.0/ apache-ant

Copy the required library to the Ant binaries:

# export CATALINA_HOME=/var/lib/apache-tomcat
# export ANT_HOME=/var/lib/apache-ant
# cp $CATALINA_HOME/server/lib/catalina-ant.jar $ANT_HOME/lib/.

Test the functionality of Ant by downloading and extracting this test application:

# cd /home/tomcat
# tar xvfz test.tar.gz
test/
test/docs/
test/src/
test/src/Test.java
test/web/
test/web/WEB-INF/
test/web/WEB-INF/test.xml
test/web/WEB-INF/web.xml
test/build.xml
test/build.properties

Test Ant by creating a distribution and deploying it to $CATALINA_HOME/webapps:

# cd /home/tomcat/test
# ant dist
Buildfile: build.xml

prepare:
[mkdir] Created dir: /home/tomcat/test/build
[mkdir] Created dir: /home/tomcat/test/build/WEB-INF
[mkdir] Created dir: /home/tomcat/test/build/WEB-INF/classes
[copy] Copying 2 files to /home/tomcat/test/build
[mkdir] Created dir: /home/tomcat/test/build/WEB-INF/lib

compile:
[javac] Compiling 1 source file to /home/tomcat/test/build/WEB-INF/classes

dist:
[mkdir] Created dir: /home/tomcat/test/dist/docs
[jar] Building jar: /home/tomcat/test/dist/test.war

BUILD SUCCESSFUL
Total time: 1 second


# cp ./dist/*.war $CATALINA_HOME/webapps/.

Test the deployment via:



Integrate Tomcat with existing Apache installation

Download the mod_jk that corresponds to your Apache version: mod_jk-1.2.26-httpd-2.0.61.so

# cd /usr/sbin/
# ./httpd -version
Server version: Apache/2.0.52
Server built: Mar 19 2007 12:13:18

Move the .so library to the Apache modules directory:

# mv mod_jk-1.2.26-httpd-2.0.61.so /etc/httpd/modules/mod_jk.so

Create the workers.properties configuration file that will be used by the connector:

# cd $CATALINA_HOME/conf 
# vi workers.properties

workers.properties

workers.tomcat_home=/var/lib/apache-tomcat
workers.java_home=/var/lib/java
ps=/
worker.list=ajp13
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13

Edit the httpd.conf to include this module along with corresponding directives:

# cd /etc/httpd/conf
# vi httpd.conf

Add the following lines:

LoadModule jk_module modules/mod_jk.so

JkWorkersFile /var/lib/apache-tomcat/conf/workers.properties
JkLogFile /etc/httpd/logs/mod_jk.log
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JKMount /*.do ajp13
JKMount /*.jsp ajp13

Reload or stop Apache Service:

# service httpd start
Starting httpd: [ OK ]

Test the Connector by requesting the test application using Apache instead of Tomcat. If everything is working properly, Apache will hand off the request to Tomcat:



If successful, you have a working Apache Tomcat development environment.


Labels: ,