You can always install more than one tomcat and change the ports and run many instances you want , However there is an easier way of doing it but it works only for tomcat 6.
simply start by downloading apache tomcat 6.
in the same directory of your tomcat create folder and name it as shared. and inside this folder create two folders, conf and logs. you can use conf folder to keep common configuration files for your multiple tomcat instances such as server.xml,tomcat-users.xml.
you can have a server.xml file as follows :
<?xml version='1.0' encoding='utf-8'? > <Server port="${shutdown.port}" shutdown="SHUTDOWN" > <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" / > <Listener className="org.apache.catalina.core.JasperListener" / > <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" / > <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" / > <GlobalNamingResources > <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="${catalina.base}/../shared/conf/tomcat-users.xml" / > <Resource name="${tomcat.jndi.resources.name}" auth="${tomcat.jndi.resources.auth}" type="${tomcat.jndi.resources.type}" driverClassName="${tomcat.jndi.resources.driverclassname}" url="${tomcat.jndi.resources.url}" username="${tomcat.jndi.resources.username}" password="${tomcat.jndi.resources.password}" maxActive="${tomcat.jndi.resources.maxactive}" maxIdle="${tomcat.jndi.resources.maxidle}" maxWait="${tomcat.jndi.resources.maxwait}" / > </GlobalNamingResources > <Service name="Catalina" > <Connector port="${http.port}" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" / > <Engine name="Catalina" defaultHost="localhost" > <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/ > <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false" > </Host > </Engine > </Service > </Server >
The variables ${XXX} should be defined specifically for each tomcat instance.
in the same directory of your tomcat create a folder and name it as tomcat_instance1. it should have the same directory structure as normal tomcat installation.
so in the bin directory create a file called setenv.sh. this file is used to set environment variables for this specific tomcat instance. the content of the file can be as follows
CATALINA_OPTS="-Xmx512m -XX:+HeapDumpOnOutOfMemoryError" CATALINA_HOME=/usr/share/tomcat6/apache-tomcat-6.0.20 CATALINA_BASE=/usr/share/tomcat6/tomcat_instance_1 JAVA_HOME=/opt/java/jdk CATALINA_PID=$CATALINA_BASE/logs/tomcat.pid
In the conf directory of tomcat_instance1 create a file called catalina.properties. this file is used to store the values for the variables refered in server.xml in shared directory.
the file content is as follows :
#changing the ports for your tomcat instance is just a matter of changing these lines below shutdown.port=8005 http.port=8080 jmx.port=6969 package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.,sun.beans. package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper. common.loader=${catalina.home}/lib,${catalina.home}/lib/*.jar server.loader= tomcat.util.buf.StringCache.byte.enabled=true #this is jndi settings for the database, remember these variables are used in server.xml in shared directory tomcat.jndi.resources.name=jndiname tomcat.jndi.resources.auth=Container tomcat.jndi.resources.type=javax.sql.DataSource tomcat.jndi.resources.driverclassname=org.postgresql.Driver tomcat.jndi.resources.url=jdbcurl tomcat.jndi.resources.username=username tomcat.jndi.resources.password=password tomcat.jndi.resources.maxactive=100 tomcat.jndi.resources.maxidle=30 tomcat.jndi.resources.maxwait=10000
the only thing you need more is run.sh file which you should place it in the same directory of your tomcat6 intsallation. and the content of the file is as follows :
#!/bin/bash syntax () { echo "Usage:" echo "./run.sh [start|stop|run] [instance number]" } PRG="$0" while [ -h "$PRG" ]; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`/"$link" fi done # Get standard environment variables PRGDIR=`dirname "$PRG"` #Absolute path PRGDIR=`cd "$PRGDIR" ; pwd` #Setup CATALINA_HOME to point to our binaries [ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/apache-tomcat-6.0.20" ; pwd` #Make sure the instance directory exists. if [ -z "$2" ]; then echo "Second argument must be an instance number." syntax exit 1 elif [ ! -r "$PRGDIR/tomcat_inst_$2" ]; then echo "Instance directory $PRGDIR/tomcat_inst_$2 does not exist." exit 1 fi #Setup the instance flag CATALINA_INSTANCE=tomcat_inst_$2 JAVA_OPTS="$JAVA_OPTS -Dcatalina.tomcat_inst=$2" #Setup the Tomcat logger JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" #Setup our CATALINA_BASE flag CATALINA_BASE="$PRGDIR/tomcat_inst_$2" #Setup our Process ID tracker CATALINA_PID="$PRGDIR/shared/logs/tomcat_inst_$2.pid" #Setup logging LOGGING_CONFIG="-Djava.util.logging.config.file=$PRGDIR/shared/conf/logging.properties" export CATALINA_HOME export CATALINA_BASE export CATALINA_PID export LOGGING_CONFIG export JAVA_OPTS #SCRIPT_TO_RUN=$CATALINA_HOME/bin/echo.sh SCRIPT_TO_RUN=$CATALINA_HOME/bin/catalina.sh if [ -r $CATALINA_BASE/conf/server.xml ] ; then SERVER_CONFIG=$CATALINA_BASE/conf/server.xml else SERVER_CONFIG=$PRGDIR/shared/conf/server.xml fi if [ "$1" = "start" ] ; then $SCRIPT_TO_RUN start -config $SERVER_CONFIG elif [ "$1" = "run" ] ; then $SCRIPT_TO_RUN run -config $SERVER_CONFIG elif [ "$1" = "stop" ] ; then $SCRIPT_TO_RUN stop -config $SERVER_CONFIG else echo "Invalid first parameter" syntax exit 1 fi
And you can run your tomcat instance with this command : ./run.sh start 1
Hope it works for you too 🙂
fabio said
Thanx a lot for sharing your tomcat configuration!
fabio said
I found just a small problem because the pid file defined inside setenv.sh does not match with the pid file defined in run.sh
Inside setenv.sh you define:
CATALINA_PID=$CATALINA_BASE/logs/tomcat.pid
But inside run.sh you define:
CATALINA_PID=”$PRGDIR/shared/logs/tomcat_inst_$2.pid”
In order to manage properly my instances I’ve comment the CATALINA_PID definition inside run.sh: by doing this tomcat use the defaul pid file $CATALINA_BASE/logs/tomcat.pid