Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changed Tomcat0 to Tomcat1 convention.

...

In this article I will give an example of setting up 2 Tomcat instances using the same version of Java as follows,

Tomcat0 Tomcat1 on Java 1.6.0_16 run by user serveradmin
Tomcat1 Tomcat2 on Java 1.6.0_16 run by user serveradmin

...

/opt/apache - root directory for Tomcat and any other Apache products

/opt/apache/tomcat.0 1 - directory for Tomcat 0 Tomcat1 running on the default port 8080
/opt/apache/tomcat.01/java - directory we will place java for Tomcat 0Tomcat1

/opt/apache/tomcat.1 2 - directory for Tomcat 1Tomcat2
/opt/apache/tomcat.12/java - directory we will place java for Tomcat 1 Tomcat2 running ont he port 8180

So as your first step, create the apache directory under opt,

...

Code Block
languagebash
su - serveradmin
cd ~
gunzip apache-tomcat-6.0.20.tar.gz
tar -xvpf apache-tomcat-6.0.20.tar # All the permissions will be kept
mv ./apache-tomcat-6.0.20/ ./tomcat.01/ # This will be Tomcat0Tomcat1
exit # Switch back to your staff account

...

Code Block
languagebash
mv ./java/ ./tomcat.01/

Log in as your staff account which has sudo access to perform the actual move to /opt/

Code Block
languagebash
sudo mkdir /opt/apache # just to organize things a bit better
cd /home/serveradmin
sudo mv ./tomcat.01/ /opt/apache/

Finally, if this is a multi-user machine, we secure tomcat from other users and processes. The only users should be serveradmin and staff.

Code Block
languagebash
cd /opt/apache
sudo chown -R serveradmin:staff ./tomcat.01/
sudo chmod -R 750 ./tomcat.01/

 

Fix Tomcat Logging

Out of the box Tomcat Logging has a bug where after catalina.out reaches a large file size over logging stops working. On top of that if your log file reaches 2GB Tomcat will fail to start and without reporting any errors. Yes this is pretty crazy that this is not fixed.

...

Code Block
languagebash
serveradmin 12150 13290   1 11:51:28 pts/2       0:10 /opt/apache/tomcat.01/java -Djava.util.logging.manager=org.apache.juli.ClassLoad
serveradmin  5906 13290   0   Jul 24 ?           6:17 /opt/apache/tomcat.12/java -Djava.util.logging.manager=org.apache.juli.ClassLoad

...

Setting up and running Tomcat with serveradmin has the advantage that you can manage the Application server without having to go into root. We want to ensure that only serveradmin starts Tomcat to prevent any issues with permissions. For example, once you start Tomcat 0s Tomcat1 as root you may find that log files spawned from that startup can no longer be managed by serveradmin. Also, by running Tomcat 0s Tomcat1s serveradmin rather than root is safer from a security standpoint.

...

Code Block
su - serveradmin

Modify Tomcat0Tomcat1's /bin/startup.sh and /bin/shutdown.sh to only allow serveradmin to start and stop Tomcat,

...

Code Block
languagebash
su - serveradmin # If you are not already serveradmin.
cd /opt/apache/tomcat.01/bin
./version.sh
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program

So now let's create the setenv.sh file. As serveradmin create /opt/apache/tomcat.01/bin/setenv.sh using your favourite editor. Your file contents will look like this,

...

Code Block
languagebash
./version.sh
Using CATALINA_BASE:   /opt/apache/tomcat.01
Using CATALINA_HOME:   /opt/apache/tomcat.01
Using CATALINA_TMPDIR: /opt/apache/tomcat.01/temp
Using JRE_HOME:       /opt/apache/tomcat.01/java
Server version: Apache Tomcat/6.0.20
Server built:   May 14 2009 01:13:50
Server number:  6.0.20.0
OS Name:        Linux
OS Version:     2.6.31-302-rs
Architecture:   amd64
JVM Version:    1.6.0_16-b01
JVM Vendor:     Sun Microsystems Inc.

Using this method, you can have different Tomcat instances running different versions of Java and control when you want to move between Java versions.

Setup

...

Tomcat2

In this example we are going to run with 2 instances of tomcat where we will load balance between them. So we duplicate the tomcat directory with a slightly different directory name,

Code Block
languagexml
cd /opt
sudo cp -Rp ./apache/tomcat.01/ ./apache/tomcat.12/ # This will be Tomcat 1Tomcat2

Leave Tomcat0 Tomcat1 on the default Tomcat6 ports,

  • 8005 - for shutdown
  • 8009 - for JK Mod
  • 8080 - regular port similar to 80
  • 8443 - ssl port similar to 443

Tomcat1 Tomcat2 will use the following ports,

...

The very first step is to verify that the ports for Tomcat1 Tomcat2 are not being used.

Code Block
netstat -an | grep LISTEN | grep 8105
netstat -an | grep LISTEN | grep 8109
netstat -an | grep LISTEN | grep 8180
netstat -an | grep LISTEN | grep 8543
 

If you get no results then there are no listening ports.

Change Tomcat1 Tomcat2 to use use the following ports by editing /opt/tomcat.12/conf/server.xml. Use an editor to search and replace or more quickly using the following sed commands to do modify your file,

Code Block
cd /opt/apache/tomcat.12/conf
sed -i 's/8005/8105/' server.xml
sed -i 's/8009/8109/' server.xml
sed -i 's/8080/8180/' server.xml
sed -i 's/8443/8543/' server.xml

...

Code Block
languagebash
su - serveradmin

cd /opt/apache/tomcat.01/bin/
./startup.sh
cd /opt/apache/tomcat.12/bin/
./startup.sh

netstat -an | grep LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN
tcp6       0      0 :::8009                 :::*                    LISTEN
tcp6       0      0 :::8080                 :::*                    LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
unix  2      [ ACC ]     STREAM     LISTENING     7376     @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     11434    /var/run/fail2ban/fail2ban.sock
unix  2      [ ACC ]     STREAM     LISTENING     21228    /var/run/apache2/cgisock.4973

Notice that here we have started Tomcat0 Tomcat1 and it is listening on port 8009 and 8080.

...