Single machine multi instance configuration of Tomcat

Keywords: Tomcat Apache Ubuntu shell

Sometimes you need to deploy multiple Tomcat on a server, and distinguish them through different ports, such as reverse proxy. But I don't want to simply copy tomcat, which is not convenient for future upgrade or management. At this time, I need to configure a single machine with multiple instances of Tomcat.

Tomcat Download

Download Tomcat directly to the official Tomcat website to download the version you need. I download Tomcat 8.5.32 here.

tar -zxvf apache-tomcat-8.5.32.tar.gz

You can see that the directory after decompression is as follows:

niu@ubuntu:~/develop/test$ cd apache-tomcat-8.5.32/
niu@ubuntu:~/develop/test/apache-tomcat-8.5.32$ ll
total 120
drwxr-x--- 2 niu niu  4096 8 month  10 01:35 bin/
drwx------ 2 niu niu  4096 6 month  20 12:53 conf/
drwxr-x--- 2 niu niu  4096 8 month  10 01:35 lib/
-rw-r----- 1 niu niu 57092 6 month  20 12:53 LICENSE
drwxr-x--- 2 niu niu  4096 6 month  20 12:50 logs/
-rw-r----- 1 niu niu  1723 6 month  20 12:53 NOTICE
-rw-r----- 1 niu niu  7138 6 month  20 12:53 RELEASE-NOTES
-rw-r----- 1 niu niu 16246 6 month  20 12:53 RUNNING.txt
drwxr-x--- 2 niu niu  4096 8 month  10 01:35 temp/
drwxr-x--- 7 niu niu  4096 6 month  20 12:51 webapps/
drwxr-x--- 2 niu niu  4096 6 month  20 12:50 work/

Configure multi instance template

To realize multi instance startup of single tomcat, first we need to modify the current Tomcat directory structure as follows. For convenience, we will first configure a template instance, and then write a start stop shell script in the template instance. In the future, only one copy of the modified port number is needed to extend the instance.

//Delete useless files
rm RUNNING.txt
//Create the WEB instance template folder, and only one copy is needed to deploy the new instance later
mkdir web-template
//Move instance file to instance template folder
mv conf/ ./web-template/
mv logs/ ./web-template/
mv tem/ ./web-template/
mv temp/ ./web-template/
mv webapps/ ./web-template/
mv work/ ./web-template/

Write a shell script to start and stop Tomcat under the template folder.

//New sehll script

Enter the following:

#tomcat instance directory
#tomcat installation directory, change to your own
export CATALINA_HOME="/home/niu/develop/test/apache-tomcat-8.5.32"
export JVM_OPTIONS="-Xms128m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m"
case "$1" in
if [ -f $CATALINA_HOME/bin/ ];then
echo $"Start Tomcat"
if [ -f $CATALINA_HOME/bin/ ];then
echo $"Stop Tomcat"
echo $"Usage:$0 {start|stop}"
exit 1
exit $RETVAL

Save to exit and give execution permission.

chmod +x

After the above operations, the current Tomcat directory structure is as follows:

├── bin
├── lib
└── web-template
    ├── conf
    ├── logs
    ├── temp
    ├── webapps
    └── work

Test case template

The instance template contains the config folder, which is the configuration file of this instance. You can modify the port number and other information. We haven't modified it. The default is 8080. The ROOT directory in the webapps folder is Tomcat's default publishing directory. We haven't modified it. It contains Tomcat's default home page information.

//Start the template instance for testing, and you can see the log of normal startup start
//Stop to stop stop

After successful startup, access IP+8080 for test.


Tomcat home page


See the properties page and it's done. It's only one step away from multiple instances.

Add an instance

Add an instance and copy only one template instance. Then modify the port number. Otherwise, it will fail to start because the port is occupied.

#Copy an example
cp -r web-template/ web-9090
#Change the port number to 9090
vim conf/server.xml
#Change the HTTP port number from 8080 to 9090, about line 69
<Connector port="9090" protocol="HTTP/1.1"
  redirectPort="8443" />
#Change the SHUTDOWN port number from 8005 to 9005, about line 22
ver port="9005" shutdown="SHUTDOWN">
#Save, exit, start start

At this time, you can access IP + port 9090 for access test.


9090 instance access test


At this point, multiple instances have been deployed and Tomcat has been closed. Exit the terminal.
To add an instance, you only need to copy the template instance and then modify the port number. Each instance has its own configuration, which can be managed and started independently.

624 original articles published, 324 praised, 300000 visitors+
His message board follow

Posted by yurko on Sun, 12 Jan 2020 03:19:57 -0800