Linux 配置多个 Tomcat

我的系统里面原本就有一份 Tomcat ,名称为 tomcat8

1
2
3
wxs@ubuntu:/usr/local$ ls
bin games jdk1.8.0_161 man redis share tomcat8
etc include lib nginx sbin src zookeeper-3.5.2-alpha

注:本篇文章只使用两台 Tomcat ,配置更多个同理。

复制两份,分别为 tomcat-jittomcat-wxs

1
2
3
4
5
6
wxs@ubuntu:/usr/local$ sudo cp -r tomcat8/ tomcat8-jit
wxs@ubuntu:/usr/local$ sudo cp -r tomcat8/ tomcat8-wxs
wxs@ubuntu:/usr/local$ ls
bin include man sbin tomcat8 zookeeper-3.5.2-alpha
etc jdk1.8.0_161 nginx share tomcat8-jit
games lib redis src tomcat8-wxs

一、修改端口

配置多个Tomcat需要修改 3个 地方的端口信息,分别是:

  1. http访问端口(默认为8080端口):
1
2
3
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
  1. 监听tomcat关闭的端口(默认为8005):
1
2
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  1. 负责接收其他http服务器的请求端口(默认为8009):
1
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

下面修改这两个tomcat的端口信息:

(1)对于 tomcat8-jit:

1
wxs@ubuntu:/usr/local$ sudo vim tomcat8-jit/conf/server.xml

(2)对于 tomcat8-wxs:

1
wxs@ubuntu:/usr/local$ sudo vim tomcat8-wxs/conf/server.xml

二、修改启动文件

如果只修改了那三个端口信息,启动某一个,另外一个无法启动,因为默认只使用了同一个环境变量,需要为每一个tomcat配置环境变量。

修改 /etc/profile 文件,在尾部添加环境变量:

1
2
3
4
5
6
7
8
9
####第一个Tomcat####
export CATALINA_JIT_BASE=/usr/local/tomcat8-jit
export CATALINA_JIT_HOME=/usr/local/tomcat8-jit
export TOMCAT_JIT_HOME=/usr/local/tomcat8-jit

####第二个Tomcat####
export CATALINA_WXS_BASE=/usr/local/tomcat8-wxs
export CATALINA_WXS_HOME=/usr/local/tomcat8-wxs
export TOMCAT_WXS_HOME=/usr/local/tomcat8-wxs

修改每个 tomcat 下的 bin/catalina.sh 文件,找到下面代码:

1
# OS specific support.  $var _must_ be set to either true or false.

在下面添加对应的 CATALINA_BASECATALINA_HOME

(1)对于 tomcat8-jit:

1
2
3
# myself : add
export CATALINA_BASE=$CATALINA_JIT_BASE
export CATALINA_HOME=$CATALINA_JIT_HOME

(2)对于 tomcat8-wxs:

1
2
3
# myself : add
export CATALINA_BASE=$CATALINA_WXS_BASE
export CATALINA_HOME=$CATALINA_WXS_HOME

三、测试

修改两个 Tomcat 首页以便测试区分:

1
2
wxs@ubuntu:/usr/local$ sudo vim tomcat8-jit/webapps/ROOT/index.jsp 
wxs@ubuntu:/usr/local$ sudo vim tomcat8-wxs/webapps/ROOT/index.jsp

我的服务器IP是 192.168.30.149 ,分别访问 192.168.30.149:8090192.168.30.149:8091 :

# Tomcat

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×