Categories
Java Plesk Technology Tomcat

Tomcat Upgrade on Plesk for Linux/Unix

This article describes how to upgrade Tomcat running under Plesk and how to disconnect the control Plesk has over the Tomcat server. This may not be the ideal configuration for you if you have many clients using JSP webapps. Your clients will no longer have the ability to use the Plesk war upload feature from the Plesk admin page.

Upgrade Java
If you plan to install the latest version of Tomcat on your server, you may need to upgrade to the latest version of Java as well. Many Plesk installations use Tomcat 4 and Java 1.4, so you may want Tomcat 5 + Java 5 or Tomcat 6 Java 6.

There are several ways to upgrade Java, and this article will give a simple outline without details.

You can download the latest Java JDK at this link. Note, you will need the stand alone JDK (java development kit) not with Netbeans, and not the JRE version.

http://java.sun.com/javase/downloads/index.jsp

Once you download the version for your platform, install the JDK. On Plesk 7.5, the current JVM is installed at /usr/lib/jvm The RPM install may install to this location. If you downloaded a tar package, you should move the expanded folder to this location.

Upgrade Tomcat

The latest version of Tomcat is available at this location

http://tomcat.apache.org/

Because Tomcat runs on the Java virtual machine, you do not need to download a platform specific version or build the code on your server. Simply choose between the .zip or tar.gz download package.

On Plesk 7.5, Tomcat 4 is installed in /var/lib and you should install the new version there. Simply unzip or untar the Tomcat download package in /var/lib

Note – the following describes Tomcat 5. Substitute Tomcat 6 or greater if needed.

You should also create a symlink in /var folder called tomcat5, notice there should be a symlink there from the older Tomcat.


ln -s /var/lib/apache-tomcat-5.5.15/ tomcat5

Add a new Tomcat user and Group

Plesk uses a user and group of tomcat4 | tomcat4 and you should create a new group and user for your new Tomcat version.


groupadd tomcat5
useradd -G tomcat5 tomcat5

Modify the Startup Script

You should copy /etc/init.d/tomcat4 to /etc/init.d/tomcat5 and change all instances of ‘tomcat4’ to ‘tomcat5’

You should also make a symlink in /usr/bin


/usr/bin/dtomcat5 -> /var/tomcat5/bin/catalina.sh


#!/bin/sh
#
# Startup script for Tomcat 5.0, the Apache Servlet Engine
#
# chkconfig: - 80 20
# description: Tomcat 5.0 is the Apache Servlet Engine RI for Servlet 2.3/JSP 1.2
# processname: tomcat
# pidfile: /var/run/tomcat5.pid
# config:  /etc/tomcat4/tomcat5.conf
#
# Gomez Henri 
# Keith Irwin 
# Nicolas Mailhot 
#
# version 1.02 - Removed initlog support
# version 1.03 - Removed config:
# version 1.04 - tomcat will start before httpd and stop after httpd
# version 1.05 - jdk hardcoded to link /usr/java/jdk and tomcat runs as "nobody"
# version 1.06 - split up into script and config file
# version 1.07 - Rework from Nicolas ideas
# version 1.08 - Fix work dir permission at start time, switch to use tomcat4
# version 1.09 - Fix pidfile and config tags
# version 1.10 - Fallback to su direct use on systems without Redhat/Mandrake init.d functions
# version 1.11 - Fix webapps dir permissions
# version 1.12 - remove initial start/stop level for chkconfig (- 80 20)
# version 1.13 - remove chown of logs/work/temp/webapps dir, owned by tomcat4 at install time
# version 1.14 - correct the start/stop ugly hack by waiting all the threads stops
# version 1.15 - ensure we're looking for TOMCAT_USER running catalina
# version 1.16 - Add support for CATALINA_PID env var
# version 1.17 - Remove run files only tomcat started correctl
#                in start area, check that tomcat is not allready running
# version 1.18 - Fix kill typo (thanks Kaj J. Niemi)
# version 1.19 - Add jar relinking
# version 1.20 - Check there is no stalling tomcat4.pid
# version 2.00 - updated to tomcat5

