Double the Tomcat: Run Two Instances on One Server
Creating a Custom Unit File for a Second Instance of Apache Tomcat
Need to run multiple instances of Tomcat on the same machine? You can set up a second instance by duplicating the configuration and customizing key parameters to avoid conflicts with the first instance.
Step-by-Step Procedure
1. Duplicate the Tomcat Installation
Start by copying the existing Tomcat directory (typically /opt/tomcat
):
# cp -r /opt/tomcat /opt/tomcat-second
This creates an independent folder for the second instance, which you can configure separately.
2. Customize Ports and Settings
Edit /opt/tomcat-second/conf/server.xml
and modify ports to avoid clashes with the default instance. For example:
- HTTP Connector: Change
port="8080"
toport="8081"
- Shutdown Port: Change
port="8005"
to something likeport="8006"
- AJP Connector: Change
port="8009"
toport="8010"
3. Create a Custom systemd Unit File
Duplicate the existing systemd unit file or create a new one:
# cp /etc/systemd/system/tomcat.service /etc/systemd/system/tomcat-second.service
Edit the tomcat-second.service
file and adjust key sections:
[Unit]
Description=Apache Tomcat Second Instance
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=CATALINA_PID=/opt/tomcat-second/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat-second
Environment=CATALINA_BASE=/opt/tomcat-second
ExecStart=/opt/tomcat-second/bin/startup.sh
ExecStop=/opt/tomcat-second/bin/shutdown.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
- CATALINA_BASE: Points to the second Tomcat directory
- CATALINA_PID: Unique PID file to prevent collision
- Startup/Shutdown Scripts: Use the ones inside
/opt/tomcat-second/bin
4. Enable and Start the Second Instance
Reload systemd so it picks up the new service:
# systemctl daemon-reload
# systemctl enable tomcat-second.service
# systemctl start tomcat-second.service
5. Verify the Status and Access
Check the status of the second instance:
# systemctl status tomcat-second.service
Then open a browser and visit: http://yourserver:8081
6. Allow Port in Firewall (If Applicable)
If you're using a firewall, don't forget to open the new port:
# firewall-cmd --permanent --add-port=8081/tcp
# firewall-cmd --reload
Optional: SELinux Adjustment (if enforced)
If SELinux is enforcing and you face binding issues, you can label the port appropriately:
# semanage port -a -t http_port_t -p tcp 8081
Done!
You now have a fully working second instance of Apache Tomcat running as a systemd service, independently managed from the original instance.
Comments
Post a Comment