多实例配置
- 一、安装 tomcat
- 二、多实例配置
tomcat 官网
tomcat 安装包下载地址
一、安装 tomcat
- 解压tomcat压缩包到
/usr/local
下
tar xf apache-tomcat-*.gz -C /usr/local/
# 可选 添加一个软链接,方便查找
ln -s /usr/local/apache-tomcat-* /usr/local/tomcat
- 在
/etc/profile
中添加环境变量
CATALINA_HOME=/usr/local/tomcat
PATH=$CATALINA_HOME/bin:$PATH
export CATALINA_HOME PATH
- 让新添加的环境变量生效
source /etc/profile
- 启动测试
/usr/local/tomcat/bin/startup.sh
或
startup.sh # 在环境变量中加入 PATH=$CATALINA_HOME/bin:$PATH 才可使用,确保命令没有冲突
访问一下服务器地址确认是否成功,服务器端口号为 8080(访问用),程序工作端口号为8005
如下图即为成功
二、多实例配置
- 关闭主站
/usr/local/tomcat/bin/shutdown.sh
- 创建示例主目录
mkdir /usr/local/tomcat/instance{1..2}
- 导入所需要的文件
主要需要/usr/local/tomcat/{conf,logs,temp,work}
这些文件夹
目录结构如下,可以通过该命令查看cp -r /usr/local/tomcat/{conf,logs,temp,work} /usr/local/tomcat/instance1 cp -r /usr/local/tomcat/{conf,logs,temp,work} /usr/local/tomcat/instance2
tree -d -L 1 /usr/local/tomcat/instance*
/usr/local/tomcat/instance1 ├── conf ├── logs ├── temp └── work /usr/local/tomcat/instance2 ├── conf ├── instance1 ├── logs ├── temp └── work
- 更改
conf
文件夹下的server.xml
更改instance1的工作端口为8091、服务端口为8081
更改instance2的工作端口为8092、服务端口为8082
vim /usr/local/tomcat/instance1/conf/server.xml
vim /usr/local/tomcat/instance2/conf/server.xml
修改后如下(前面的数字是行号)
instance1:
instance2:22 <Server port="8091" shutdown="SHUTDOWN"> ······ 70 <Connector port="8081" protocol="HTTP/1.1" 71 connectionTimeout="20000" 72 redirectPort="8443" />
22 <Server port="8092" shutdown="SHUTDOWN"> ······ 70 <Connector port="8082" protocol="HTTP/1.1" 71 connectionTimeout="20000" 72 redirectPort="8443" />
- 新建工作目录
mkdir -p /usr/local/tomcat/{instance1,instance2}/webapps/ROOT
- 添加测试代码
vim /usr/local/tomcat/instance1/webapps/ROOT/index.jsp
<html> <body> <%@ page language="java" %> <%! int count = 0; %> <% count++; %> <p>instance1</p> count = <%= count %> </body> </html>
vim /usr/local/tomcat/instance2/webapps/ROOT/index.jsp
<html> <body> <%@ page language="java" %> <%! int count = 0; %> <% count++; %> <p>instance2</p> count = <%= count %> </body> </html>
- 编写启动脚本
instance1 的启动脚本
vim /usr/local/tomcat/instance1/ins1.sh
instance2 的启动脚本#!/bin/bash #instance1 . /etc/init.d/functions export CATALINA_BASE="/usr/local/tomcat/instance1" case "$1" in start) $CATALINA_HOME/bin/startup.sh ;; stop) $CATALINA_HOME/bin/shutdown.sh ;; restart) $CATALINA_HOME/bin/shutdown.sh sleep 5 $CATALINA_HOME/bin/startup.sh ;; esac
vim /usr/local/tomcat/instance2/ins2.sh
赋予执行权限#!/bin/bash #instance2 . /etc/init.d/functions export CATALINA_BASE="/usr/local/tomcat/instance2" case "$1" in start) $CATALINA_HOME/bin/startup.sh ;; stop) $CATALINA_HOME/bin/shutdown.sh ;; restart) $CATALINA_HOME/bin/shutdown.sh sleep 5 $CATALINA_HOME/bin/startup.sh ;; esac
chmod +x /usr/local/tomcat/{instance1,instance2}/ins*.sh
- 启动tomcat实例,并访问
访问/usr/local/tomcat/instance1/ins1.sh start /usr/local/tomcat/instance2/ins2.sh start
http://IP:8081
和http://IP:8082
把IP换为自己的地址
每刷新一次count+1