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 http://www.dsgnwrld.com/am/tomcat/tomcat-7/v7.0.42/bin/apache-tomcat-7.0.42.tar.gz .
  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/catalina.sh 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 libtcnative-1.so* symlinks are present. If not, create them:
  root#> ln -s libtcnative-1.so.0.1.xx libtcnative-1.so.0
  root#> ln -s libtcnative-1.so.0.1.xx libtcnative-1.so

Then copy libtcnative-1.so.0.1.xx 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:
#!/bin/sh
#
# Start script for Jakarta Tomcat 5
#
# chkconfig: 2345 84 15
# description: Jakarta Tomcat 6 start script – visit manpage.ch for updates
# processname: tomcat
# pidfile: /var/run/tomcat.pid

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

TOMCAT_USER=<tomcat_username>
TOMCAT_PROCESS=<tomcat_username>
PS_HEADER=`ps www -u ${TOMCAT_USER} | grep PID`

RETVAL=0

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

case “$1″ in
   start)
        if [ -f /var/lock/subsys/${TOMCAT_PROCESS} ] ; then
            echo “${TOMCAT_PROCESS} already running”
            exit 1
        fi
        echo “Starting ${TOMCAT_PROCESS}:”
        echo
        su – ${TOMCAT_USER} -c “~${TOMCAT_USER}/bin/catalina.sh start”
        RETVAL=$?
        [ ${RETVAL} = 0 ] && touch /var/lock/subsys/${TOMCAT_PROCESS}
        ;;
   stop)
        if [ ! -f /var/lock/subsys/${TOMCAT_PROCESS} ] ; then
            echo “${TOMCAT_PROCESS} not running”
            exit 1
        fi
        echo “Shutting down ${TOMCAT_PROCESS}:”
        echo
        su – ${TOMCAT_USER} -c “~${TOMCAT_USER}/bin/catalina.sh stop”
        RETVAL=$?
        [ ${RETVAL} = 0 ] && rm -f /var/lock/subsys/${TOMCAT_PROCESS}
        ;;
   stopforce)
        echo “Enforcedly shutting down ${TOMCAT_PROCESS}: “
        echo
        su – ${TOMCAT_USER} -c “~${TOMCAT_USER}/bin/catalina.sh stop -force”
        port | grep 8080 | awk ‘{ print $7}’ | awk -F/ ‘{ print $1 }’
        RETVAL=$?
        [ ${RETVAL} = 0 ] && rm -f /var/lock/subsys/${TOMCAT_PROCESS}
        ;;
   restart)
        $0 stop
        sleep 5
        $0 start
        ;;
   kill)
        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}
        fi
        ;;
   status)
        if [ -f /var/lock/subsys/${TOMCAT_PROCESS} ] ; then
            echo “Process ${TOMCAT_PROCESS} running with properties:”
            echo
            ps www -u ${TOMCAT_USER}
        else
            echo “Process ${TOMCAT_PROCESS} not running”
        fi
        ;;
   pid)
        if [ -f /var/lock/subsys/${TOMCAT_PROCESS} ] ; then
            echo -n “Process ${TOMCAT_PROCESS} running with number “
            running | gawk ‘{print $1}’
        else
            echo “Process ${TOMCAT_PROCESS} not running”
        fi
        ;;
   version)
        echo “Checking version for ${TOMCAT_PROCESS}:”
        echo
        su – ${TOMCAT_USER} -c “~${TOMCAT_USER}/bin/catalina.sh 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/catalina.sh accepts additional parameters as wel                                                                                                                      l”
        exit 1
esac
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

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

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>