Adsense 1

Thursday, April 03, 2008

How to configure 2 or more tomcat instances in the same server

We have already seen how to install tomcat in Linux.

Now let us create one more instance. As you may already know we cannot have two services listening on the same ip & port. So, for the second instance of the tomcat we have to change the port settings.

This tutorial is in continuation of my first post on installing tomcat. You can read it here. I assume that you already have one tomcat instance up and running (as a special user 'myuser').

Login as or su as myuser:
# su - myuser
$ cd ~

$ unzip /your/download/directory/apache-tomcat-x.x.x.zip (or)

if downloaded gzip file then
$ tar zxfv /your/download/directory/apache-tomcat-x.x.x.tar.gz

$ mv apache-tomcat-x.x.x test-tomcat1
$ cd ~/test-tomcat1/conf

Changing the default ports:
$ cp server.xml server.xml.orig
$ vi server.xml

Change the shutdown port 8006 (as shown in below line):

server port="8006" shutdown="SHUTDOWN"

to:
server port="9006" shutdown="SHUTDOWN"


Change non-ssl http 1.1 Connector port on 8080 (as shown in below line):

Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" ...

to:
Connector port="9080" maxHttpHeaderSize="8192" maxThreads="150" ...


Ajp connector port 8009 (as shown in below line):
connector port="8009" enablelookups="false" redirectport="8443" protocol="AJP/1.3"

to:
connector port="9009" enablelookups="false" redirectport="9443" protocol="AJP/1.3"


If you have enabled proxied http connector port 8082 (found in line below):
Connector port="8082" maxThreads="150" minSpareThreads="25" ...

Change it to:
Connector port="9082" maxThreads="150" minSpareThreads="25" ...


If you have enabled https port 8443 (found in lines below):

connector
port="8443" maxhttpheadersize="8192"
keystorefile="/home/tomcat/keystore/tomcat.key" scheme="https"
secure="true" clientauth="false" sslprotocol="TLS"

Change it to:
connector
port="9443" maxhttpheadersize="8192"
keystorefile="/home/myuser/keystore/domain.key" scheme="https"
secure="true" clientauth="false" sslprotocol="TLS"


Now give executable permission to the scripts:
$ cd ~/test-tomcat1/bin/
$ chmod u+x *.sh

Create a directory for your Servlet's log files:
$ mkdir ~/myservlet1

And add aliases:

$alias tomcat1-down=~/test-tomcat/bin/shutdown.sh
$ alias tomcat1-up=cd ~/myservlet1; ~/test-tomcat/bin/startup.sh

Remember to put alias entries in ~/.bashrc so that they are persistent.

Startup tomcat:
$ tomcat1-up

Open your browser. You must be able to view both tomcat test pages by now.
Page1: http://localhost:8080/
Page2: http://localhost:9080/

Tuesday, April 01, 2008

Installing tomcat in Linux

We will need Java to install tomcat. You can download it here. I used JDK 6 Update 5.

I am not going into the details of installing jdk. Make a note of where you install it. My jdk location is: /usr/java/jdk1.6.0_5

Download tomcat from here. I used 6.0.16

By default tomcat listens on port 8080, so you will not need super user permissions. If you are using any port <1024>
# passwd myuser

su - myuser

$ unzip /your/download/directory/apache-tomcat-x.x.x.zip (or)

if downloaded gzip file then
$ tar zxfv /your/download/directory/apache-tomcat-x.x.x.tar.gz

This creates a directory apache-tomcat-x.x.x. It is always a good practice to rename the directory and give it some meaning full name:
$ mv apache-tomcat-x.x.x test-tomcat

Set JAVA_HOME
$ export JAVA_HOME=/usr/java/jdk1.6.0_5/bin

Optionally set CATALINA_HOME (If you are running only one instance).
$ export CATALINA_HOME=/opt/test-tomcat

Remember to put the above entries in user's .bash_profile.

Starting tomcat:

$ cd ~/test-tomcat/bin/
$ chmod u+x *.sh ( so that the sh scripts can be executed)
$ cd ~/myservlet/ (where you store all config files needed by your servlet)
$ ~/test-tomcat/bin/startup.sh

Note: Normally any output/log file generated by your Servlet will be created in the directory where you started tomcat from. I generally use a directory named after my servlet. For this tutorial i am using used ~/myservlet as the directory.

If your servlet isn't generating logs of its own then you can view the logs in ~/test-tomcat/logs/ directory. This directory has many files. Two files are particularly important.
1. catalina.out is the main log file (watch out this grows faster in a big site with more hits & logging).
2. There are also individual files for each day (eg format: catalina-yyyy-mm-dd.log).

As i said above, tomcat service listens on port 8080 and serves webpages/ servlets through it. It also listens on following default ports:

8005 - Shutdown port (you can telnet to this port from localhost and shutdown the tomcat service by issuing SHUTDOWN in the telnet session. Afaik, You cannot enable this for remote connections).

8009 - AJP connector port (used to connect this tomcat instance with other tomcat or apache instances. Apache communicates with tomcat through this port).

8443 - SSL port (disabled by default).

To make life easier add aliases for shutting down and starting the tomcat instances:
$alias tomcat-down=~/test-tomcat/bin/shutdown.sh

$ alias tomcat-up= cd ~/myservlet ; ~/test-tomcat/bin/startup.sh

Remember to put alias entries in ~./bashrc. Also note that the above alias entry will work only if tomcat is installed in that user's home directory.