tomcat多实例.md
多tomcat实例
环境说明
操作系统:CentOS 6.6
JDK:
# ll /usr/local/java
lrwxrwxrwx 1 root root 22 Feb 27 17:43 /usr/local/java -> /usr/local/jdk1.7.0_51
# cat /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin/:$PATH
tomcat_1 安装目录:/opt/tomcat8080/
tomcat_1 数据目录:/data/tomcat8080/
tomcat_2 安装目录:/opt/tomcat9090/
tomcat_2 数据目录:/data/tomcat9090/
创建tomcat实例
# mkdir -p /opt/tomcat{8080,9090}
# cp -r apache-tomcat-7.0.50/ /opt/tomcat8080/
# cp -r apache-tomcat-7.0.50/ /opt/tomcat9090/
配置文件
# vim /opt/tomcat8080/conf/server.xml
....
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="www.example.com" appBase="/data/tomcat8080/" unpackWARs="true" autoDeploy="true">
<Alias>example.com</Alias>
<Context path="/bbs" docBase="bbs" />
<Context path="" docBase="/data/tomcat8080/" />
</Host>
.....
# vim /opt/tomcat9090/conf/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="www.test.com" appBase="/data/tomcat9090" unpackWARs="true" autoDeploy="true">
<Alias>test.com</Alias>
<Context path="" docBase="/data/tomcat9090/" />
<Context path="bbs" docBase="bbs" />
</Host>
注意上面的配置中要将两个tomcat实例配置成不同的端口!
启动文件
tomcat_1
# vim /opt/tomcat8080/bin/catalina.sh
开头添加如下的内容:
CATALINA_HOME=/opt/tomcat8080
# cp /opt/tomcat8080/bin/catalina.sh /etc/init.d/tomcat8080
timcat_2:
# vim /opt/tomcat9090/bin/catalina.sh
开头添加如下的内容:
CATALINA_HOME=/opt/tomcat9090
# cp /opt/tomcat9090/bin/catalina.sh /etc/init.d/tomcat9090
测试文件
# tree /data/
/data/
├── tomcat8080
│ ├── bbs
│ │ └── index.jsp
│ └── index.jsp
└── tomcat9090
├── bbs
│ └── index.jsp
└── index.jsp
4 directories, 4 files
启动
# /etc/init.d/tomcat8080 start
# /etc/init.d/tomcat9090 start
# ss -tanlp |grep java
LISTEN 0 100 :::9009 :::* users:(("java",3906,43))
LISTEN 0 100 :::9090 :::* users:(("java",3906,42))
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* users:(("java",3637,49))
LISTEN 0 100 :::8009 :::* users:(("java",3637,43))
LISTEN 0 1 ::ffff:127.0.0.1:9005 :::* users:(("java",3906,49))
LISTEN 0 100 :::8080 :::* users:(("java",3637,42))
按上面的启动后可以在浏览器中进行测试,这种方式会带来一定的问题下面说明使用CATALINA_BASE的方式进行配置。
增加CATALINA_BASE
这种方式的一个实例增加一个 catalina_base ,但只用一个Tomcat软件实体。
环境说明
操作系统:CentOS 6.6
JDK:
# ll /usr/local/java
lrwxrwxrwx 1 root root 22 Feb 27 17:43 /usr/local/java -> /usr/local/jdk1.7.0_51
# cat /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin/:$PATH
tomcat 安装目录:
]# ll /usr/local/tomcat
lrwxrwxrwx. 1 root root 31 Nov 3 17:59 /usr/local/tomcat -> /usr/local/apache-tomcat-7.0.50
tomcat 实例1目录:
# tree /opt/tomcat8080/
/opt/tomcat8080/
├── conf
│ ├── Catalina
│ ├── catalina.policy
│ ├── catalina.properties
│ ├── context.xml
│ ├── logging.properties
│ ├── server.xml
│ ├── tomcat-users.xml
│ └── web.xml
├── logs
├── temp
├── tomcat.sh
├── webapps
│ ├── bbs
│ │ └── index.jsp
│ └── index.jsp
└── work
tomcat 实例2目录:
# tree /opt/tomcat9090/
/opt/tomcat9090/
├── conf
│ ├── Catalina
│ ├── catalina.policy
│ ├── catalina.properties
│ ├── context.xml
│ ├── logging.properties
│ ├── server.xml
│ ├── tomcat-users.xml
│ └── web.xml
├── logs
├── temp
├── tomcat.sh
├── webapps
│ ├── bbs
│ │ └── index.jsp
│ └── index.jsp
└── work
启动脚本
tomcat 实例1:
# cat /opt/tomcat8080/tomcat.sh
#!/bin/sh
# 本脚本参考:http://www.ttlsa.com
. /etc/init.d/functions
RETVAL=$?
# export CATALINA_BASE="$PWD"
export CATALINA_BASE="/opt/tomcat8080"
# tomcat安装目录,这个指向tomact所在的路径
export CATALINA_HOME="/usr/local/tomcat"
case "$1" in
start)
if [ -f $CATALINA_HOME/bin/startup.sh ];then
echo $"Start Tomcat"
$CATALINA_HOME/bin/startup.sh
fi
;;
stop)
if [ -f $CATALINA_HOME/bin/shutdown.sh ];then
$CATALINA_HOME/bin/shutdown.sh
fi
;;
*)
echo $"Usage: $0 {start|stop}"
exit 1
;;
esac
exit $RETVAL
tomcat 实例2:
# cat /opt/tomcat9090/tomcat.sh
#!/bin/sh
# 本脚本参考:http://www.ttlsa.com
. /etc/init.d/functions
RETVAL=$?
# export CATALINA_BASE="$PWD"
export CATALINA_BASE="/opt/tomcat9090"
# tomcat安装目录,这个指向tomact所在的路径
export CATALINA_HOME="/usr/local/tomcat"
case "$1" in
start)
if [ -f $CATALINA_HOME/bin/startup.sh ];then
echo $"Start Tomcat"
$CATALINA_HOME/bin/startup.sh
fi
;;
stop)
if [ -f $CATALINA_HOME/bin/shutdown.sh ];then
$CATALINA_HOME/bin/shutdown.sh
fi
;;
*)
echo $"Usage: $0 {start|stop}"
exit 1
;;
esac
exit $RETVAL
tomcat 实例配置
配置文件和上面的相同,这里就不做介绍了。注意在配置中两个的配置文件的端口不能出现相同的!
测试
前面的配置好然后启动各tomcat 实例进行测试,测试还是和上面的相同也不做介绍。