** 说明 **
如果我们想要以一台机器上运行Tomcat的多个实例,但是不想安装多个Tomcat软件副本。那么我们可以配置多个工作 目录,每个运行实例独占一个工作目录,但是共享同一个安装目录。
创建多个实例就涉及到两个属性:CATALINA_HOME和CATALINA_BASE。官方解释
大概意思:
CATALINA_HOME:表示Tomcat安装的根目录,例如/home/tomcat/apache-tomcat-9.0.10 或C:\Program Files\apache-tomcat-9.0.10。
CATALINA_BASE:表示特定Tomcat实例的运行时配置的根。如果要在一台计算机上拥有多个Tomcat实例,请使用该CATALINA_BASE 属性。
以下实现方式使用的tomcat版本是:apache-tomcat-9.0.33
机器:centos 7
其中相关内容在Tomcat 的 catalina.properties 配置文件说明及使用配置文件处理异常这篇博客中最后有介绍。
1.安装Tomcat
首先到官网下载指定版本的Tomcat
,下载完成后解压下载的Tomcat压缩包。如下
为了方便使用,将Tomcat添加到系统环境变量
sudo vim /etc/profile
export CATALINA_HOME=/opt/tomcat/apache-tomcat-9.0.33
#使环境变量生效
source /etc/profile
2.创建多个实例
(1)在当前目录下创建tomcat-ins1和tomcat-ins2目录
$ mkdir tomcat-ins1 tomcat-ins2
(2)将刚解压完的Tomcat中的conf、logs、temp、webapps、work目录分别拷贝到创建的两个节点目录,作为私有目录
$ cp /opt/tomcat/apache-tomcat-9.0.33/{conf,logs,temp,webapps,work} /opt/tomcat/tomcat-ins1/
$ cp /opt/tomcat/apache-tomcat-9.0.33/{conf,logs,temp,webapps,work} /opt/tomcat/tomcat-ins2/
(3)进入实例tomcat-ins1,创建bin目录,将apache-tomcat-9.0.33中bin下的startup.sh、shutdown.sh文件拷贝到新建bin目录,修改startup.sh脚本。
#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
# Better OS/400 detection: see Bugzilla 31132
os400=false
case "`uname`" in
OS400*) os400=true;;
esac
# resolve links - $0 may be a softlink
PRG="$0"
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`/"$link"
fi
done
PRGDIR=`dirname "$PRG"`
#跳转到父级目录
export CATALINA_BASE="$(dirname "$PRGDIR")"
# 未添加环境变量的需要添加共享安装目录
#export CATALINA_HOME="/opt/tomcat/apache-tomcat-9.0.33"
EXECUTABLE="${CATALINA_HOME}"/bin/catalina.sh
# Check that target executable exists
if $os400; then
# -x will Only work on the os400 if the files are:
# 1. owned by the user
# 2. owned by the PRIMARY group of the user
# this will not work if the user belongs in secondary groups
eval
else
# 去掉此脚本的目录
#if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
if [ ! -x "$EXECUTABLE" ]; then
echo "Cannot find $PRGDIR/$EXECUTABLE"
echo "The file is absent or does not have execute permission"
echo "This file is needed to run this program"
exit 1
fi
fi
#执行安装目录脚本
#exec "$PRGDIR"/"$EXECUTABLE" start "$@"
exec "$EXECUTABLE" start "$@"
shutdown.sh停止脚本同理。
(4)在tomcat ins1下的conf中的server.xml中修改Server和http端口号分别为8011、8081。
== tomcat-ins2的修改方式与 tomcat-ins1相同 ==
(5)Windows环境操作类似,只需要修改startup.bat、shutdown.bat,修改内容与形式相同。
3.启动
分别启动apache-tomcat-9.0.33、 tomcat-ins1、tomcat-ins2三个实例。运行各自的startup.sh
(1)apache-tomcat-9.0.33的http端口为8080
(2)tomcat-ins1的http端口为8081
(3)tomcat-ins2的http端口为8082