环境
tomcat:/home/tomcat-6.0.45
oms-client
站点程序:/home/oms-client/oms-book-client
站点tomcat实例:/home/oms-client/tomcat6-sever1
端口:38080
bms-client
站点程序:/home/bms-client/bms-book-client
站点tomcat实例:/home/bms-client/tomcat6-sever2
端口:48080
1. tomcat安装
安装很简单,配置好jdk与解压tomcat即可
我们首先看到的是tomcat的目录结构,其每个文件夹有以下用途:
- bin – 包含所有运行tomcat的二进制和脚本文件
- lib – 包含tomcat使用的所有共享库
- conf - 包含配置信息,如tomcat绑定的端口等
- logs – 包含所有的日志文件
- temp – 此目录是tomcat存放的临时文件
- webapps – 此目录非常重要,这里存放所有的应用程序war包
- work – 如果应用程序包含jsp文件,那么每个jsp文件会被编译转化为servlet,存放于此
当我们运行Tomcat时,会用到5个环境变量。他们是:
- CATALINA_HOME
- CATALINA_BASE
- CATALINA_TMPDIR
- JRE_HOME/JAVA_HOME
- CLASSPATH
在以上列表中,CATALINA_HOME和JAVA_HOME是必要的环境变量。其它的都可以通过CATALINA_HOME来转换,是可选的。
CATALINA_HOME – 此环境变量是tomcat安装/提取的根目录。所以通过CATALINA_HOME,可以得到bin和lib目录。
CATALINA_BASE – 如果不指定则是CATALINA_HOME的值。该变量指向的目录里面包括每个运行实例需要使用自己的conf、logs、temp、webapps、work目录。
一般运行Tomcat的方法是,只设置CATALINA_HOME变量,执行startup.sh脚本,startup.sh会自动转换其它未设置的变量。
2. 配置多实例目录
在tomcat安装目录下创建oms-client、bms-client,在oms-client创建tomcat实例1 tomcat6-server1 并且将conf、logs、temp、webapp、work目录拷贝到这两个目录,然后tomcat安装目录可以全部留下。配置后的目录结构如下:
备注:截图中有bin目录,实际上bin目录只会放重新写的启动和删除.sh,原文件都已删除
3. 配置站点server.xml
3.1 配置tomcat6-server1
需要修改的端口是:Shutdown port,Connector port,ajp port和Redirect port。
Shutdown port – 此端口用于关闭Tomcat。当执行shutdown.sh脚本时,它会给此端口发出一个信号,Tomcat的进程会监听此端口,如果接收到这样的信号,进程会清理退出。
Connector port - 此端口是应用对外公开发布的端口。
ajp port – Web服务器(例如Apache的httpd Server)通过此端口和Tomcat进行通信,也可以使用它设置一个负载均衡服务器。
Redirect port – 如果此Connector支持非SSL请求和接收SSL请求,Catalina会自动将请求指向到此端口。
修改Shutdown port
修改Connectors port 和 redirectPort
修改Connectors port 和 redirectPort
修改Host 加入
3.2 配置tomcat6-server2
同上,几个端口号都要修改一样
4. 多实例启动脚本
在/home/bms-client/tomcat6-sever1/bin、/home/bms-client/tomcat6-sever2/bin目录下分别创建启动脚本tomcat.sh, 两个tomcat.sh的区别主要就是CATALINA_BASE不同
4.1 脚本内容tomcat.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#!/bin/sh
# description: 启动tomcat多实例.
.
/
etc
/
init
.
d
/
functions
RETVAL
=
$
?
# tomcat实例目录
export
CATALINA_BASE
=
"/home/oms-client/tomcat6-server1
"
# tomcat安装目录
export
CATALINA_HOME
=
"/home/tomcat-6.0.45"
# 可选
export
JVM_OPTIONS
=
"-Xms128m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m"
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
echo
$
"Stop Tomcat"
$
CATALINA_HOME
/
bin
/
shutdown
.
sh
fi
;
;
*
)
echo
$
"Usage: $0 {start|stop}"
exit
1
;
;
esac
exit
$
RETVAL
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#!/bin/sh
# description: 启动tomcat多实例.
.
/
etc
/
init
.
d
/
functions
RETVAL
=
$
?
# tomcat实例目录
export
CATALINA_BASE
=
"/home/bms-client/tomcat6-server2
"
# tomcat安装目录
export
CATALINA_HOME
=
"/home/tomcat-6.0.45"
# 可选
export
JVM_OPTIONS
=
"-Xms128m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m"
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
echo
$
"Stop Tomcat"
$
CATALINA_HOME
/
bin
/
shutdown
.
sh
fi
;
;
*
)
echo
$
"Usage: $0 {start|stop}"
exit
1
;
;
esac
exit
$
RETVAL
|
4.2 启动脚本赋权限
1
|
# chmod a+x tomcat.sh
|
5. 启动测试
5.1 启动/关闭server1
1
2
3
4
5
6
|
启动
# cd /home/oms-client/tomcat6-server1/bin
# ./tomcat.sh start
关闭
# cd /home/oms-client/tomcat6-server1/bin
# ./tomcat.sh stop
|
5.2 启动/关闭server2
1
2
3
4
5
6
|
启动
# cd /home/bms-client/tomcat6-server2/bin
# ./tomcat.sh start
关闭
# cd /home/bms-client/tomcat6-server2/bin
# ./tomcat.sh stop
|
备注:一定需要cd到tomcat.sh的当前目录下执行才可以
具体XML修改结果为:
<?xml version='1.0' encoding='utf-8'?>
<!--
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.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="48005" shutdown="SHUTDOWN">
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="48080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8445" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="48009" protocol="AJP/1.3" redirectPort="8445" />
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- The request dumper valve dumps useful debugging information about
the request and response data received and sent by Tomcat.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.RequestDumperValve"/>
-->
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<!-- Define the default virtual host
Note: XML Schema validation will not work with Xerces 2.2.
-->
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/home/bms-client/hello" reloadable="false"/>
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
-->
</Host>
</Engine>
</Service>
</Server>