Install Tomcat 7 on CentOS 6.4

The following steps shows how to install Tomcat 7 (or Tomcat 6) from source, on CentOS 6.4.
Although YUM/RPM install is highly recommended, this howto is usefull where several Tomcat instances are needed.

Installing Java SDK

First, install Sun JDK 1.7.x from Oracle’s official web site.

Download tar.gz (not RPM) package for your Linux arch and unpack it in /usr/local  (location is arbitrary, I just used to use this directory).
Create symlink to extracted directory, then install install JDK using the following update-alternatives commands:
  root#> ln -s /usr/local/jdk1.7.0_xx/ java
  root#> update-alternatives –install “/usr/bin/java” “java” “/usr/local/java/bin/java” 1
  root#> update-alternatives –set java /usr/local/java/bin/java

Finally, to check whether JDK was successfully installed, run the following commands:

  root#> which java
  root#> java -version

Installing Tomcat

Download Apache Tomcat 7 from official web site and extract it in /usr/local (again, location is arbitrary). Rename the apache-tomcat directory into something shorter, let’s say, “tomcat”.

  root#> cd /usr/local
  root#> wget .
  root#> tar xzvf apache-tomcat-7.0.42.tar.gz
  root#> mv apache-tomcat-7.0.42 tomcat

Next, edit the /usr/local/tomcat/bin/ and add the following (after the closing “esac”):

  export JAVA_HOME=/usr/local/java
  export PATH=${PATH}:${JAVA_HOME}/bin
  export JAVA_OPTS=”-Djava.awt.headless=true -Xms3072m -Xmx4096m  -server -XX:MaxPermSize=3072m”

Values Xms, Xmx and MaxPermSize are per server configuration. How much memory you will assign to Java depends on your server’s RAM resources.

Next, download MySQL JDBC connector, extract the archive and copy JAR file into tomcat/lib directory:

  root#> tar xzvf mysql-connector-java-5.x.x.tar.gz
  root#> cp mysql-connector-java-5.x.x-bin.jar /usr/local/tomcat/lib/

Next, install the Tomcat Native Library. Go into tomcat/bin directory and extract the tomcat-native.tar.gz arhive. Before compiling the library, install the necessary packages:

  root#> yum -y install apr-devel httpd-devel
  root#> cd /usr/local/tomcat/bin/tomcat-native-xx/jni/native/
  root#> ./configure –with-apr=/usr –with-java-home=/usr/local/java/
  root#> make
  root#> make install

Switch to /usr/local/apr/lib. Check whether* symlinks are present. If not, create them:
  root#> ln -s
  root#> ln -s

Then copy and both symlinks into /usr/local/java/jre/lib/i386/server/. If Linux OS is 64bit version, change i386 with amd64.

Finally, create new tomcat user, which will run the Tomcat instance:

  root#> useradd -d /usr/local/tomcat -s /bin/bash tomcat
  root#> chown -R tomcat:tomcat /usr/local/tomcat

Init script

Since Tomcat source installation does not create the /etc/init.d/ script, we have to create it manually.
Create /etc/init.d/tomcat with the following content and make it executable:
# Start script for Jakarta Tomcat 5
# chkconfig: 2345 84 15
# description: Jakarta Tomcat 6 start script – visit for updates
# processname: tomcat
# pidfile: /var/run/

. /etc/rc.d/init.d/functions

PS_HEADER=`ps www -u ${TOMCAT_USER} | grep PID`


running() {
        ps www -u ${TOMCAT_USER} | grep org.apache.catalina.startup.Bootstrap | grep -v gr                                                                                                                      ep

case “$1″ in
        if [ -f /var/lock/subsys/${TOMCAT_PROCESS} ] ; then
            echo “${TOMCAT_PROCESS} already running”
            exit 1
        echo “Starting ${TOMCAT_PROCESS}:”
        su – ${TOMCAT_USER} -c “~${TOMCAT_USER}/bin/ start”
        [ ${RETVAL} = 0 ] && touch /var/lock/subsys/${TOMCAT_PROCESS}
        if [ ! -f /var/lock/subsys/${TOMCAT_PROCESS} ] ; then
            echo “${TOMCAT_PROCESS} not running”
            exit 1
        echo “Shutting down ${TOMCAT_PROCESS}:”
        su – ${TOMCAT_USER} -c “~${TOMCAT_USER}/bin/ stop”
        [ ${RETVAL} = 0 ] && rm -f /var/lock/subsys/${TOMCAT_PROCESS}
        echo “Enforcedly shutting down ${TOMCAT_PROCESS}: “
        su – ${TOMCAT_USER} -c “~${TOMCAT_USER}/bin/ stop -force”
        port | grep 8080 | awk ‘{ print $7}’ | awk -F/ ‘{ print $1 }’
        [ ${RETVAL} = 0 ] && rm -f /var/lock/subsys/${TOMCAT_PROCESS}
        $0 stop
        sleep 5
        $0 start
        echo “Killing ${TOMCAT_PROCESS} process: “
        PID=`running | gawk ‘{print $1}’`
        kill -9 $PID
        if [ -f /var/lock/subsys/${TOMCAT_PROCESS} ] ; then
            rm -f /var/lock/subsys/${TOMCAT_PROCESS}
        if [ -f /var/lock/subsys/${TOMCAT_PROCESS} ] ; then
            echo “Process ${TOMCAT_PROCESS} running with properties:”
            ps www -u ${TOMCAT_USER}
            echo “Process ${TOMCAT_PROCESS} not running”
        if [ -f /var/lock/subsys/${TOMCAT_PROCESS} ] ; then
            echo -n “Process ${TOMCAT_PROCESS} running with number “
            running | gawk ‘{print $1}’
            echo “Process ${TOMCAT_PROCESS} not running”
        echo “Checking version for ${TOMCAT_PROCESS}:”
        su – ${TOMCAT_USER} -c “~${TOMCAT_USER}/bin/ version”
        echo “Usage: $0 {start|stop|stopforce|kill|restart|status|pid|version}”
        echo ”       If you need to supply ‘kill’ try ‘stopforce’ first”
        echo “Command ~${TOMCAT_USER}/bin/ accepts additional parameters as wel                                                                                                                      l”
        exit 1
exit ${RETVAL}

In order to start Tomcat at boot, add the init.d script into chkconfig database and turn it on. After this step, we can also start Tomcat with “service” command.

root#> chkconfig –add /etc/init.d/tomcat
root#> chkconfig tomcat on
root#> service tomcat start

Posted in SysAdmin, Web servers Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>