Tomcat的安装(前提是已经安装Jdk)
1、下载安装tomcat,地址: http://tomcat.apache.org/download-80.cgi 2、解压 3、启动tomcat先使用 cd 命令进入tomcat的bin目录,命令如下:
cd ~/tomcat/bin/
启动服务命令:
sudo sh startup.sh
启动成功,会出现如下结果:
Using CATALINA_BASE: /ProgramFile/tomcat
Using CATALINA_HOME: /ProgramFile/tomcat
Using CATALINA_TMPDIR: /ProgramFile/tomcat/temp
Using JRE_HOME: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Using CLASSPATH: /ProgramFile/tomcat/bin/bootstrap.jar:/ProgramFile/tomcat/bin/tomcat-juli.jar
Tomcat started.
如果出现如上结果,说明tomcat启动成功
也可以在/Users/didi/software/apache-tomcat-8.0.36/logs/canalina.out(Tomcat的启动关闭日志)日志中查看,如果出现server startup in **ms, 则表示tomcat启动成功!如下图所示: 4、输入http://localhost:8080,如果看到猫的页面,即表示tomcat和jdk安装成功! 5、tomcat配置的更改在目录/Users/didi/software/apache-tomcat-8.0.36/conf下的server.xml文件中PS:如果同时安装nginx和tomcat,注意两者端口的更改。如果仅仅使用tomcat服务,需要关闭nginx服务。
Tomcat目录结构
123 | [root@tomcat # cd /usr/local/tomcat/ [root@tomcat # ls bin |
bin ——Tomcat执行脚本目录
conf ——Tomcat配置文件
lib ——Tomcat运行需要的库文件(JARS)
logs ——Tomcat执行时的LOG文件
temp ——Tomcat临时文件存放目录
webapps ——Tomcat的主要Web发布目录(存放我们自己的JSP,SERVLET,类)
work ——Tomcat的工作目录,Tomcat将翻译JSP文件到的Java文件和class文件放在这里。
12345 | [root@tomcat # ls bin/ bootstrap.jar catalina.bat tar .gz daemon.sh setclasspath.sh startup.sh tool-wrapper.sh catalina.sh shutdown .bat tomcat-juli.jar version.bat catalina-tasks.xml shutdown .sh tomcat-native. tar .gz version.sh |
catalina.sh 用于启动和关闭tomcat服务器
configtest.sh 用于检查配置文件
startup.sh 启动Tomcat脚本
shutdown.sh 关闭Tomcat脚本
12 | [root@tomcat # ls conf/ Catalina users .xml web.xml |
server.xml Tomcat 的全局配置文件
web.xml 为不同的Tomcat配置的web应用设置缺省值的文件
tomcat-users.xml Tomcat用户认证的配置文件
12345 | [root@tomcat # ls lib/ annotations-api.jar catalina-ant.jar catalina-ha.jar catalina.jar |
123 | [root@tomcat # ls logs/ catalina.2013-09-18.log catalina.out |
localhost_access_log.2013-09-18.txt 访问日志
localhost.2013-09-18.log 错误和其它日志
manager.2013-09-18.log 管理日志
catalina.2013-09-18.log Tomcat启动或关闭日志文件
12 | [root@tomcat # ls webapps/ docs |
1234 | [root@tomcat # ls work/ Catalina [root@tomcat # ls work/Catalina/localhost/ _/ |
WEB-INF目录中包含应用软件所使用的资源,但是WEB-INF却不在公共文档根目录之中。在这个目录中所包含的文件都不能被客户机所访问。
类目录中(在WEB-INF下)包含运行Web应用程序时所需的Servlets,Beans等类。
lib目录(在WEB-INF下)包含有Java archive files (JARs),例如标签库或者Servlets,Beans等类的*.jar文件。
如果一个类出现在JAR文件中同时也出现在类的目录中,类加载器会加载位于类目录中的那一个。
其中msbase.jar、mssqlserver.jar、msutil.jar文件为SqlServer2000的JDBC驱动程序
其中servlet-api.jar和jsp-api.jar为Servlet和JSP的API所在的包
123456789101112 | [root@tomcat # cd /usr/local/tomcat/ [root@tomcat # ls bin [root@tomcat # cd webapps/ [root@tomcat # ls docs [root@tomcat # cd examples/ [root@tomcat # ls index.html [root@tomcat # cd WEB-INF/ [root@tomcat # ls classes |
Tomcat配置文件
1.简介
123 | [root@tomcat # cd /usr/local/tomcat/conf/ [root@tomcat # ls Catalina users .xml web.xml |
server.xml: Tomcat的主配置文件,包含Service, Connector, Engine, Realm, Valve, Hosts主组件的相关配置信息;
web.xml:遵循Servlet规范标准的配置文件,用于配置servlet,并为所有的Web应用程序提供包括MIME映射等默认配置信息;
tomcat-user.xml:Realm认证时用到的相关角色、用户和密码等信息;Tomcat自带的manager默认情况下会用到此文件;在Tomcat中添加/删除用户,为用户指定角色等将通过编辑此文件实现;
catalina.policy:Java相关的安全策略配置文件,在系统资源级别上提供访问控制的能力;
catalina.properties:Tomcat内部package的定义及访问相关的控制,也包括对通过类装载器装载的内容的控制;Tomcat在启动时会事先读取此文件的相关设置;
logging.properties: Tomcat通过自己内部实现的JAVA日志记录器来记录操作相关的日志,此文件即为日志记录器相关的配置信息,可以用来定义日志记录的组件级别以及日志文件的存在位置等;
context.xml:所有host的默认配置信息;
2.server.xml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 | [root@tomcat # cat server.xml <?xml '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. --> <!-- "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 "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" > <!--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" /> <!-- 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= "8009" protocol= "AJP/1.3" redirectPort= "8443" /> <!-- 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" /> --> <!-- 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 > |
1 | <Server shutdown =”SHUTDOWN”> |
className: 用于实现此Server容器的完全限定类的名称,默认为org.apache.catalina.core.StandardServer;
port: 接收shutdown指令的端口,默认仅允许通过本机访问,默认为8005;
shutdown:发往此Server用于实现关闭tomcat实例的命令字符串,默认为SHUTDOWN;
className: 用于实现service的类名,一般都是org.apache.catalina.core.StandardService。
name:此服务的名称,默认为Catalina;
Tomcat作为应用程序服务器:请求来自于前端的web服务器,这可能是Apache, IIS, Nginx等;
Tomcat作为独立服务器:请求来自于web浏览器;
HTTP连接器
SSL连接器
AJP 1.3连接器
proxy连接器
123 | <Connector "8080" protocol= "HTTP/1.1" maxThreads= "150" connectionTimeout= "20000" redirectPort= "8443" /> |
address:指定连接器监听的地址,默认为所有地址,即0.0.0.0;
maxThreads:支持的最大并发连接数,默认为200;
port:监听的端口,默认为0;
protocol:连接器使用的协议,默认为HTTP/1.1,定义AJP协议时通常为AJP/1.3;
redirectPort:如果某连接器支持的协议是HTTP,当接收客户端发来的HTTPS请求时,则转发至此属性定义的端口;
connectionTimeout:等待客户端发送请求的超时时间,单位为毫秒,默认为60000,即1分钟;
enableLookups:是否通过request.getRemoteHost()进行DNS查询以获取客户端的主机名;默认为true;
acceptCount:设置等待队列的最大长度;通常在tomcat所有处理线程均处于繁忙状态时,新发来的请求将被放置于等待队列中;
1234 | <Connector "8443" maxThreads= "150" minSpareThreads= "25" maxSpareThreads= "75" enableLookups= "false" acceptCount= "100" debug= "0" scheme= "https" secure= "true" clientAuth= "false" sslProtocol= "TLS" /> |
defaultHost:Tomcat支持基于FQDN的虚拟主机,这些虚拟主机可以通过在Engine容器中定义多个不同的Host组件来实现;但如果此引擎的连接器收到一个发往非非明确定义虚拟主机的请求时则需要将此请求发往一个默认的虚拟主机进行处理,因此,在Engine中定义的多个虚拟主机的主机名称中至少要有一个跟defaultHost定义的主机名称同名;
name:Engine组件的名称,用于日志和错误信息记录时区别不同的引擎;
1234 | <Host "localhost" appBase= "webapps" unpackWARs= "true" autoDeploy= "true" xmlValidation= "false" xmlNamespaceAware= "false" > < /Host > |
appBase:此Host的webapps目录,即存放非归档的web应用程序的目录或归档后的WAR文件的目录路径;可以使用基于$CATALINA_HOME的相对路径;
autoDeploy:在Tomcat处于运行状态时放置于appBase目录中的应用程序文件是否自动进行deploy;默认为true;
unpackWars:在启用此webapps时是否对WAR格式的归档文件先进行展开;默认为true;
12345678910 | <Engine "Catalina" defaultHost= "localhost" > <Host name= "localhost" appBase= "webapps" > <Context path= "" docBase= "ROOT" /> <Context path= "/bbs" docBase= "/web/bss" reloadable= "true" crossContext= "true" /> < /Host > <Host name= "mail.test.com" appBase= "/web/mail" > <Context path= "" docBase= "ROOT" /> < /Host > < /Engine > |
123 | <Host "www.test.com" appBase= "webapps" unpackWARs= "true" > <Alias> test .com< /Alias > < /Host > |
1234567891011 | <!-- <Context "" docBase= "/web/webapps" /> <!-- <Context "/bbs" docBase= "/web/threads/bbs" reloadable= "true" > < /Context > <!-- <Context path= "/chat" docBase= "/web/chat" /> <!-- <Context "/darian" docBase= "darian" /> |
docBase:相应的Web应用程序的存放位置;也可以使用相对路径,起始路径为此Context所属Host中appBase定义的路径;切记,docBase的路径名不能与相应的Host中appBase中定义的路径名有包含关系,比如,如果appBase为deploy,而docBase绝不能为deploy-bbs类的名字;
path:相对于Web服务器根路径而言的URI;如果为空“”,则表示为此webapp的根路径;如果context定义在一个单独的xml文件中,此属性不需要定义;
reloadable:是否允许重新加载此context相关的Web应用程序的类;默认为false;
JAASRealm:基于Java Authintication and Authorization Service实现用户认证;
JDBCRealm:通过JDBC访问某关系型数据库表实现用户认证;
JNDIRealm:基于JNDI使用目录服务实现认证信息的获取;
MemoryRealm:查找tomcat-user.xml文件实现用户信息的获取;
UserDatabaseRealm:基于UserDatabase文件(通常是tomcat-user.xml)实现用户认证,它实现是一个完全可更新和持久有效的MemoryRealm,因此能够跟标准的MemoryRealm兼容;它通过JNDI实现;
12345678910 | <Realm resourceName=”UserDatabase”/> 下面是一个使用JDBC方式获取用户认证信息的配置: <Realm className= "org.apache.catalina.realm.JDBCRealm" debug= "99" driverName= "org.gjt.mm.mysql.Driver" connectionURL= "jdbc:mysql://localhost/authority" connectionName= "test" connectionPassword= "test" userTable= "users" userNameCol= "user_name" userCredCol= "user_pass" userRoleTable= "user_roles" roleNameCol= "role_name" /> |
AccessLogValve:访问日志Valve
ExtendedAccessValve:扩展功能的访问日志Valve
JDBCAccessLogValve:通过JDBC将访问日志信息发送到数据库中;
RequestDumperValve:请求转储Valve;
RemoteAddrValve:基于远程地址的访问控制;
RemoteHostValve:基于远程主机名称的访问控制;
SemaphoreValve:用于控制Tomcat主机上任何容器上的并发访问数量;
JvmRouteBinderValve:在配置多个Tomcat为以Apache通过mod_proxy或mod_jk作为前端的集群架构中,当期望停止某节点时,可以通过此Valve将用记请求定向至备用节点;使用此Valve,必须使用JvmRouteSessionIDBinderListener;
ReplicationValve:专用于Tomcat集群架构中,可以在某个请求的session信息发生更改时触发session数据在各节点间进行复制;
SingleSignOn:将两个或多个需要对用户进行认证webapp在认证用户时连接在一起,即一次认证即可访问所有连接在一起的webapp;
ClusterSingleSingOn:对SingleSignOn的扩展,专用于Tomcat集群当中,需要结合ClusterSingleSignOnListener进行工作;
1234 | <Context "/probe" docBase= "probe" > <Valve "org.apache.catalina.valves.RemoteAddrValve" allow= "127\.0\.0\.1" /> < /Context > |
className:相关的java实现的类名,相应于分别应该为org.apache.catalina.valves.RemoteHostValve或org.apache.catalina.valves.RemoteAddrValve;
allow:以逗号分开的允许访问的IP地址列表,支持正则表达式,因此,点号“.”用于IP地址时需要转义;仅定义allow项时,非明确allow的地址均被deny;
deny: 以逗号分开的禁止访问的IP地址列表,支持正则表达式;使用方式同allow;
Environment;
Resource;
ResourceEnvRef;
Channel 用于Cluster中给集群中同一组中的节点定义通信“信道”。Channel中需要至少定义Membership、Receiver和Sender三个元素,此外还有一个可选元素Interceptor。
Membership 用于Channel中配置同一通信信道上节点集群组中的成员情况,即监控加入当前集群组中的节点并在各节点间传递心跳信息,而且可以在接收不到某成员的心跳信息时将其从集群节点中移除。Tomcat中Membership的实现是org.apache.catalina.tribes.membership.McastService。
Sender 用于Channel中配置“复制信息”的发送器,实现发送需要同步给其它节点的数据至集群中的其它节点。发送器不需要属性的定义,但可以在其内部定义一个Transport元素。
Transport 用于Sender内部,配置数据如何发送至集群中的其它节点。Tomcat有两种Transport的实现:
1) PooledMultiSender
基于Java阻塞式IO,可以将一次将多个信息并发发送至其它节点,但一次只能传送给一个节点。
2)PooledParallelSener
基于Java非阻塞式IO,即NIO,可以一次发送多个信息至一个或多个节点。Receiver 用于Channel定义某节点如何从其它节点的Sender接收复制数据,Tomcat中实现的接收方式有两种BioReceiver和NioReceiver。