Linux configuration of single machine and multiple instances of Tomcat

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.
The following operations are running under Linux.

Tomcat Download

The download of Tomcat can go 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 1001:35 bin/
drwx------ 2 niu niu  4096 6 Month 2012:53 conf/
drwxr-x--- 2 niu niu  4096 8 Month 1001:35 lib/
-rw-r----- 1 niu niu 57092 6 Month 2012:53 LICENSE
drwxr-x--- 2 niu niu  4096 6 Month 2012:50 logs/
-rw-r----- 1 niu niu  1723 6 Month 2012:53 NOTICE
-rw-r----- 1 niu niu  7138 6 Month 2012:53 RELEASE-NOTES
-rw-r----- 1 niu niu 16246 6 Month 2012:53 RUNNING.txt
drwxr-x--- 2 niu niu  4096 8 Month 1001:35 temp/
drwxr-x--- 7 niu niu  4096 6 Month 2012:51 webapps/
drwxr-x--- 2 niu niu  4096 6 Month 2012: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"
# Optional
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.

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.

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.

< > >

Personal website:
Public account reply [resources] can get the most popular Java core knowledge sorting & interview materials without any routine.

Keywords: Tomcat Apache Ubuntu shell

Added by Deemo on Fri, 25 Oct 2019 05:54:09 +0300