RSS Feed Subscribe to RSS Feed

 

Setting up multiple instances of Tomcat

With multiple tomcat instances, each can run in its own JVM, have its own configuration and can be started/stopped independently.

One approach to doing this would be to have multiple, full tomcat installations. This article instead details how to install tomcat once (in CATALINA_HOME) but have multiple independent instances (by utilizing CATALINA_BASE). This is a more streamlined approach that makes creating multiple instances easier and also simplifies upgrades/rollbacks of tomcat.

Version info

I have tested with tomcat 6 and tomcat 7 and it seems to work well with both.
These notes relate to a windows install, but should be easily modified for *nix.

Steps

First, download and install tomcat to whatever directory you choose. We will refer to this directory as CATALINA_HOME.
Now, repeat the following steps for each instance of tomcat you want to setup.

  1. Create a new folder which will be your tomcat instance. Note that this should preferably not be in CATALINA_HOME. We will refer to this tomcat instance folder as CATALINA_BASE.
  2. Copy the conf directory from CATALINA_HOME to CATALINA_BASE. (I think, strictly speaking, just copying the server.xml would suffice, but no harm in copying the whole directory for now).
  3. Isolate your tomcat instance
    Edit CATALINA_BASE\conf\server.xml file so that the following ports do not interfere with other Tomcat instances:

    • HTTP connector port (e.g. use 8080, 8081 …)
    • Shutdown port (e.g. use 8005, 8006…)
    • AJP port (e.g. use 8009, 8010…)

    Obviously if you have more than 4 instances, the shutdown and AJP ports will start to clash, so adapt accordingly (e.g. Shutdown = 9000, 9001; AJP = 9500, 9501 and HTTP continues with =8084, 8085…).

  4. Create the following empty directories under CATALINA_BASE
    • logs
    • temp
    • work
    • webapps

    Tomcat will use the first 3 as part of its normal running. The webapps directory is where you will place your projects.

  5. Optional: Set up the default page and tomcat manager
    Copy CATALINA_HOME\webapps\ROOT and CATALINA_HOME\webapps\manager to CATALINA_BASE\webapps.
    This step is optional but will allow you access the default page (useful for ensuring tomcat is running properly) as well as the Tomcat Manager page (useful for managing tomcat and your deployed applications).
  6. Create startup and shutdown scripts
    Create a bin directory in CATALINA_BASE.
    In that bin directory, create a file called startup.bat like this:

        set CATALINA_BASE=C:\tomcats\tomcat-x
        set CATALINA_HOME=C:\tomcats\apache-tomcat-7.0.21
        C:\tomcats\apache-tomcat-7.0.21\bin\startup.bat

    And create a file called shutdown.bat like this:

        set CATALINA_BASE=C:\tomcats\tomcat-x
        set CATALINA_HOME=C:\tomcats\apache-tomcat-7.0.21
        C:\tomcats\apache-tomcat-7.0.21\bin\shutdown.bat

    Note that these are just basic scripts; you can obviously customize as you see fit. You can also optionally create a setenv.bat in the same bin directory to set any environment variables.

That’s it! From each instance directory, run CATALINA_BASE\bin\startup.bat and you will have multiple instances of tomcat up and running simultaneously.

Notes

I had previously suggested in this blog that this approach was suitable for setting up a dev, QA and prod instances of tomcat. As Luis alluded to in the comments below, the problem is that all instances are effectively upgraded when CATALINA_HOME is. So this approach is best suited to a setup where all environments can be upgraded at once. In a dev/QA/prod setup, you are likely to want to upgrade then independently.

Related links

Troubleshooting

If you are having problems, it can be useful to have tomcat run in the same window you start it in (rather than popping up a window with an error that immediately disappears!). To do this, modify the last line in CATALINA_HOME\bin\startup.bat by replacing the word start with the word run

Tags: , ,

