大纲
一、Tomcat 基本配置
1.为Tomcat提供SysV脚本
2.catalina 脚本讲解
3.telnet 登录管理Tomcat
4.配置Tomcat虚拟主机
5.Tomcat图形管理接口
manager 管理应用程序的部署及监控
host-manager 虚拟主机的管理
6.部署JSP网站案例
布署JavaCenter Home(开源的SNS网站)
二、Nginx反向代理Tomcat服务器
1.Nginx将请求反向代理到后端Tomcat
2.Nginx将图片缓存到本地
3.Nginx将请求实现动静分离
注,实验环境说明,操作系统:CentOS 6.4 x86_64,软件版本:jdk-7u40、apache-tomcat-7.0.42、Nginx-1.4.2,博客中所用到的软件请到这里下载:http://yunpan.cn/QGBCLwrZnpLMS。
一、Tomcat 基本配置
1.为Tomcat提供SysV脚本
注,在上一篇博文中我们已经演示安装了Tomcat,这里我们就不在演示,不清楚的博友可以参考这篇博文,http://freeloda.blog.51cto.com/2033581/1299644,在上一篇博文中我们没有增加,SysV脚本,在这篇博文中我们来增加一下,下面我们就来具体演示一下。
1
2
3
4
5
6
7
8
9
10
|
[root@tomcat
~] #
vim /etc/init.d/tomcat #!/bin/sh #
Tomcat init script for Linux. # #
chkconfig: 2345 96 14 #
description: The Apache Tomcat servlet/JSP container. CATALINA_HOME= /usr/local/tomcat #注意你的脚本路径 export CATALINA_HOME #
export CATALINA_OPTS="-Xms128m -Xmx256m" exec $CATALINA_HOME /bin/catalina .sh
$* |
下面我们来增加执行权限,并加入服务列表设置开机自启动,
1
2
3
|
[root@tomcat
~] #
chmod +x /etc/init.d/tomcat [root@tomcat
~] #
chkconfig --add tomcat [root@tomcat
~] #
chkconfig tomcat --list |
tomcat 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
下面我们来启动一下Tomcat并测试一下,
1
2
3
4
5
6
|
[root@tomcat
~] #
service tomcat start Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar |
查看一下启动的端口号,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@tomcat
~] #
netstat -ntulp Active
Internet connections (only servers) Proto
Recv-Q Send-Q Local Address Foreign Address State PID /Program name tcp
0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1044 /sshd tcp
0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1121 /master tcp
0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 12988 /sshd tcp
0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 13053 /sshd tcp
0 0 :::8080 :::* LISTEN 13088 /java tcp
0 0 :::22 :::* LISTEN 1044 /sshd tcp
0 0 ::1:25 :::* LISTEN 1121 /master tcp
0 0 ::1:6010 :::* LISTEN 12988 /sshd tcp
0 0 ::1:6011 :::* LISTEN 13053 /sshd tcp
0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 13088 /java tcp
0 0 :::8009 :::* LISTEN 13088 /java |
用浏览器访问一下,
好了,到这里Tomcat的SysV脚本增加完成,下面我们来说一下catalina脚本。
2.catalina 脚本讲解
首先我们来查看一下这个脚本,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@tomcat
bin] #
catalina.sh -h Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr/java/jdk1 .7.0_40 Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar Usage:
catalina.sh ( commands ... ) commands: debug
Start Catalina in a
debugger debug
-security Debug Catalina with a security manager jpda
start Start Catalina under JPDA debugger run
Start Catalina in the
current window run
-security Start in the
current window with security manager start
Start Catalina in a
separate window start
-security Start in a
separate window with security manager stop
Stop Catalina, waiting up to 5 seconds for the
process to end stop
n Stop Catalina, waiting up to n seconds for the
process to end stop
-force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still
running stop
n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still
running configtest
Run a basic syntax check on server.xml - check exit code for result version
What version of tomcat are you running? Note:
Waiting for the
process to end and use of the -force option require that $CATALINA_PID is defined |
注,从帮助上来看,这个脚本使用还是挺简单的。下面我们来说几个常用的选项,
catalina.sh
-
start 启动Tomcat
-
stop 关闭Tomcat
-
configtest 测试配置文件是否有错
-
version 查看Tomcat版本
下面我们就来具体演示一下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@tomcat
~] #
catalina.sh configtest Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr/java/jdk1 .7.0_40 Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar 九月
21, 2013 11:08:26 下午 org.apache.catalina.core.AprLifecycleListener init 信息:
The APR based Apache Tomcat Native library which allows
optimal performance in production
environments was not found on the java.library.path: /usr/java/packages/lib/amd64 : /usr/lib64 : /lib64 : /lib : /usr/lib 九月
21, 2013 11:08:27 下午 org.apache.coyote.AbstractProtocol init 信息:
Initializing ProtocolHandler [ "http-bio-8080" ] 九月
21, 2013 11:08:27 下午 org.apache.coyote.AbstractProtocol init 信息:
Initializing ProtocolHandler [ "ajp-bio-8009" ] 九月
21, 2013 11:08:27 下午 org.apache.catalina.startup.Catalina load 信息:
Initialization processed in 1269
ms |
注,使用configtest选项时,得关闭Tomcat,不然会报错。
启动Tomcat,
1
2
3
4
5
6
|
[root@tomcat
~] #
catalina.sh start Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr/java/jdk1 .7.0_40 Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar |
关闭Tomcat,
1
2
3
4
5
6
|
[root@tomcat
~] #
catalina.sh stop Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr/java/jdk1 .7.0_40 Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar |
查看Tomcat版本,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@tomcat
~] #
catalina.sh version Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr/java/jdk1 .7.0_40 Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar Server
version: Apache Tomcat /7 .0.42 Server
built: Jul 2 2013 08:57:41 Server
number: 7.0.42.0 OS
Name: Linux OS
Version: 2.6.32-358.el6.x86_64 Architecture:
amd64 JVM
Version: 1.7.0_40-b43 JVM
Vendor: Oracle Corporation |
好了,catalina脚本,我们就说到这里了,下面我们来说一下telnet管理Tomcat。
3.telnet 登录管理Tomcat
注,在说telnet管理Tomcat之前,我们得先看一下默认的配置文件,这里面定义了默认的管理端口,
1
2
|
[root@tomcat
~] #
vim /usr/local/tomcat/conf/server.xml <Server
port= "8005" shutdown = "SHUTDOWN" > |
说明,定义了一个管理端口为8005,我们可以用telnet直接登录进本机的8005端口,来执行SHUTDOWN命令,来关闭Tomcat实例。下面我们来具体演示一下,
先安装telnet客户端,
1
|
[root@tomcat
~] #
yum install -y telnet |
下面我们一测试并查看,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@tomcat
~] #
telnet localhost 8005 Trying
::1... telnet:
connect to address ::1: Connection refused Trying
127.0.0.1... Connected
to localhost. Escape
character is '^]' . SHUTDOWN #输入SHOWDOWN就可以直接关闭Tomcat服务。 Connection
closed by foreign host. [root@tomcat
~] #
netstat -ntulp Active
Internet connections (only servers) Proto
Recv-Q Send-Q Local Address Foreign Address State PID /Program name tcp
0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1044 /sshd tcp
0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1121 /master tcp
0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 12988 /sshd tcp
0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 13053 /sshd tcp
0 0 :::22 :::* LISTEN 1044 /sshd tcp
0 0 ::1:25 :::* LISTEN 1121 /master tcp
0 0 ::1:6010 :::* LISTEN 12988 /sshd tcp
0 0 ::1:6011 :::* LISTEN 13053 /sshd |
注,大家可以看到Tomcat服务器已经关闭。好了,telnet管理我们就说到这里,下面我们来说一下,Tomcat虚拟主机的配置。
4.配置Tomcat虚拟主机
注,在说Tomcat虚拟主机之前,咱们来详细的看看默认的配置文件,虽然在上一篇博客中全部有讲解,在这篇博客中我还是再和大家简单说一下,下面是默认配置文件。大家可以看到,绝大部分的配置文件是注释,包含在<!-- -->、全是注释。下面我们就来具体的看看,注释我们就不说了,说具体的定义的内容
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
[root@tomcat
~] #
cat /usr/local/tomcat/conf/server.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= "8005" shutdown = "SHUTDOWN" > #大家可以看到,这里是我们刚才讲解的,定义一个管理接口 <!--
Security listener. Documentation at /docs/config/listeners .html <Listener
className= "org.apache.catalina.security.SecurityListener" /> --> <!--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" /> <Listener
className= "org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener
className= "org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!--
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" > #定义一个Service命令为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= "8080" protocol= "HTTP/1.1" connectionTimeout= "20000" redirectPort= "8443" /> #这里定义了一个连接器,协议为http,端口为8080,最大连接超时为20s,这里还定义了一个SSL的重定向端口8443。我们可以根据需要进行修改。一般我们都用80端口与443端口。 <!--
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" /> #这里定义了一个SSL的案例,主要定义相关密钥与证书。 --> <!--
Define an AJP 1.3 Connector on port 8009 --> <Connector
port= "8009" protocol= "AJP/1.3" redirectPort= "8443" /> #这里定义了一个支持AJP协议的连接器。 <!--
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" > #这里定义了一个名为Catalina的引擎,并定义了一个默认主机为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" /> --> <!--
Use the LockOutRealm to prevent attempts to guess user passwords via
a brute-force attack --> <Realm
className= "org.apache.catalina.realm.LockOutRealm" > <!--
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" /> < /Realm > <Host
name= "localhost" appBase= "webapps" unpackWARs= "true" autoDeploy= "true" > #这里就是定义的虚拟主机。 <!--
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 Note:
The pattern used is equivalent to using pattern= "common" --> <Valve
className= "org.apache.catalina.valves.AccessLogValve" directory= "logs" prefix= "localhost_access_log." suffix= ".txt" pattern= "%h
%l %u %t "%r" %s %b" /> #这里定义了一个日志相关的属性。 < /Host > < /Engine > < /Service > < /Server > |
好了,在这里我们又简单的说明一下,配置文件下面我们就来具体演示一下怎么配置虚拟主机。
首先,我们来修改一下配置文件,
[root@tomcat conf]# vim server.xml
#增加下面几行
1
2
3
4
|
<Host
name= "www.test.com" appBase= "/web/webapp" unpackWARs= "true" autoDeploy= "true" > <Context
path= "/" docBase= "/web/webapp" reloadable= "true" /> < /Host > |
接下来我们来创建文档目录与测试页面,
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@tomcat
~] #
mkdir -pv /web/webapp [root@tomcat
~] #
cd /web/webapp [root@tomcat
webapp] #
vim index.jsp <%@
page language= "java" %> <%@
page import = "java.util.*" %> <html> < head > <title>JSP test page.< /title > < /head > <body> <%
out.println( "Welcome
to test. Site, http://www.test.com" );
%> < /body > < /html > |
现在我们来测试一下我们修改的配置文件,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@tomcat
~] #
service tomcat stop Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar ^[[A[root@tomcat
service tomcat configtest Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar Sep
22, 2013 2:15:47 AM org.apache.catalina.core.AprLifecycleListener init INFO:
The APR based Apache Tomcat Native library which allows
optimal performance in production
environments was not found on the java.library.path: /usr/java/packages/lib/amd64 : /usr/lib64 : /lib64 : /lib : /usr/lib Sep
22, 2013 2:15:47 AM org.apache.coyote.AbstractProtocol init INFO:
Initializing ProtocolHandler [ "http-bio-80" ] Sep
22, 2013 2:15:47 AM org.apache.coyote.AbstractProtocol init INFO:
Initializing ProtocolHandler [ "ajp-bio-8009" ] Sep
22, 2013 2:15:47 AM org.apache.catalina.startup.Catalina load INFO:
Initialization processed in 1294
ms |
注,大家可以看到,我们这里没有报错,说明配置都是正确的,若配置有错误,会在最后一行提醒你。
再下面我们来启动Tomcat并测试一下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@tomcat
~] #
service tomcat start Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar [root@tomcat
~] #
netstat -ntulp Active
Internet connections (only servers) Proto
Recv-Q Send-Q Local Address Foreign Address State PID /Program name tcp
0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1044 /sshd tcp
0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1121 /master tcp
0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 13368 /sshd tcp
0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 13387 /sshd tcp
0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 13407 /sshd tcp
0 0 :::80 :::* LISTEN 13557 /java tcp
0 0 :::22 :::* LISTEN 1044 /sshd tcp
0 0 ::1:25 :::* LISTEN 1121 /master tcp
0 0 ::1:6010 :::* LISTEN 13368 /sshd tcp
0 0 ::1:6011 :::* LISTEN 13387 /sshd tcp
0 0 ::1:6012 :::* LISTEN 13407 /sshd tcp
0 0 :::8009 :::* LISTEN 13557 /java |
注,还有一点得说明一下,我这里为了方便测试,将默认端口8080修改为了80端口。下面是测试效果,
为了帮助大家理解,我们这里再次讲解一下,Host组件与Context组件以及相关属性。
Host组件:
位于Engine容器中用于接收请求并进行相应处理的主机或虚拟主机,如前面我们自定义的内容:
1
2
3
4
|
<Host
name= "www.test.com" appBase= "/web/webapp" unpackWARs= "true" autoDeploy= "true" > <Context
path= "/" docBase= "/web/webapp" reloadable= "true" /> < /Host > |
常用属性说明:
name:定义虚拟主机的域名
-
appBase:此Host的webapps目录,即存放非归档的web应用程序的目录或归档后的WAR文件的目录路径;可以使用基于$CATALINA_HOME的相对路径;
-
autoDeploy:在Tomcat处于运行状态时放置于appBase目录中的应用程序文件是否自动进行deploy;默认为true;
-
unpackWars:在启用此webapps时是否对WAR格式的归档文件先进行展开;默认为true;
主机别名定义:
如果一个主机有两个或两个以上的主机名,额外的名称均可以以别名的形式进行定义,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<Host
name= "www.test.com" appBase= "webapps" unpackWARs= "true" > <Alias>web. test .com< /Alias > < /Host > Context组件: Context在某些意义上类似于apache中的路径别名,一个Context定义用于标识tomcat实例中的一个Web应用程序。如下面的定义: <!--
Tomcat Root Context --> <Context
path= "" docBase= "/web/webapps" /> <!--
buzzin webapp --> <Context
path= "/bbs" docBase= "/web/threads/bbs" reloadable= "true" > < /Context > <!--
chat server --> <Context
path= "/chat" docBase= "/web/chat" /> <!--
darian web --> <Context
path= "/darian" docBase= "darian" /> |
在Tomcat中,每一个context定义也可以使用一个单独的XML文件进行,其文件的目录为$CATALINA_HOME/conf/<engine name>/<host name>。可以用于Context中的XML元素有Loader,Manager,Realm,Resources和WatchedResource。
常用的属性定义有:
-
docBase:相应的Web应用程序的存放位置;也可以使用相对路径,起始路径为此Context所属Host中appBase定义的路径;切记,docBase的路径名不能与相应的Host中appBase中定义的路径名有包含关系,比如,如果appBase为deploy,而docBase绝不能为deploy-bbs类的名字;
-
path:相对于Web服务器根路径而言的URI;如果为空“”,则表示为此webapp的根路径;如果context定义在一个单独的xml文件中,此属性不需要定义;
-
reloadable:是否允许重新加载此context相关的Web应用程序的类;默认为false;
为了便于大家理解,我们这里再定义一个Context并测试一下,
我们先来修改一下配置文件
1
2
3
4
5
6
|
[root@tomcat
conf] #
vim server.xml <Host
name= "www.test.com" appBase= "/web/webapp" unpackWARs= "true" autoDeploy= "true" > <Context
path= "/" docBase= "/web/webapp" reloadable= "true" /> <Context
path= "/test" docBase= "/web/test" reloadable= "true" /> #增加这一行 < /Host > |
下面来增加目录文档与测试文件,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@tomcat
webapp] #
mkdir /web/test [root@tomcat
webapp] #
cd /web/test [root@tomcat test ] #
vim index.jsp <%@
page language= "java" %> <html> < head ><title>TomcatA< /title >< /head > <body> <h1><font
color= "red" >TomcatA
< /h1 > <table
align= "centre" border= "1" > < tr > <td>Session
ID< /td > <%
session.setAttribute( "abc" , "abc" );
%> <td><%=
session.getId() %>< /td > < /tr > < tr > <td>Created
on< /td > <td><%=
session.getCreationTime() %>< /td > < /tr > < /table > < /body > < /html > |
测试一下配置文件是否有错并启动Tomcat,
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
33
34
35
36
|
[root@tomcat
~] #
service tomcat configtest Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar Sep
22, 2013 2:40:57 AM org.apache.catalina.core.AprLifecycleListener init INFO:
The APR based Apache Tomcat Native library which allows
optimal performance in production
environments was not found on the java.library.path: /usr/java/packages/lib/amd64 : /usr/lib64 : /lib64 : /lib : /usr/lib Sep
22, 2013 2:40:58 AM org.apache.coyote.AbstractProtocol init INFO:
Initializing ProtocolHandler [ "http-bio-80" ] Sep
22, 2013 2:40:58 AM org.apache.coyote.AbstractProtocol init INFO:
Initializing ProtocolHandler [ "ajp-bio-8009" ] Sep
22, 2013 2:40:58 AM org.apache.catalina.startup.Catalina load INFO:
Initialization processed in 1352
ms [root@tomcat
~] #
service tomcat start Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar [root@tomcat
~] #
netstat -ntulp Active
Internet connections (only servers) Proto
Recv-Q Send-Q Local Address Foreign Address State PID /Program name tcp
0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1044 /sshd tcp
0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1121 /master tcp
0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 13587 /sshd tcp
0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 13387 /sshd tcp
0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 13407 /sshd tcp
0 0 :::80 :::* LISTEN 13945 /java tcp
0 0 :::22 :::* LISTEN 1044 /sshd tcp
0 0 ::1:25 :::* LISTEN 1121 /master tcp
0 0 ::1:6010 :::* LISTEN 13587 /sshd tcp
0 0 ::1:6011 :::* LISTEN 13387 /sshd tcp
0 0 ::1:6012 :::* LISTEN 13407 /sshd tcp
0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 13945 /java tcp
0 0 :::8009 :::* LISTEN 13945 /java |
下面我们就用浏览器测试一下,
好了,到这里我们的Tomcat虚拟主机的讲解就到这里了,下面我们来说一下Tomcat图形管理接口。
5.Tomcat图形管理接口
-
manager 管理应用程序的部署及监控
-
host-manager 虚拟主机的管理
我们先来看一下默认的图形配置界面,
注,大家注意看右上角,我用红色方框标记出来的,大家可以看有三个按钮,分别为
-
Server Status 主要用来查看服务器的状态
-
Manager App 主要用来管理应用程序的部署及监控
-
Host Manager 主要用来管理虚拟主机
下面我们就来具休的配置一下,大家可以看到,你点击任何一个按钮都要输入用户名和密码的,在我们配置之前我们先来说一下,Tomcat的Manager功能,
Manager的四个管理角色:
-
manager-gui - allows access to the HTML GUI and the status pages
-
manager-script - allows access to the text interface and the status pages
-
manager-jmx - allows access to the JMX proxy and the status pages
-
manager-status - allows access to the status pages only
注,这里我说一下,上面的英文比较简单我就不在里翻译了,大家自己看一下。
下面我们就来启用manager功能,修改tomcat-user.xml文件,添加如下行:
1
2
3
|
<role
rolename= "manager-gui" /> <role
rolename= "admin-gui" /> <user
username= "tomcat" password= "tomcat" roles= "manager-gui,admin-gui" /> |
简单解释一下,Tomcat有内置的角色,我们这里增加了两个角色一个为manager-gui,另一个为admin-gui,用户名和密码都为tomcat。
注,增加的内容一定要在<tomcat-users></tomcat-users>之间。不然,不会生效。好了,下面我们一来测试一下配置文件,并重新启动一下Tomcat
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
33
34
35
36
|
[root@tomcat
~] #
service tomcat configtest Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar Sep
22, 2013 3:08:44 AM org.apache.catalina.core.AprLifecycleListener init INFO:
The APR based Apache Tomcat Native library which allows
optimal performance in production
environments was not found on the java.library.path: /usr/java/packages/lib/amd64 : /usr/lib64 : /lib64 : /lib : /usr/lib Sep
22, 2013 3:08:44 AM org.apache.coyote.AbstractProtocol init INFO:
Initializing ProtocolHandler [ "http-bio-80" ] Sep
22, 2013 3:08:44 AM org.apache.coyote.AbstractProtocol init INFO:
Initializing ProtocolHandler [ "ajp-bio-8009" ] Sep
22, 2013 3:08:44 AM org.apache.catalina.startup.Catalina load INFO:
Initialization processed in 1213
ms [root@tomcat
~] #
service tomcat start Using
CATALINA_BASE: /usr/local/tomcat Using
CATALINA_HOME: /usr/local/tomcat Using
CATALINA_TMPDIR: /usr/local/tomcat/temp Using
JRE_HOME: /usr Using
CLASSPATH: /usr/local/tomcat/bin/bootstrap .jar: /usr/local/tomcat/bin/tomcat-juli .jar [root@tomcat
~] #
netstat -ntulp Active
Internet connections (only servers) Proto
Recv-Q Send-Q Local Address Foreign Address State PID /Program name tcp
0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1044 /sshd tcp
0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1121 /master tcp
0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 13587 /sshd tcp
0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 13387 /sshd tcp
0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 13407 /sshd tcp
0 0 :::80 :::* LISTEN 14197 /java tcp
0 0 :::22 :::* LISTEN 1044 /sshd tcp
0 0 ::1:25 :::* LISTEN 1121 /master tcp
0 0 ::1:6010 :::* LISTEN 13587 /sshd tcp
0 0 ::1:6011 :::* LISTEN 13387 /sshd tcp
0 0 ::1:6012 :::* LISTEN 13407 /sshd tcp
0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 14197 /java tcp
0 0 :::8009 :::* LISTEN 14197 /java |
好了,下面我们用浏览器查看一下,
注,点击Server Status按钮,让你输入用户名和密码。我这里全部设置是tomcat。
然后,会出现以下界面。显示全部服务器运行状态!大家可以仔细的看一下,我就不带着大家看了。
下面是应用程序部署管理界面,
下面是虚拟主机管理页面,
注,我们一般在生产环境中用的最多是应用程序部署界面,可以进行热布署应用程序,很方便,大家可以尝试一下。好了,图形管理界面我们就说到这里了,下面我们来说一下,Tomcat的一个小案例。我们说了那么多,有朋友就会说了,怎么一个案例也没有呢?下面我们就来布署一个社交网站的案例JavaCenter Home。