# Source function library.
if [ -x /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi

SHUTDOWN_WAIT=30

# Path to the tomcat launch script (direct don't use wrapper)
TOMCAT_SCRIPT=/usr/bin/dtomcat5

# Path to the script that will refresh jar symlinks on startup
#TOMCAT_RELINK_SCRIPT="/usr/share/tomcat5/bin/relink"

# Tomcat name :)
TOMCAT_PROG=tomcat5
        
# if TOMCAT_USER is not set, use tomcat4 like Apache HTTP server
if [ -z "$TOMCAT_USER" ]; then
    TOMCAT_USER="tomcat5"
fi

# Since the daemon function will sandbox $tomcat
# no environment stuff should be defined here anymore.
# Please use the /etc/tomcat.conf file instead ; it will
# be read by the $tomcat script

RETVAL=0

# See how we were called.
start() {
    echo -n "Starting $TOMCAT_PROG: "

        if [ -f /var/lock/subsys/tomcat5 ] ; then
                if [ -f /var/run/tomcat5.pid ]; then
                        read kpid < /var/run/tomcat5.pid
                        if checkpid $kpid 2>&1; then
                                echo "process allready running"
                                return -1
                        else
                                echo "lock file found but no process running for pid $kpid, continuing"
                        fi
                fi
        fi
 
        export CATALINA_PID=/var/run/tomcat5.pid
        touch $CATALINA_PID
        chown $TOMCAT_USER:$TOMCAT_USER $CATALINA_PID

        $TOMCAT_RELINK_SCRIPT

        if [ -x /etc/rc.d/init.d/functions ]; then
#               daemon --user $TOMCAT_USER $TOMCAT_SCRIPT start -security 
               daemon --user $TOMCAT_USER $TOMCAT_SCRIPT start
        else
#               su - $TOMCAT_USER -c "$TOMCAT_SCRipT start -security"
                su - $TOMCAT_USER -c "$TOMCAT_SCRIPT start"
        fi

        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/tomcat5
        return $RETVAL
}

stop() {
    echo -n "Stopping $TOMCAT_PROG: "

    if [ -f /var/lock/subsys/tomcat5 ] ; then
      if [ -x /etc/rc.d/init.d/functions ]; then
          daemon --user $TOMCAT_USER $TOMCAT_SCRIPT stop
      else
          su - $TOMCAT_USER -c "$TOMCAT_SCRIPT stop"
      fi
      RETVAL=$?

      if [ 0 ]; then
        count=0;

        if [ -f /var/run/tomcat5.pid ]; then

            read kpid < /var/run/tomcat5.pid
            let kwait=$SHUTDOWN_WAIT

            until [ `ps --pid $kpid | grep -c $kpid` = '0' ] || [ $count -gt $kwait ]
            do
                echo "waiting for processes to exit";
                sleep 1
                let count=$count+1;
            done

            if [ $count -gt $kwait ]; then
                echo "killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
                kill -9 $kpid
            fi
        fi
    
                rm -f /var/lock/subsys/tomcat5 /var/run/tomcat5.pid
    fi

    fi
}


# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 2
        start
        ;;
  condrestart)
        if [ -f /var/run/tomcat5.pid ] ; then
                stop
                start
        fi
        ;;
  *)
        echo "Usage: $TOMCAT_PROG {start|stop|restart|condrestart}"
        exit 1
esac

exit $RETVAL

Modify Startup Symlinks

You will need to break the existing symlinks that point to the tomcat4 startup script and have them point to the tomcat5 script.

Your server's configuration may vary, but you should find similar links from the tomcat4 installation. Simply delete them and create new ones for Tomcat 5 that point to ../init.d/tomcat5


