Is there a proper way to run more than one tomcat instance on an Ubuntu server?
I've done some searching and found two options:
- Download a zipped tomcat and manually deploy it. The obvious con is that it won't be upgraded using
apt-get. - Use some advanced scripting, which is dangerous in a production server.
Any other ideas to cleanly run another instance?
Thanks in advance,
Adam
33 Answers
I am setting this up on Ubuntu 14.04.3 LTS.
I am using the Tomcat 7 provided by the tomcat7 package.
It installs Tomcat as a system service by providing a standard init script:
/etc/init.d/tomcat7and configuration file:
/etc/default/tomcat7Tomcat supports running multiple instances with the same server software.
The server software is located in $CATALINA_HOME, the files for the instance are located in $CATALINA_BASE. They are defined as follows in /etc/init.d/tomcat7:
NAME=tomcat7
CATALINA_HOME=/usr/share/$NAME
CATALINA_BASE=/var/lib/$NAME(Caveat: when editing files in the latter, be aware that it has some symlinks into the former.)
The tomcat7-user package provides the utility tomcat7-instance-create that can be used to create a directory tree for an additional Tomcat instance, including a bin/ directory with scripts for starting and stopping the instance manually.
What I haven't found is support for turning such an additional instance into a system service. Therefore, it must be done manually, e.g. as follows:
- Pick a value for the service name; it will be
$NAMEin the new init script. - Create a new user with that name that will own the files for the Tomcat instance and as which Tomcat will run. It can be a system user, its properties should be the same as for the
tomcat7user. - Run
tomcat7-instance-createas that user to create a Tomcat instance. - Configure it and install the web application(s) you want to run with it. Test them using its
bin/startup.shandbin/shutdown.shscripts. - Move the logs to
/var/log/$NAMEand symlink them back tologs/of the new Tomcat instance. - Write
/etc/init.d/$NAME, e.g. by copying and modifying/etc/init.d/tomcat7and modifying the assignment to$NAME. (It would be nicer if you didn't need to copy the whole script but could just create a link to it.) - Write
/etc/default/$NAMEto point at your Tomcat instance and user. - Use
upstart-rc.dto install the new service.
I gleaned some of the details from Kodjo-Kuma Djomeda.
1Have you considered configuring several webapps directories instead of running multiple tomcat instances?
Of course there are cases where you really need multiple instances but in case of serving same application separately for multiple hosts, you may do it by adding multiple declarations in server.xml:
<Host name="host1.example.com" appBase="host1" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false" /> <Host name="host2.example.com" appBase="host2" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false" />Now you may create "/var/lib/tomcat6/host1" and "/var/lib/tomcat6/host2" directories and deploy WAR files to them.
I did the following:
run the following command with the dir where tomcat should be created:
tomcat8-instance-create stagingthan changed the ports to something unique (if you have other tomcat8 running:
nano conf/server.xmli use the same user as the original tomcat8 so i make sure all files are owned by tomcat8
chown -R tomcat8:tomcat8 * i copied the script tomcat8 script in /etc/init.d/
cp /etc/init.d/tomcat8 /etc/init.d/tomcat8_stagingand edited the script to make it work with my new staging env:
nano /etc/init.d/tomcat8_stagingi had to edit the following lines to:
NAME=tomcat8_staging
DEFAULT=/etc/default/tomcat8
CATALINA_BASE=/app/tomcat8/staging
CATALINA_HOME=/usr/share/tomcat8and finally i enabled the server like this:
systemctl enable tomcat8_stagingwhen starting it with:
service tomcat8_staging starti got an error about missing policy files so i created a link for that in the conf dir:
ln -s /etc/tomcat8/policy.d policy.d