Tomcat multiple instances simultaneously
Asked Answered
R

5

30

I am trying to run multiple instances of Tomcat, but even after configuring different ports for listening and shutting down the second instance, it keeps trying to listen on 8080 (configured for 8081). I read that I have to set a different value for CATALINA_BASE. From all the articles there are online, none of them actually show in which file this variable can be set.

Where and how can I set CATALINA_BASE for my Tomcat instance in C:\apache-tomcat-7.0.39

Rolland answered 19/4, 2013 at 17:43 Comment(4)
[CATALINA_BASE explained here in detail][1] [1]: #3090898Gotcher
It doesn't actually tell WHERE and HOW to set the variables.Rolland
#1794944Gotcher
I think easier is to run the installer again for tomcat 7 as mentioned here. pe-kay.blogspot.in/2011/08/…Fixer
S
35

The easiest way I have run two copies of Tomcat involved the following steps (I was trying to run two distinct versions of tomcat, 6 and 7):

  • Establish 2 copies of tomcat in different folders (if they are different versions then this is easy, if they are the same version then you will need be distinguished in some other way. There are a lot of files that Tomcat creates to manage it so running two instances with the same work directory likely isn't possible)

  • Change the following ports that tomcat is listening to in server.xml

    • <Connector port="8080"> <- This is the port that tomcat uses to respond to HTTP requests
    • <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <- this defines two ports, one for the AJP connector (used if you are using tomcat behind an Apache or IIS server) and the port used for HTTPS traffic
    • <Server port="8005" shutdown="SHUTDOWN"> <- this is the port that Tomcat uses to respond to SHUTDOWN events

Finally, if you are running this as a Windows service you will need to establish different service names for each instance (you can do this during setup, the default for Tomcat 7 is tomcat7). Once Tomcat is running all of it's configuration fields use relative paths so you don't need to touch CATALINA_BASE

Seniority answered 19/4, 2013 at 17:52 Comment(3)
Yes, that is what I firstly tried I have 2 folders with 2 different versions of TomCat, 7.0.37 and 7.0.39. But even after I edited server.xml on .39 it keeps trying to listen on 8080 and the other default ones.Rolland
If you change the server port and the two connector ports I can confirm that this will work. One the computer I'm typing on I have tomcat7 listen on port 80 and tomcat6 on port 8080.Seniority
Don't forget that you need to restart the Tomcat-Server to load changes made in server.xmlBlondy
S
56

Let's say that you have only one Tomcat folder located in C:\apache-tomcat-7.0.39, and that you wish to run two instances from it.

Make sure that you have CATALINA_HOME system/user variable set, and pointing to C:\apache-tomcat-7.0.39

  1. Create a folder C:\instance1. Copy conf, webapps and temp folders from C:\apache-tomcat-7.0.39 and paste them to C:\instance1. You can delete contents from webapps and temp folders located under instance1, but don't touch conf contents.

  2. Now copy>paste C:\instance1 and rename it to instance2. That way, both instance1 and instance2 will have the same content.

  3. Go to C:\instance2\conf, edit server.xml and change the numbers of these ports (I marked those as XXXX):

    <Server port="XXXX" shutdown="SHUTDOWN">

    <Connector port="XXXX" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

    <Connector port="XXXX" protocol="AJP/1.3" redirectPort="8443" />

  4. Deploy whatever you want into instance1\webapps and instance2\webapps

  5. Create the following 4 batch files under C:\

instance1_startup.bat

@echo off

set CATALINA_BASE=C:\instance1

cd "%CATALINA_HOME%\bin"

set TITLE=My Tomcat Instance 01

call startup.bat %TITLE%

instance1_shutdown.bat

@echo off

set CATALINA_BASE=C:\instance1

cd "%CATALINA_HOME%\bin"

call shutdown.bat

instance2_startup.bat

@echo off

set CATALINA_BASE=C:\instance2

cd "%CATALINA_HOME%\bin"

set TITLE=My Tomcat Instance 02

call startup.bat %TITLE%

instance2_shutdown.bat

@echo off

set CATALINA_BASE=C:\instance2

cd "%CATALINA_HOME%\bin"

call shutdown.bat

  1. Run instance1_startup.bat and instance2_startup.bat, hopefully it should work.
Sacramental answered 21/2, 2014 at 21:39 Comment(5)
This should be the easiest way to do this. I don't know why I need to bother to download multiple versions of tomcat. Thx, Goran!Turgeon
This one gives concrete resolution.Ramtil
Really detailed and useful solution. Thank youFiasco
In my specific case I needed two distinct major versions (6 and 7 at the time), this approach would let you run distinct app contexts from the same installation (possibly two apps that expect to run as ROOT.war), for that approach I have tended to just add multiple <container> elements and use hostnames to distinguish them or even multiple <connector> to distinguish by port.Seniority
i followed the same steps but when i shutdown my instance 2, it shutsdown instance 1. even i compared the ports are different as well.Blasius
S
35

The easiest way I have run two copies of Tomcat involved the following steps (I was trying to run two distinct versions of tomcat, 6 and 7):

  • Establish 2 copies of tomcat in different folders (if they are different versions then this is easy, if they are the same version then you will need be distinguished in some other way. There are a lot of files that Tomcat creates to manage it so running two instances with the same work directory likely isn't possible)

  • Change the following ports that tomcat is listening to in server.xml

    • <Connector port="8080"> <- This is the port that tomcat uses to respond to HTTP requests
    • <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <- this defines two ports, one for the AJP connector (used if you are using tomcat behind an Apache or IIS server) and the port used for HTTPS traffic
    • <Server port="8005" shutdown="SHUTDOWN"> <- this is the port that Tomcat uses to respond to SHUTDOWN events

Finally, if you are running this as a Windows service you will need to establish different service names for each instance (you can do this during setup, the default for Tomcat 7 is tomcat7). Once Tomcat is running all of it's configuration fields use relative paths so you don't need to touch CATALINA_BASE

Seniority answered 19/4, 2013 at 17:52 Comment(3)
Yes, that is what I firstly tried I have 2 folders with 2 different versions of TomCat, 7.0.37 and 7.0.39. But even after I edited server.xml on .39 it keeps trying to listen on 8080 and the other default ones.Rolland
If you change the server port and the two connector ports I can confirm that this will work. One the computer I'm typing on I have tomcat7 listen on port 80 and tomcat6 on port 8080.Seniority
Don't forget that you need to restart the Tomcat-Server to load changes made in server.xmlBlondy
G
1

If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.

Gotcher answered 19/4, 2013 at 17:51 Comment(3)
"I am trying to run multiple instances of Tom Cat"Rolland
#1794944Gotcher
Its for tomcat 6 but you can still use it.Gotcher
S
0

The easiest way is download another tomcat 8 or 9 and install it while installing change all the port numbers. Copying the existing instance and changing the port number hardly in server.xml can cause some errors with services.

Sisyphus answered 10/4, 2018 at 12:25 Comment(0)
Y
-1
1. 1st create one bat file(tomcat-start1.bat) and add the below code and keep   this bat file in any location.create instance(a folder having with webapps, config, bin folders) or give directly location in "newins" tomcat folder. and change the port numbers as 8181 in server.xml.
@echo off

set javabin=C:\Program Files\Java\jdk1.8.0_101\bin
set apachehom=E:\Apache Tomcat 8.0.15
set newins=E:\my_instance_1 (or)E:\Apache Tomcat 8.0.15

start "Tomcat 01" "%javabin%\java.exe" ^
-Djava.util.logging.config.file="%apachebas%\conf\logging.properties" ^
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager ^
-Djava.util.logging.config.file="%apachebas%\conf\logging.properties" ^
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager ^
-Djava.endorsed.dirs="%apachehom%\endorsed" ^
-classpath "%apachehom%\bin\bootstrap.jar;%apachehom%\bin\tomcat-juli.jar" ^
-Dcatalina.base="%newins%" ^
-Dcatalina.home="%apachehom%" ^
-Djava.io.tmpdir="%apachebas%\temp" ^
org.apache.catalina.startup.Bootstrap  start

exit

2. create java class and add this code and run
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

@Controller
@RequestMapping("/hello")
public class TomcatInstance {

    @RequestMapping(method = RequestMethod.GET)
    public void printHello(ModelMap model) {

        Runtime runtime = Runtime.getRuntime();
        try {
            Process p1 = runtime.exec("cmd /c start E:\\Jammulaiah\\Sample\\tomcat-start1.bat");
            InputStream is = p1.getInputStream();
             int i = 0;
             StringBuffer sb=new StringBuffer();
             while ((i = is.read()) != -1) {
             sb.append((char) i);
             System.out.println(sb.toString());
             }
        } catch (IOException ioException) {
            System.out.println(ioException.getMessage());
        }

    }

}
Yuma answered 17/11, 2016 at 13:55 Comment(1)
Welcome to Stack Overflow! While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.Lydalyddite

© 2022 - 2024 — McMap. All rights reserved.