35 Responses to “Setting up multiple instances of Tomcat”

  1. Jeff |

    Good explanation, thanks. One correction: “First, download and install tomcat to whatever directory you choose. We will refer to this directory as CATALINA_BASE.” I think that you mean CATALINA_HOME.

  2. Shaun Abram |

    Hi Jeff,
    Glad you liked the article. And thanks for spotting the typo! Fixed now…

    Shaun

  3. alz |

    When I tried to create instances a window opened and suddenly disappeaded-in trouble shooting you said “CATALINA_HOME\bin\startup.bat by replacing the word start with the word run” what exactly you tried to say here..please help

  4. Shaun Abram |

    The last line in your startup.bat file should be something ike this:
        call “%EXECUTABLE%” start %CMD_LINE_ARGS%
    So, update it to be:
        call “%EXECUTABLE%” run %CMD_LINE_ARGS%
    Alternatively, call ‘catalina.bat run’ instead.
    Either way, you should now be able to see the details of the error that is causing tomcat to not start…

  5. alz |

    Thanks for your swift reply…I have got an error saying

    INFO: The stop() method was called on component [StandardEngine[Catalina].Stand
    rdHost[localhost].StandardContext[/Some Project]] after stop() had already
    een called. The second call will be ignored.

    Since I am new to this I am not able to rectify…if you could help me that would be really great..thanks in advance

  6. Shaun Abram |

    The message you posted (“The stop() method was called…”) seems to be an INFO message only, not an error, so I doubt it is the cause of your problems. Are you getting other errors? You can check logs/catalina.out if you have already closed your console.

    Without seeing the actual errors, you might want to check what you have in your webapps folder and remove any old projects.

  7. alz |

    Once the server is started I got the following and server immediately stoped…Please can you help me on this…

    SEVERE: StandardServer.await: create[localhost:8005]:
    java.net.BindException: Address already in use: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365
    at java.net.ServerSocket.bind(ServerSocket.java:319)
    at java.net.ServerSocket.(ServerSocket.java:185)

  8. Shaun Abram |

    “java.net.BindException: Address already in use” means you probably already have an instance of tomcat running.
    Run bin/shutdown.bat to make sure it is stopped before trying to start.

  9. alz |

    Thank you so much…you really awesome..it worked now..thanks heaps

  10. hg |

    excellent works! But I don’t know how to configure different JVM form each Tomcat instance, would you please give me some suggestion?

  11. Shaun Abram |

    Glad it worked for you hg.
    Regarding different JVMs, if you mean two instances of the same JVM version, it works that way out of the box. Each tomcat starts in a new JVM.

    If you mean you need each tomcat to use a different JVM version, you need to modify your Java environment variables for each instance.

  12. hg |

    Would you please give some detail information?

  13. Shaun Abram |

    Sure, if you can give some detail question.
    What problem are you having? What part of my previous response wasn’t clear?

  14. hg |

    Shaun, thanks for quickly reply. The question is:
    I deploy two instance of Tomcat according your article, but i want use different JVM for this two instance, for example one use Sun JDK and other use JRockit, What should I do?

  15. hg |

    “If you mean you need each tomcat to use a different JVM version, you need to modify your Java environment variables for each instance.”

    But I don’t know where and how to configure, please give some detail info!

  16. Shaun Abram |

    Try setting a different value for JAVA_HOME for each tomcat instance. For example, customize the startup script mentioned in step 6 above.
    In CATALINA_BASE1\bin\startup.bat use
        set JAVA_HOME = C:\JDK1\…
    In CATALINA_BASE2\bin\startup.bat use
        set JAVA_HOME = C:\JDK2\…

    Let me know if that works.

  17. hg |

    Shaun, thanks!
    It’s works!
    I deploy two instance of Tomcat 7.0.27.
    One instance use “Oracle Corporation: 1.6.0_31 (Oracle JRockit(R))” and the other instance use “Oracle Corporation: 1.7.0_03 (Java HotSpot(TM) Server VM)”.

  18. Shaun Abram |

    Awesome! Glad I could help and that it works for you.

    On a side note, I was at an Oracle Developer Day today and found out that they are merging JRockit and HotSpot in to a single JVM. It seems all the performance features of JRockit will be ported to the HotSpot codebase and released with Java 8.

  19. hg |

    @Shaun
    Got it! Thanks!
    But what’s time they release J8?

  20. Shaun Abram |

    Java8 is scheduled for mid 2013 – about a year from now. Probably a bit longer than you are prepared to wait!

    Looks like it will be a very big release, with Lambdas (closures), Project Jigsaw (modularization) and a bunch of smaller changes.

  21. hg |

    @Shaun
    Thanks again!

  22. Manoj |

    Hi Shauan,

    I like ur article its easy to implement.
    Can you please put some light on how to configure fail-over mechanism and the load balancing system with tomcat 7 step by step as explained in this tutorial ?

    That will be very much helpful.

    Thanks in advance.

  23. Shaun Abram |

    Hey Manoj,
    I’d love to, but don’t have the time. Perhaps you can put one together based on your research and post a link here :-)

    Shaun

  24. Od |

    Hi!

    Why not to download the first instance, copy everything and change the other’s server.xml file

  25. Shaun Abram |

    Hi Od,
    Good question.
    The main reason is that the approach described here simplifies upgrades/rollbacks of tomcat. You can upgrade/rollback all tomcat installs in a single place – CATALINA_HOME.
    Other lesser reasons would be that this approach saves disk space (you are not copying all files for every install, just those needed to customize your install). Also, if you try copying an existing install, you would end up copying all the existing logs and any projects in webapps etc, which would then need to be deleted etc. Starts to get a little messy.

    Hope that answers your question.

    Shaun

  26. Luis |

    Hi Shaun,

    Thanks for the guide. I used it earlier this year to set up dev, qa, and prod instances of our web app using Tomcat 6. We recently received a new version of our web app that requires Tomcat 7. Is it possible to upgrade our separate instances individually? How would we go about upgrading our dev instance to tomcat 7 while qa and prod continued running on 6? Any help would be appreciated. thanks

    Luis

  27. sabram |

    Luis,
    Apologies for the delay in responding. You raise a good point. Using a single tomcat install (CATALINA_HOME) with multiple independent instances (CATALINA_BASE) means that all instanced are effectively upgraded when CATALINA_HOME is.

    So, using this approach when the different instances need to be upgraded independently is not a good approach. I have updated the post to reflect this (see the Notes section).

    Thanks,

    Shaun

  28. Nagendra |

    I got :

    Using CLASSPATH: “C:\PROGRA~1\APACHE~1\apache-tomcat-7.0.23\bin\bootstrap.jar;C:\PROGR
    tomcat-juli.jar”
    Listening for transport dt_socket at address: 8000
    Sep 28, 2012 3:44:08 PM org.apache.catalina.startup.Catalina initDirs
    SEVERE: Cannot find specified temporary folder at C:\tomcats\tomcat-x\temp
    Sep 28, 2012 3:44:08 PM org.apache.catalina.startup.Catalina load
    WARNING: Unable to load server configuration from [C:\tomcats\tomcat-x\conf\server.xml]
    Sep 28, 2012 3:44:08 PM org.apache.catalina.startup.Catalina initDirs
    SEVERE: Cannot find specified temporary folder at C:\tomcats\tomcat-x\temp
    Sep 28, 2012 3:44:08 PM org.apache.catalina.startup.Catalina load
    WARNING: Unable to load server configuration from [C:\tomcats\tomcat-x\conf\server.xml]
    Sep 28, 2012 3:44:08 PM org.apache.catalina.startup.Catalina start
    SEVERE: Cannot start server. Server instance is not configured.

  29. Nagendra |

    It worked, my folder was not what I gave in startup.bat:

    C:\tomcats\tomcat-x

  30. Java Tutorials Blog |

    I even created a tool to automate the above steps. pls. see if it looks helpful to you for installing multiple tomcat instances

  31. siva |

    thanks a lot it is very helpful

  32. nginx tomcat |

    i wish it is possible to just have 1 tomcat and yet each application in webapps will run on separate VMs

  33. sai |

    thanks..

  34. Srisri |

    Hi Shaun,

    I just need one clarification.

    If I have lot of space on my disk and if I install Tomcat 7 multiple times then is it equal to what you’ve explained above or is there any problems involved with multiple installations.
    Because I want to do in such a way to get “Windows Services” (where I can setup ‘Automatic’ when system restarts, but it’s not possible if I do copying folders and updating server.xml and starts)

    FYI -
    my installs -
    Tomcat7
    Tomcat701

    Thanks In adv

  35. Bhargava |

    Thanks for the info,very useful

Leave a Reply