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:

$ sudo apt-get install openjdk-7-jre

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.

$ sudo apt-get install -y tomcat6 tomcat6-users

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.

$ sudo service tomcat6 stop

The let’s disable it(removing its scripts from update-rc.d)

$ sudo update-rc.d -f tomcat6 remove

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

$ cd /opt

Creating tomcat6 directory

$ sudo mkdir tomcat6
$ sudo chown tomcat6:root #Let's own the folder to simplify access privileges to that folder

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

$ sudo tomcat6-instance-create -p 8081 -c 8006 timo
$ sudo tomcat6-instance-create -p 8082 -c 8007 pumba

-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.

$ cd /etc/init.d
$ sudo cp tomcat6 timo
$ sudo cp tomcat6 pumba

Using nano editor we are going to personalize our startup/shutdown script.

$ sudo nano timo

Let’s change the following values

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
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

After this edit, let’s automate their shutdown and startup.

 $ sudo update-rc.d timo defaults 90
 $ sudo update-rc.d pumba defaults 90

Voila!, now you can start your timo instance with:

$ sudo service timo start # or shutdown sudo service timo stop

Same for pumba

$ sudo service pumba start # or shutdown sudo service pumba stop

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

22 Comments

  1. Lee

    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.

    Reply
  2. kodjo-kuma djomeda (Post author)

    Thank you Lee, am glad, this helped. About the policy.d I will read more on it and update this post or write a new one with tomcat7.

    Reply
  3. Eunice Obugyei

    Very helpful article. Way better tutorial than most of the ones I saw on the same topic on other sites. @Lee, your comment saved me some hours as well.

    Reply
    1. kodjo-kuma djomeda (Post author)

      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.

      Reply
  4. Pingback: How to reverse proxy with apache2 with mod_jk load balancing | My Coding Pains

  5. pradeep

    when starting instane error showing like this “invalid CATALINA_BASE: /var/lib/pradeep”

    Reply
  6. Siobhan

    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?

    Reply
    1. kodjo-kuma djomeda (Post author)

      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.

      Reply
  7. Siobhan

    here is the link for one of them

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

    Reply
    1. Siobhan
      Reply
      1. Siobhan

        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

        Reply
      2. kodjo-kuma djomeda (Post author)

        Hello Sorry for the long silence, I have been a little busy. Are you sure you created the server at

         /opt/tomcat7/geoserver

        ?

        Reply
  8. Ankur

    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.

    Reply
  9. kodjo-kuma djomeda (Post author)

    Hello Ankur, I have not encountered this in tomcat6. However @Lee has already pointed this out for tomcat7 which I had verify myself.

    Reply
  10. Ankur

    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.

    Reply
  11. Valery

    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.”
    ?

    Reply
  12. Yasser Hesham

    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.

    Reply
    1. kodjo-kuma djomeda (Post author)

      Hi Yasser,

      this is unusual. This never happened to me. Does it throw any error?

      Reply
  13. tibi

    for tomcat 8 it is
    apt-get install tomcat8-user

    (without the s at the end)

    Reply
    1. tibi

      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

      Reply
  14. tibi

    if i need a manager for each of my tomcats do i need to install one in each?

    Reply
    1. kodjo-kuma djomeda (Post author)

      Hello Tibi,

      I have never used a tomcat manager on production so I have not tested that. But it would seem logical it’s a manager per instance. Based on this introduction page ,the manager is port bound. I hope this helps

      Reply

Leave a Comment

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

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