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.
//Decompression 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 LICENSE rm NOTICE rm RELEASE-NOTES 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 vim tomcat.sh
Enter the following:
RETVAL=$? #tomcat instance directory export CATALINA_BASE="$PWD" #tomcat installation directory, change to your own export CATALINA_HOME="/home/niu/develop/test/apache-tomcat-8.5.32" #Optional export JVM_OPTIONS="-Xms128m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m" case "$1" in start) if [ -f $CATALINA_HOME/bin/startup.sh ];then echo $"Start Tomcat" $CATALINA_HOME/bin/startup.sh fi ;; stop) if [ -f $CATALINA_HOME/bin/shutdown.sh ];then echo $"Stop Tomcat" $CATALINA_HOME/bin/shutdown.sh fi ;; *) echo $"Usage:$0 {start|stop}" exit 1 ;; esac exit $RETVAL
Save to exit and give execution permission.
chmod +x tomcat.sh
After the above operations, the current Tomcat directory structure is as follows:
apache-tomcat-8.5.32 ├── 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 tomcat.sh start //Stop to stop tomcat.sh 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" connectionTimeout="20000" redirectPort="8443" /> #Change the SHUTDOWN port number from 8005 to 9005, about line 22 ver port="9005" shutdown="SHUTDOWN"> #Save, exit, start tomcat.sh 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.