安装环境
操作系统:CentOS7 64位 Mini版
JDK:1.8,安装目录:/opt/jdk1.8
Tomcat:Tomcat8.0.35,安装目录:/opt/apache-tomcat-8.0.35
配置3个Tomcat实例组件一个集群:
/opt/tomcat_instance/tomcat1
/opt/tomcat_instance/tomcat2
/opt/tomcat_instance/tomcat3
安装步聚
1> 下载Tomcat安装包并解压,目录结构如下:
drwxr-xr-x. 3 root root 4096 5月 26 01:41 bin
drwxr-xr-x. 3 root root 4096 6月 28 16:14 conf
drwxr-xr-x. 2 root root 4096 5月 25 22:12 lib
drwxr-xr-x. 2 root root 4096 6月 28 15:48 logs
drwxr-xr-x. 2 root root 4096 5月 25 22:12 temp
drwxr-xr-x. 7 root root 4096 5月 25 22:14 webapps
drwxr-xr-x. 3 root root 4096 5月 12 05:57 work
2> 在/opt/tomcat_instance下分别创建3个实例的目录:
shell> mkdir -p /opt/tomcat_instance/tomcat1
shell> mkdir -p /opt/tomcat_instance/tomcat2
shell> mkdir -p /opt/tomcat_instance/tomcat3
3> 拷贝tomcat安装目录下的conf目录分别放入3个实例目录中
shell> copy -R /opt/apache-tomcat-8.0.35/conf /opt/tomcat_instance/tomcat1/
shell> copy -R /opt/apache-tomcat-8.0.35/conf /opt/tomcat_instance/tomcat2/
shell> copy -R /opt/apache-tomcat-8.0.35/conf /opt/tomcat_instance/tomcat3/
4> 在tomcat实例下创建实例启动和停止脚本
启动和停止脚本放到tomcat实例的bin目录下
a) 启动脚本
shell> mkdir -p /opt/tomcat_instance/tomcat[1-4]/bin
shell> touch startup.sh
shell> chown +x startup.sh
将以下脚本拷贝到startup.sh文件中,注意修改CATALINA_BASE
指向的tomcat实例路径:
#!/bin/bash
export JRE_HOME=/opt/jdk1.8/jre
export CATALINA_HOME=/opt/tomcat8
export CATALINA_BASE="/opt/tomcat_instance/tomcat1"
export CATALINA_TMPDIR="$CATALINA_BASE/temp"
export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
export JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat1"
#创建logs目录
if [ ! -d "$CATALINA_BASE/logs" ]; then
mkdir $CATALINA_BASE/logs
fi
#创建temp目录
if [ ! -d "$CATALINA_BASE/temp" ]; then
mkdir $CATALINA_BASE/temp
fi
# 调用tomcat启动脚本
bash $CATALINA_HOME/bin/startup.sh "$@"
b) 停止脚本
shell> touch shutdown.sh
shell> chown +x shutdown.sh
shell> vim shutdown.sh
拷贝以下脚本到shutdown.sh文件中:
#!/bin/bash
export JRE_HOME=/opt/jdk1.8/jre
export CATALINA_HOME=/opt/tomcat8
export CATALINA_BASE="/opt/tomcat_instance/tomcat1"
export CATALINA_TMPDIR="$CATALINA_BASE/temp"
export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
bash $CATALINA_HOME/bin/shutdown.sh "$@"
5、修改每个tomcat实例中server.xml中的端口
<Server port="8005" shutdown="SHUTDOWN">
...
<Service name="Catalina">
...
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
...
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
...
<Engine name="Catalina" defaultHost="localhost">
...
<Host name="localhost" appBase="/opt/deploy/webapps"
unpackWARs="true" autoDeploy="true">
...
</Host>
</Engine>
</Service>
</Server>
上面是裁剪过后的server.xml,分别修改以上三个端口(Server、HTTP、AJP),不要和其它实例的端口或系统已经占用的端口发生冲突。
6、创建实例部署应用的目录
shell> mkdir -p /opt/deploy/webapps
所有实例的app默认都部署到/opt/deploy/webapps目录下,注意修改server.xml中Host节点的appBase属性的值。
7、启动服务
shell> /opt/tomcat_instance/tomcat1/bin/startup.sh
shell> /opt/tomcat_instance/tomcat2/bin/startup.sh
shell> /opt/tomcat_instance/tomcat2/bin/startup.sh
服务器启动之后,会分别在相应的实例目录下生成logs、temp、work和webapps目录。另外也可以在实例目录下创建lib目录,用于存放app的jar。现在来看实例的安装目录,就和tomcat的安装包解压后的目录结构一样了,但所有实例共享同一套tomcat安装程序的bin和lib。后面如果需要升级tomcat或修改tomcat脚本的相关配置,只需要更新这一套程序就行,也方便了日后的维护。