Java and Technology weblog
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.
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.
- 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.
- 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).
- 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…).
- Create the following empty directories under CATALINA_BASE
Tomcat will use the first 3 as part of its normal running. The webapps directory is where you will place your projects.
- 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).
- 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.
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.
- Use multiple CATALINA_BASE to setup tomcat 6 instances on windows
- Running multiple Tomcat instances on one server
- How to create multiple tomcat instances
- Tomcat Configuration Using CATALINA_BASE
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