Tomcat详解
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat详解相关的知识,希望对你有一定的参考价值。
Tomcat详解
1、tomcat安装
tomcat的组件: <Server> <Service> <connector/> <connector/> <Engine> <Host /> <Host> <Context/> ... </Host> </Engine> </Service> </Server> 每个组件,均有java类来实现; 此些组件可分为如下几类: 顶级组件:Server 服务类:Service 连接器:http, https, ajp(apache jserv protocol) 容器类:Engine, Host, Context 被嵌套的组件:valve, logger, realm, loader, manager 集群类组件:listener, ...
OS版本CentOS 7
(1)系统自带的openjdk+tomcat
- 查看yum仓库中jdk版本: yum list all *jdk* 仓库中现有的openjdk版本为 java-1.6.0-openjdk java-1.7.0-openjdk java-1.8.0-openjdk - 本次测试使用java-1.7.0-openjdk: yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel -y - 设定java的环境变量($JAVA_HOME): 查看java目录 [[email protected] ~]# which java /usr/bin/java [[email protected] ~]# ls -l /usr/bin/java lrwxrwxrwx 1 root root 22 4月 26 09:46 /usr/bin/java -> /etc/alternatives/java [[email protected] ~]# ls -l /etc/alternatives/java lrwxrwxrwx 1 root root 75 4月 26 09:46 /etc/alternatives/java -> /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.13 1-2.6.9.0.el7_3.x86_64/jre/bin/java ~]# java -version 查看bin目录环境变量是否正常 java version "1.7.0_131" OpenJDK Runtime Environment (rhel-2.6.9.0.el7_3-x86_64 u131-b00) OpenJDK 64-Bit Server VM (build 24.131-b00, mixed mode) 添加$JAVA_HOME ~]# vim /etc/profile.d/java.sh export JAVA_HOME=/usr/bin ~]# . /etc/profile.d/java.sh ~]# echo $JAVA_HOME /usr/bin -tomcat安装 ~]# yum install tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps tomcat的配置文件查看 ~]# rpm -ql tomcat /etc/logrotate.d/tomcat /etc/sysconfig/tomcat /etc/tomcat /etc/tomcat/Catalina /etc/tomcat/Catalina/localhost /etc/tomcat/catalina.policy /etc/tomcat/catalina.properties /etc/tomcat/conf.d /etc/tomcat/conf.d/README /etc/tomcat/context.xml /etc/tomcat/log4j.properties /etc/tomcat/logging.properties /etc/tomcat/server.xml /etc/tomcat/tomcat-users.xml /etc/tomcat/tomcat.conf /etc/tomcat/web.xml /usr/bin/tomcat-digest /usr/bin/tomcat-tool-wrapper /usr/lib/systemd/system/tomcat.service /usr/lib/systemd/system/[email protected] /usr/libexec/tomcat /usr/libexec/tomcat/functions /usr/libexec/tomcat/preamble /usr/libexec/tomcat/server /usr/sbin/tomcat /usr/share/doc/tomcat-7.0.69 /usr/share/doc/tomcat-7.0.69/LICENSE /usr/share/doc/tomcat-7.0.69/NOTICE /usr/share/doc/tomcat-7.0.69/RELEASE-NOTES /usr/share/tomcat /usr/share/tomcat/bin/bootstrap.jar /usr/share/tomcat/bin/catalina-tasks.xml /usr/share/tomcat/conf /usr/share/tomcat/lib /usr/share/tomcat/logs /usr/share/tomcat/temp /usr/share/tomcat/webapps /usr/share/tomcat/work /var/cache/tomcat /var/cache/tomcat/temp /var/cache/tomcat/work /var/lib/tomcat /var/lib/tomcat/webapps /var/lib/tomcats /var/log/tomcat /var/log/tomcat/catalina.out - tomcat开启:8080和8009端口均正常开启 默认连接器: http, 8080 ajp, 8009 ~]# systemctl start tomcat.service ~]# ss -tnlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=1078,fd=3))LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1423,fd=13))LISTEN 0 100 :::8009 :::* users:(("java",pid=2942,fd=47))LISTEN 0 100 :::8080 :::* users:(("java",pid=2942,fd=46))LISTEN 0 128 :::22 :::* users:(("sshd",pid=1078,fd=4))LISTEN 0 100 ::1:25 :::* users:(("master",pid=1423,fd=14))
(2)使用Oracle JDK和tomcat提供的编译完成的程序包
- Oracle JKD安装 ~]# rpm -ivh jdk-7u79-linux-x64.rpm 准备中... ################################# [100%] 正在升级/安装... 1:jdk-2000:1.7.0_79-fcs ################################# [100%] Unpacking JAR files... rt.jar... jsse.jar... charsets.jar... tools.jar... localedata.jar... jfxrt.jar... - java环境变量设置 查看java目录 ~]# ls -l /usr/java/ 总用量 4 lrwxrwxrwx 1 root root 16 4月 26 10:23 default -> /usr/java/latest drwxr-xr-x 8 root root 4096 4月 26 10:23 jdk1.7.0_79 lrwxrwxrwx 1 root root 21 4月 26 10:23 latest -> /usr/java/jdk1.7.0_79 java的bin目录 ~]# cd /usr/java/jdk1.7.0_79/ jdk1.7.0_79]# ls bin include LICENSE release THIRDPARTYLICENSEREADME.txt COPYRIGHT jre man src.zip db lib README.html THIRDPARTYLICENSEREADME-JAVAFX.txt jdk1.7.0_79]# ls bin appletviewer jarsigner javap jdb jps jvisualvm rmic tnameserv apt java java-rmi.cgi jhat jrunscript keytool rmid unpack200 ControlPanel javac javaws jinfo jsadebugd native2ascii rmiregistry wsgen extcheck javadoc jcmd jmap jstack orbd schemagen wsimport idlj javafxpackager jconsole jmc jstat pack200 serialver xjc jar javah jcontrol jmc.ini jstatd policytool servertool 设定环境变量,添加JAVA_HOME和PATH ~]# vim /etc/profile.d/java.sh JAVA_HOME=/usr/java/latest PATH=$JAVA_HOME/bin:$PATH export JAVA_HOME PATH ~]# . /etc/profile.d/java.sh ~]# echo $JAVA_HOME /usr/java/latest ~]# echo $PATH /usr/java/latest/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin ~]# java -version java version "1.7.0_79" Java(TM) SE Runtime Environment (build 1.7.0_79-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode) - 安装tomcat ~]# tar -xf apache-tomcat-7.0.55.tar.gz -C /usr/local/ ~]# cd /usr/local/ local]# ll 总用量 0 drwxr-xr-x 9 root root 149 4月 26 10:34 apache-tomcat-7.0.55 drwxr-xr-x. 2 root root 6 8月 12 2015 bin drwxr-xr-x. 2 root root 6 8月 12 2015 etc drwxr-xr-x. 2 root root 6 8月 12 2015 games drwxr-xr-x. 2 root root 6 8月 12 2015 include drwxr-xr-x. 2 root root 6 8月 12 2015 lib drwxr-xr-x. 2 root root 6 8月 12 2015 lib64 drwxr-xr-x. 2 root root 6 8月 12 2015 libexec drwxr-xr-x. 2 root root 6 8月 12 2015 sbin drwxr-xr-x. 5 root root 46 11月 7 16:32 share drwxr-xr-x. 2 root root 6 8月 12 2015 src local]# ln -s apache-tomcat-7.0.55 tomcat ocal]# cd tomcat/ tomcat]# ls 目录结构 bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work tomcat]# ls conf/ catalina.policy context.xml server.xml web.xml catalina.properties logging.properties tomcat-users.xml tomcat]# ls webapps/ docs examples host-manager manager ROOT tomcat]# ls bin/ bootstrap.jar commons-daemon-native.tar.gz digest.sh startup.bat tool-wrapper.sh catalina.bat configtest.bat setclasspath.bat startup.sh version.bat catalina.sh configtest.sh setclasspath.sh tomcat-juli.jar version.sh catalina-tasks.xml daemon.sh shutdown.bat tomcat-native.tar.gz commons-daemon.jar digest.bat shutdown.sh tool-wrapper.bat - 设定tomcat环境变量 ~]# vim /etc/profile.d/tomcat.sh CATALINA_BASE=/usr/local/tomcat PATH=$CATALINA_BASE/bin:$PATH export CATALINA_BASE PATH ~]# . /etc/profile.d/tomcat.sh ~]# catalina.sh --help Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest 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 -开启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/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar 四月 26, 2017 10:40:10 上午 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environmen ts was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib四月 26, 2017 10:40:11 上午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-bio-8080"] 四月 26, 2017 10:40:11 上午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["ajp-bio-8009"] 四月 26, 2017 10:40:11 上午 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 1328 ms ~]# catalina.sh start 开启tomcat Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. ~]# ss -tnlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=1071,fd=3))LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1429,fd=13))LISTEN 0 100 :::8009 :::* users:(("java",pid=16417,fd=43))LISTEN 0 100 :::8080 :::* users:(("java",pid=16417,fd=42))LISTEN 0 128 :::22 :::* users:(("sshd",pid=1071,fd=4))LISTEN 0 100 ::1:25 :::* users:(("master",pid=1429,fd=14)) 目录文件查看: ~]# cd /usr/local/tomcat/ tomcat]# ls bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work tomcat]# cd webapps/ [[email protected] webapps]# ls docs examples host-manager manager ROOT webapps]# tree manager/ manager/ ├── images │ ├── add.gif │ ├── asf-logo.gif │ ├── code.gif │ ├── design.gif │ ├── docs.gif │ ├── fix.gif │ ├── tomcat.gif │ ├── update.gif │ └── void.gif ├── index.jsp ├── META-INF │ └── context.xml ├── status.xsd ├── WEB-INF │ ├── jsp │ │ ├── 401.jsp │ │ ├── 403.jsp │ │ ├── 404.jsp │ │ ├── sessionDetail.jsp │ │ └── sessionsList.jsp │ └── web.xml └── xform.xsl 4 directories, 19 files webapps]# tree ROOT/ ROOT/ ├── asf-logo.png ├── asf-logo-wide.gif ├── bg-button.png ├── bg-middle.png ├── bg-nav-item.png ├── bg-nav.png ├── bg-upper.png ├── build.xml ├── favicon.ico ├── index.jsp ├── RELEASE-NOTES.txt ├── tomcat.css ├── tomcat.gif ├── tomcat.png ├── tomcat-power.gif ├── tomcat.svg └── WEB-INF └── web.xml 1 directory, 17 files webapps]# tree host-manager/ host-manager/ ├── images │ ├── add.gif │ ├── asf-logo.gif │ ├── code.gif │ ├── design.gif │ ├── docs.gif │ ├── fix.gif │ ├── tomcat.gif │ ├── update.gif │ └── void.gif ├── index.jsp ├── manager.xml ├── META-INF │ └── context.xml └── WEB-INF ├── jsp │ ├── 401.jsp │ ├── 403.jsp │ └── 404.jsp └── web.xml
tomcat的配置文件:
server.xml:主配置文件; context.xml:每个webapp都可以有专用的配置文件,这些配置文件通常位于webapp程序目录下的WEB-INF目录中,用于定义会话管理顺、JDBC等 ;conf/context.xml是为各webapp提供默认配置; web.xml:每个webapp只有在“部署”之后才能够被访问;此文件则用于为各webapps定义默认的部署操作方式; tomcat-users.xml:用户认证的账号和密码配置文件; catalina.policy:当使用-security选项来启动tomcat实例时会读取此配置文件来实现基于安全策略的运行方式; catalina.properties:Java属性的定义文件,用于设定类加载器路径等 ,以及一些与JVM性能相关的调优参数; logging.properties:日志系统相关的配置;
Java Webapp的组织结构:
有特定的组织形式、层次型的目录结构;主要包含了servlet代码文件、JSP页面文件、类文件、部署描述符文件等 ; /:webapps的根目录 index.jsp:jsp的主页面文件 WEB-INF/:当前webapp的私有资源目录,通常存放当前webapp自用的web.xml; META-INF/:当前webapp的私有资源目录,通常存储当前webapp自用的context.xml; classes/: 当前webapp的私有类; lib/:当前webapp的私有类,被打包成jar格式; webapp归档格式: .war:webapp .jar:EJB的类; .rar:资源适配器; .ear:企业级应用程序;
2、tomcat应用程序部署
部署(deploy)webapp的相关操作: deploy:将webapp的源文件放置于目标目录、配置tomcat服务器能够基于context.xml文件中定义的路径来访问此webapp;将其特有的类通过class loader装载至tomcat: 有两种方式: 自动部署:auto deploy 手动部署: (1) 冷部署:把webapp复制到指定位置,而后才启动tomcat; (2) 热部署:在不停止tomcat的前提下进行的部署: 部署工具:manager app、ant脚本、tcd(Tomcat Client Deployer)等; undeploy:反部署,停止webapp,并从tomcat实例上拆除其部署文件和部署名; stop:停止,不再向用户提供服务; start:启动处于“停止”状态的webapp; redeploy:重新部署; tomcat自带的应用程序: manager app: webapp管理工具; host manager: VHosts管理工具;
(1)手动创建一个测试应用程序
~]# cd /usr/local/tomcat/webapps/ webapps]# ls docs examples host-manager manager ROOT webapps]# mkdir myapp webapps]# cd myapp/ myapp]# mkdir class lib WEB-INF META-INF [[email protected] myapp]# ls class lib META-INF WEB-INF myapp]# vim index.jsp myapp]# cat index.jsp <%@ page language="java" %> <%@ page import="java.util.*" %> <html> <head> <title>JSP Test Page</title> </head> <body> <% out.println("hello world"); %> </body> </html>
此时由于tomcat默认为热部署,所以网页已经可以正常访问 tomcat会默认把jsp网页编译成java语言 ~]# cd /usr/local/tomcat/work/Catalina/localhost/myapp/org/apache/jsp/ jsp]# ls index_jsp.class index_jsp.java
(2)tomcat自带的应用程序
提示用户在conf/tomcat-users.xml下进行配置 ~]# cd /usr/local/tomcat/conf/ conf]# vim tomcat-users.xml 在<tomcat-users>下面添加 <role rolename="manager-gui"/> <user username="tomcat" password="tomcat" roles="manager-gui"/> 重启tomcat服务 conf]# 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/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar conf]# 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/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar 四月 26, 2017 12:18:23 下午 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environmen ts was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib四月 26, 2017 12:18:24 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-bio-8080"] 四月 26, 2017 12:18:24 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["ajp-bio-8009"] 四月 26, 2017 12:18:24 下午 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 1348 ms conf]# 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/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. conf]# ss -tnlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=1071,fd=3))LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1429,fd=13))LISTEN 0 100 :::8009 :::* users:(("java",pid=16600,fd=43))LISTEN 0 100 :::8080 :::* users:(("java",pid=16600,fd=42))LISTEN 0 128 :::22 :::* users:(("sshd",pid=1071,fd=4))LISTEN 0 100 ::1:25 :::* users:(("master",pid=1429,fd=14))
管理页面可以对web app进行部署,开启,关闭,reload,update
(3)部署一个商城app进行测试
商城app解压 ~]# unzip shopxx-a5-Beta.zip ~]# ls all.sql apache-tomcat-7.0.55.tar.gz shopxx-a5-Beta.zip 说明.htm anaconda-ks.cfg jdk-7u79-linux-x64.rpm shopxx-v3.0-Beta 将文件mv至部署目录 ~]# cd shopxx-v3.0-Beta/shopxx-3.0Beta/ shopxx-3.0Beta]# ls admin favicon.ico install META-INF robots.txt upload changelog.txt index.jsp license.html resources shopxx.txt WEB-INF hopxx-3.0Beta]# ls install/ check_encoding.jsp css index.html install_demo.jsp js check.jsp data index.jsp install_init.jsp sample common.jsp images install_config.jsp install.jsp setting.jsp shopxx-3.0Beta]# cd .. shopxx-v3.0-Beta]# ls shopxx-3.0Beta shopxx-v3.0-Beta]# mv shopxx-3.0Beta/ /usr/local/tomcat/webapps/shopxx
此时网站已经可以正常访问
配置后台数据库 ~]# yum -y install mariadb-server ~]# systemctl start mariadb.service ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.52-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. MariaDB [(none)]> GRANT ALL ON shopxx.* to [email protected]‘localhost‘ IDENTIFIED BY ‘shopxx‘; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> GRANT ALL ON shopxx.* to [email protected]‘127.0.0.1‘ IDENTIFIED BY ‘shopxx‘; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> exit Bye
安装提示删除install目录,并重启
~]# cd /usr/local/tomcat/ tomcat]# ls bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work tomcat]# cd webapps/ webapps]# ls docs examples host-manager manager myapp ROOT shopxx webapps]# cd shopxx/ shopxx]# ls admin changelog.txt index.html license.html product robots.txt sitemap WEB-INF article favicon.ico install META-INF resources shopxx.txt upload shopxx]# mv install/ install.bak
此时我的tomcat管理界面上已经有shopxx应用,进行reload操作,或者使用catalina.sh进行stop再start
此时网站部署完成
3、tomcat常用组件
每个组件几乎都是通过特有的类来实现,而且有的组件还不止一种实现方式
Server:tomcat实例,即运行的一个jvm进程;监听于8005端口接收“SHUTDOWN”。各server监听的端口不能相同,因此,一个物理主机上启动多个server实例应该使用不同的端口;
service:用于实现将一个或多个connector关联至一个engine;
connector组件:
进入tomcat的请求可分为两类: tomcat做为独立的应用程序服务器:standalone,此时,请求将来自于浏览器; http, https tomcat做为应用程序服务器:请求将来自于前面反代主机; httpd: http, https, ajp nginx: http, https 属性: address:监听的IP地址; maxThreads:最大并发连接数,默认为150; port:监听的端口; protocol:连接器使用的协议,一般为HTTP/1.1或AJP/1.3; redirecPort: connectionTimeout:连接的超时时长,单位为毫秒,默认为60000; enableLookups: acceptCount:定义等待队列的长度; scheme: debug: secure: clinetAuth: sslProtocol: Engine:Servlet的一个实例,即servlet引擎,其内部可以有一个或多个Host组件来定义站点;通常需要通过defaultHost的属性定义默认虚拟主机; 属性: name= defaultHost= jvmRoute=
Host组件:
位于Engine容器中用于接收请求并进行相应处理的主机或虚拟主机
常用属性说明: 1) appBase:此Host的webapps目录,即存放非归档的web应用程序的目录或归档后的WAR文件的目录路径;可以使用基于$CATALINA_HOME的相对路径; 2) autoDeploy:在Tomcat处于运行状态时放置于appBase目录中的应用程序文件是否自动进行deploy;默认为true; 3) unpackWars:在启用此webapps时是否对WAR格式的归档文件先进行展开;默认为true;
示例: - 服务器端 修改server.xml,添加Host主机 ~]# cd /usr/local/tomcat/conf/ conf]# vim server.xml <Host name="node2" appBase="/data/webapps" unpackWARs="true" autoDeploy="true"> </Host> conf]# mkdir -pv /data/webapps/ROOT mkdir: 已创建目录 "/data" mkdir: 已创建目录 "/data/webapps" mkdir: 已创建目录 "/data/webapps/ROOT" conf]# cp ../webapps/myapp/index.jsp /data/webapps/ROOT/ conf]# mkdir /data/webapps/ROOT/{classes,lib,WEB-INF,META-INF} 修改index.jsp内容,方便后续辨认是哪个主机 conf]# vim /data/webapps/ROOT/index.jsp <%@ page language="java" %> <%@ page import="java.util.*" %> <html> <head> <title>JSP Test Page</title> </head> <body> <% out.println("hello world from /data/webapps"); %> </body> </html> catalina重启 catalina.sh stop catalina.sh configtest catalina.sh start -客户端 首先要保证客户端可以解析node这个主机 ~]# ping node2 PING node2 (192.168.150.138) 56(84) bytes of data. 64 bytes from node2 (192.168.150.138): icmp_seq=1 ttl=64 time=1.69 ms 访问测试: ~]# curl http://node2:8080 此时访问的是node2的目录 <html> <head> <title>JSP Test Page</title> </head> <body> hello world from /data/webapps </body> </html> [[email protected] ~]# curl http://192.168.150.138:8080 访问ip时还是访问原目录 <!DOCTYPE html> <html lang="en"> <head> <title>Apache Tomcat/7.0.55</title> <link href="favicon.ico" rel="icon" type="image/x-icon" /> <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" /> <link href="tomcat.css" rel="stylesheet" type="text/css" /> </head>
tomcat自带Host Manager配置
配置方法和Manager App相同,我只要在刚才manager app配置中添加Host Manager属性即可,点击按钮也会出现提示画面
修改内容为: conf]# vim tomcat-users.xml <tomcat-users> <role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/> 修改后重启catalina catalina.sh stop catalina.sh configtest catalina.sh start
此时在Host Manager界面可以看到我刚才添加的HOST主机
Context组件:
示例: - 服务器端 conf]# vim server.xml 在原Host内容中添加Context内容 <Host name="node2" appBase="/data/webapps" unpackWARs="true" autoDeploy="true"> <Context path="/test" docBase="testapp" reloadable="true"> </Context> </Host> 创建testapp测试页面 conf]# cp /data/webapps/ROOT/ /data/webapps/testapp -r conf]# vim /data/webapps/testapp/index.jsp <%@ page language="java" %> <%@ page import="java.util.*" %> <html> <head> <title>JSP Test Page for Context</title> </head> <body> <% out.println("hello world from /data/webapps/testapp"); %> </body> </html> catalina重启 catalina.sh stop catalina.sh configtest catalina.sh start -客户端访问测试: 访问node2 [[email protected] conf]# curl http://node2:8080 <html> <head> <title>JSP Test Page</title> </head> <body> hello world from /data/webapps </body> </html> 访问Conext目录 [[email protected] conf]# curl http://node2:8080/test/ <html> <head> <title>JSP Test Page for Context</title> </head> <body> hello world from /data/webapps/testapp </body> </html>
Value组件:
示例: 继续在刚才的测试示例中添加Value组件: - 服务器端 制定日志格式,设定testapp的访问控制(192.168.150.139为客户端主机) conf]# vim server.xml <Host name="node2" appBase="/data/webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="node2_access_log." suffix=".log" pattern="%h %l %u %t "%r" %s %b" /> <Context path="/test" docBase="testapp" reloadable="true"> <Valve className="org.apache.catalina.valves.RemoteAddrValve" deny="192\.168\.150\.139" /> </Context> </Host> 重启catalina - 客户端访问 conf]# curl http://node2:8080 访问node2是正常的 <html> <head> <title>JSP Test Page</title> </head> <body> hello world from /data/webapps </body> </html>
conf]# curl http://node2:8080/test/ 访问test被forbidden
HTTP Status 403 -
type Status report
message
description Access to the specified resource has been forbidden.
Apache Tomcat/7.0.55
- 服务器端日志 [[email protected] logs]# pwd /usr/local/tomcat/logs [[email protected] logs]# ls catalina.2017-04-26.log localhost.2017-04-26.log node2_access_log.2017-04-26.log catalina.out localhost_access_log.2017-04-26.txt host-manager.2017-04-26.log manager.2017-04-26.log logs]# cat node2_access_log.2017-04-26.log 192.168.150.139 - - [26/Apr/2017:15:03:37 +0800] "GET / HTTP/1.1" 200 116 192.168.150.139 - - [26/Apr/2017:15:03:50 +0800] "GET /test/ HTTP/1.1" 403 961
未完待续......
本文出自 “Open World” 博客,请务必保留此出处http://voidyao000.blog.51cto.com/12458042/1919542
以上是关于Tomcat详解的主要内容,如果未能解决你的问题,请参考以下文章