How to install Multiple instances of tomcat on a single ubuntu server

This article is the first part of my serie : load balancing using mod_jk

Let’s install first java using openjdk:

[bash]
$ sudo apt-get install openjdk-7-jre
[/bash]

In fact in the previous command we are just installing the Java Runtime Environment (JRE). If there is a case where you would need the Development ToolKit (JDK) then use openjdk-7-jdk instead.
Now that we have our java installed let’s proceed with the tomcat.

[bash]
$ sudo apt-get install -y tomcat6 tomcat6-users
[/bash]

Normally tomcat6 itself would have been enough but because we needed to install multiple instance of it, there is a very convenient command to cater for that bundled with the tomcat6-users packages.

We actually needed some files and components provided by tomcat6 command, that was the only reason we installed it, we could have actually done without it if not. But as is we don’t want tomcat with all the defaults stuff to get in our way specially when restarting the box(machine) or anything else, we will disable it and remove it’s scripts from all the auto start stop etc stuff that ubuntu itself runs.

Let’s stop the default tomcat6 that we installed.
[bash]
$ sudo service tomcat6 stop
[/bash]

The let’s disable it(removing its scripts from update-rc.d)
[bash]
$ sudo update-rc.d -f tomcat6 remove
[/bash]

That’s done now let’s go to where we want to actually create our instances. Let me stop here and give a brief overview of what we’ve done so far. we installed java and used(you can say trick in this case 🙂 ) tomcat6 to get some structure, files and folders done for us. But in reality we never had the intention of using that tomcat instead we would use that special utility that comes with the tomcat6-users package. i will be using /opt/tomcat6 folder for this tutorial but feel free to use any path at your convenience.

Moving to /opt path
[bash]
$ cd /opt
[/bash]

Creating tomcat6 directory
[bash]
$ sudo mkdir tomcat6
$ sudo chown tomcat6:root #Let’s own the folder to simplify access privileges to that folder
[/bash]

For this tutorial we are creating 2 instances of tomcat. Let’s call them timo and pumba

[bash]
$ sudo tomcat6-instance-create -p 8081 -c 8006 timo
[/bash]
[bash]
$ sudo tomcat6-instance-create -p 8082 -c 8007 pumba
[/bash]

-p specifies the running port of the instance and -c the shutdown port. according to this documentation

Normally we would have stopped here but let’s configure a little utility to ease our life.
[bash]
$ cd /etc/init.d
[/bash]
[bash]
$ sudo cp tomcat6 timo
$ sudo cp tomcat6 pumba
[/bash]

Using nano editor we are going to personalize our startup/shutdown script.
[bash]
$ sudo nano timo
[/bash]
Let’s change the following values
[bash]
NAME=timo
DESC="Tomcat Timo servlet engine"
#DEFAULT=/etc/default/$NAME
DEFAULT=/etc/default/tomcat6
JVM_TMP=/tmp/$NAME-tmp
CATALINA_HOME=/usr/share/tomcat6
#CATALINA_BASE=/var/lib/$NAME
CATALINA_BASE=/opt/tomcat6/timo
[/bash]

[bash]
NAME=pumba
DESC="Tomcat Pumba servlet engine"
#DEFAULT=/etc/default/$NAME
DEFAULT=/etc/default/tomcat6
JVM_TMP=/tmp/$NAME-tmp
CATALINA_HOME=/usr/share/tomcat6
#CATALINA_BASE=/var/lib/$NAME
CATALINA_BASE=/opt/tomcat6/pumba
[/bash]

After this edit, let’s automate their shutdown and startup.
[bash]
$ sudo update-rc.d timo defaults 90
$ sudo update-rc.d pumba defaults 90
[/bash]

Voila!, now you can start your timo instance with:
[bash]
$ sudo service timo start # or shutdown sudo service timo stop
[/bash]

Same for pumba

[bash]
$ sudo service pumba start # or shutdown sudo service pumba stop
[/bash]

Stay tune for the next post where i show how i load balance the timo and pumba with apache2 mod_jk

25 thoughts on “How to install Multiple instances of tomcat on a single ubuntu server

  1. For those here after I’m gone. This procedure was very helpful especially some of the explanations. I think the procedure is missing copying a policy.d directory from /etc/tomcat7 to your private tomcat instance conf directory. It worked for me.

    1. Glad to see that. I have myself had the chance to work on tomcat7 and could verify the adding of policy.d directory. I wasn’t necessary in tomcat6. I think I will add a note tomcat7 policy.d directory.

  2. I followed the advice as it was but for some reason I get an error when I try to start the instance for one or the other.
    The error I get is: invalid CATALINA_BASE /opt/tomcat7/geoserver.

    Any way I can rectify this issue or if I have done something wrong?

    1. Hello Siobhan,

      I am sorry it’s not working for you. But I suspect it has to do with the editing of the file(s) in your /etc/init.d. what /etc/init.d/yourtomcatinstancefile looks like. put it in pastie.org and give us the link here.

  3. here is the link for one of them

    hope that helps otherwise let me know what else I need to add to it please.

      1. for some reason its not linking correctly so I’m pasting it here at the bottom

        PATH=/bin:/usr/bin:/sbin:/usr/sbin
        NAME=geoserver
        DESC=”=Tomcat geoserver servlet engine”
        #DEFAULT=/etc/default/$NAME
        DEFAULT=/etc/default/tomcat7
        JVM_TMP=/tmp/$NAME-tmp
        CATALINA_HOME=/usr/share/tomcat7
        #CATALINA_BASE=/var/lib/$NAME
        CATALINA_BASE=/opt/tomcat7/geoserver

        #Directory where the Tomcat7 binary distribution resides
        CATALINA_HOME=/usr/share/tomcat7

  4. There might be some problem in starting tomcat instances because of policy files. In that case one just need to copy the policy folders from old tomcat6 to new tomcat folders at correct places.

    Not mandatory but good to have- it will be nice if you will copy the original ROOT folder too.

  5. Running a web application might show errors like “java.lang.IllegalStateException: No output folder” for JSPs.
    The reason is that newly created directory “work” needs to have a folder named “Catalina” (Case sensitive) inside it. if its not there create one and assign proper permission. To be on the safer side give “777” permissions.

    Also, there is a jar named “tomcat-juli.jar” that needs to be placed in “/bin” folder.

    Restart your instance and the exception will disappear.

  6. Many thanks for your article. Regarding the step “sudo update-rc.d -f tomcat6 remove”, how would you comment the following paragraph from “man update-rc.d”:

    ” A common system administration error is to delete the links with the thought that this will “disable” the service, i.e., that this will prevent the service from being started. However, if all links have been deleted then the next time the package is upgraded, the package’s postinst script will run update-rc.d again and this will reinstall links at their factory default locations.”
    ?

  7. It worked fine with me except that using the services created in the init.d, when I restart one instance it closes the other. so I had to run them without the service name using the ./startup.sh directly from the bin directory.

    1. for tomcat 8 i needed to add a link to the policy.d dir like this (run from the conf dir in your new tomcat8):

      ln -s /etc/tomcat8/policy.d policy.d

Leave a Reply

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

captcha * Time limit is exhausted. Please reload the CAPTCHA.

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

Scroll to top