Running multiple JVM instances in Apache Tomcat using a single installation can help optimize resource utilization while keeping administration simpler. This guide will walk you through the steps to configure multiple JVMs within a single Tomcat installation.
✅ Why Use Multiple JVMs in a Single Tomcat Installation?
- Efficient Resource Utilization: Use the same Tomcat binaries while running separate JVM instances.
- Application Isolation: Each application runs in its dedicated JVM instance.
- Simplified Management: Maintain one Tomcat installation instead of multiple copies.
- Better Load Balancing: Spread requests across different JVMs for performance optimization.
🔧 Steps to Configure Multiple JVMs in One Tomcat Installation
1️⃣ Create Separate CATALINA_BASE Directories
Since we will use the same CATALINA_HOME
, we need separate CATALINA_BASE
directories for each instance. This ensures that each instance has its own configuration, logs, and web applications, preventing conflicts.
mkdir /opt/tomcat-instance1
mkdir /opt/tomcat-instance2
cp -r /opt/tomcat/conf /opt/tomcat-instance1/
cp -r /opt/tomcat/conf /opt/tomcat-instance2/
cp -r /opt/tomcat/logs /opt/tomcat-instance1/
cp -r /opt/tomcat/logs /opt/tomcat-instance2/
cp -r /opt/tomcat/temp /opt/tomcat-instance1/
cp -r /opt/tomcat/temp /opt/tomcat-instance2/
cp -r /opt/tomcat/webapps /opt/tomcat-instance1/
cp -r /opt/tomcat/webapps /opt/tomcat-instance2/
cp -r /opt/tomcat/work /opt/tomcat-instance1/
cp -r /opt/tomcat/work /opt/tomcat-instance2/
Now, each instance will have its own configurations and logs, allowing them to function independently.
2️⃣ Set Up Environment Variables
Each Tomcat instance needs a unique environment setup to control which JVM it runs on and to assign specific memory allocations.
Instance 1 (setenv.sh
):
export CATALINA_HOME=/opt/tomcat
export CATALINA_BASE=/opt/tomcat-instance1
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
export CATALINA_OPTS="-Xms512m -Xmx1024m -Djava.awt.headless=true"
export CATALINA_PID=/opt/tomcat-instance1/temp/tomcat.pid
Instance 2 (setenv.sh
):
export CATALINA_HOME=/opt/tomcat
export CATALINA_BASE=/opt/tomcat-instance2
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export CATALINA_OPTS="-Xms256m -Xmx512m -Djava.awt.headless=true"
export CATALINA_PID=/opt/tomcat-instance2/temp/tomcat.pid
This ensures each Tomcat instance runs with its own Java version and memory settings, providing better resource control and application isolation.
3️⃣ Modify Server Configuration (server.xml)
Each instance must listen on different ports to avoid conflicts. Modifying server.xml
allows each instance to have unique connectors, ensuring smooth operation.
Instance 1 (/opt/tomcat-instance1/conf/server.xml
):
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"/>
</Engine>
</Server>
Instance 2 (/opt/tomcat-instance2/conf/server.xml
):
<Server port="8105" shutdown="SHUTDOWN">
<Connector port="8180" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8543" />
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"/>
</Engine>
</Server>
This prevents port conflicts and allows each instance to handle requests independently.
4️⃣ Start & Stop Tomcat Instances
Using separate startup and shutdown scripts ensures that each Tomcat instance runs independently.
Start Instance 1:
CATALINA_BASE=/opt/tomcat-instance1 /opt/tomcat/bin/startup.sh
Start Instance 2:
CATALINA_BASE=/opt/tomcat-instance2 /opt/tomcat/bin/startup.sh
Stopping instances:
CATALINA_BASE=/opt/tomcat-instance1 /opt/tomcat/bin/shutdown.sh
CATALINA_BASE=/opt/tomcat-instance2 /opt/tomcat/bin/shutdown.sh
This allows smooth operation and maintenance of each instance without affecting the others.
🎯 Best Practices
✅ Assign unique ports for HTTP, AJP, and Shutdown ports to avoid conflicts.
✅ Monitor JVM performance using JVisualVM or JConsole for optimal resource usage.
✅ Configure separate log directories for each instance for easier debugging and maintenance.
✅ Use systemd service files to automate the startup and shutdown process for better reliability.
🔥 Conclusion
By following this guide, you can successfully configure multiple JVM instances using a single Tomcat installation to enhance scalability and isolate applications effectively.
🤝 Connect With Us
Are you looking for certified Tomcat professionals or need expert guidance on your project? We are here to help!
🔹 Get Certified Candidates: Hire skilled professionals with Tomcat expertise.
🔹 Project Consultation: Get best practices and hands-on support for seamless implementation.
💼 Discuss Your Project