/etc/rc.d/rc0.d/K20tomcat4	/etc/rc.d/rc0.d/K20tomcat5
/etc/rc.d/rc1.d/K20tomcat4	/etc/rc.d/rc1.d/K20tomcat5
/etc/rc.d/rc2.d/S80tomcat4	/etc/rc.d/rc2.d/S80tomcat5
/etc/rc.d/rc3.d/S80tomcat4	/etc/rc.d/rc3.d/S80tomcat5
/etc/rc.d/rc4.d/S80tomcat4	/etc/rc.d/rc4.d/S80tomcat5
/etc/rc.d/rc5.d/S80tomcat4	/etc/rc.d/rc5.d/S80tomcat5
/etc/rc.d/rc6.d/K20tomcat4	/etc/rc.d/rc6.d/K20tomcat5

Get rid of mod_webapp

Plesk may have used mod_webapp to bridge Apache with Tomcat and pass all .JSP and servelet request to Tomcat. When upgrading Tomcat, you should also switch to the latest version of mod_jk

You will need to download and build the .so file and add this to your /etc/httpd/conf/htpd.conf file for Apache to include. Details of this process are not covered here, but should be a Google search away.
Also, you will most likely need to add a text file /etc/httpd/conf/workers.properties


worker.list= ajp13

# Definition for Ajp13 worker
#
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13

For each virtual host, you will need to give Apache several mod_jk configuration directives as listed here This step is covered at the end of this article.

Modify Tomcat's Startup Script /bin/catalina.sh

You can set the JAVA_HOME and CATALINA_HOME as environment variables if you like. Otherwise, you can set them directly in the catalina.sh file for Tomcat to pick up.
Of course you need to set these values to the version and the path that you used for both Java and Tomcat.


JAVA_HOME="/usr/lib/jvm/jdk1.5.0_06/"
CATALINA_HOME="/var/tomcat5/"
CATALINA_PID=/var/run/tomcat5.pid

Modify Tomcat's Config file /conf/server.xml

In this step, you can configure one or more domains under Plesk to serve a JSP webapp. Although this process is done by hand, you can configure scripts to modify this file when a new domain is added.

To add a webapp for a domain, you need to add a new host declaration.

Find the default virtual host:


      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost" appBase="webapps"
       unpackWARs="true" autoDeploy="true"
       xmlValidation="false" xmlNamespaceAware="false">

and add a new virtual host:


      <Host name="mydomain.com" debug="0" appBase="/home/httpd/vhosts/mydomain.com/webapps/"
       unpackWARs="true" autoDeploy="true"
       xmlValidation="false" xmlNamespaceAware="false">
       <Alias>www.mydomain.com</Alias>
      </Host>

Create a directory /home/httpd/vhosts/mydomain.com/webapps/ for each virtual host's webapp.

Change ownership of this directory so that Tomcat can work with the files chown tomcat5:tomcat5 /home/httpd/vhosts/mydomain.com/webapps/

And finally, for each virtual host that you define, you will need to modify the vhost.conf file to have Apache play nice with Tomcat. This step gives Apache the mod_jk configuration directives for your virtual host.

Restart Tomcat
You should now be ready to restart Apache and Tomcat


/etc/init.d/httpd restart
/etc/init.d/tomcat5 restart

Try connecting to one of your virtual hosts, and if this does not work, try using port 8080 http://www.mydomain.com:8080/ If port 8080 is working, there is a problem with Apache talking to Tomcat and you will need to check your mod_jk settings.

If all is working, try restarting your server to make sure the startup script works and Tomcat runs on startup. Nobody likes webserver downtime!

Some things to consider
This upgrade does break the Plesk / Tomcat relationship. If you have customers with their own domains, you may want to take additional steps such as defining the security policy for Tomcat, automating the addition of virtual hosts to server.xml, and exposing the Tomcat manager to each virtual host client so they can deploy .war files on their own.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.