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目录
[root@localhost ~]# which java  
/usr/bin/java
[root@localhost ~]# ls -l /usr/bin/java 
lrwxrwxrwx 1 root root 22 4月  26 09:46 /usr/bin/java -> /etc/alternatives/java
[root@localhost ~]# 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/tomcat@.service
/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)) 
Tomcat详解

(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))  

Tomcat详解
目录文件查看:
~]# cd /usr/local/tomcat/
tomcat]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
tomcat]# cd webapps/
[root@localhost 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
[root@localhost 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默认为热部署,所以网页已经可以正常访问
tomcat会默认把jsp网页编译成java语言
~]# cd /usr/local/tomcat/work/Catalina/localhost/myapp/org/apache/jsp/
jsp]# ls
index_jsp.class  index_jsp.java

(2)tomcat自带的应用程序

Tomcat详解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))

Tomcat详解Tomcat详解Tomcat详解Tomcat详解

管理页面可以对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

此时网站已经可以正常访问

Tomcat详解

配置后台数据库
~]# 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 shopxx@'localhost' IDENTIFIED BY 'shopxx';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> GRANT ALL ON shopxx.* to shopxx@'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

Tomcat详解Tomcat详解Tomcat详解

安装提示删除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

Tomcat详解

此时网站部署完成

Tomcat详解

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>

[root@localhost ~]# 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属性即可,点击按钮也会出现提示画面

Tomcat详解Tomcat详解

修改内容为:
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

Tomcat详解

此时在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
[root@localhost conf]# curl http://node2:8080


<html>
<head>
    <title>JSP Test Page</title>
</head>
<body>
    hello world from /data/webapps

</body>
</html>

访问Conext目录
[root@localhost 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 &quot;%r&quot; %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
<html><head><title>Apache Tomcat/7.0.55 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans
-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 403 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>Access to the specified resource has been forbidden.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.55</h3></body></html> 

- 服务器端日志
[root@localhost logs]# pwd
/usr/local/tomcat/logs
[root@localhost 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

4、LNMT/LAMT

LNMT:

Tomcat详解

- nginx安装:

~]# yum -y install nginx

- 修改nginx配置文件
~]# cd /etc/nginx/
nginx]# vim nginx.conf
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  localhost;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

#修改location /进行反代,由于我测试时nginx和tomcat是在同一台server上,所以使用localhost
    location / {
        proxy_pass http://localhost:8080/;
    }

- 验证配置文件并启动nginx
nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx]# systemctl start nginx.service
nginx]# ss -tnl
State      Recv-Q Send-Q        Local Address:Port                       Peer Address:Port              
LISTEN     0      50                        *:3306                                  *:*                  
LISTEN     0      128                       *:80                                    *:*                  
LISTEN     0      128                       *:22                                    *:*                  
LISTEN     0      100               127.0.0.1:25                                    *:*                  
LISTEN     0      100                      :::8009                                 :::*                  
LISTEN     0      128                      :::80                                   :::*                  
LISTEN     0      100                      :::8080                                 :::*                  
LISTEN     0      128                      :::22                                   :::*                  
LISTEN     0      100                     ::1:25                                   :::*                  
LISTEN     0      1          ::ffff:127.0.0.1:8005                                 :::*

此时进行访问测试,我访问http时已经不需要指定8080端口,使用默认端口即可访问我后端的tomcat

Tomcat详解

对HOST主机进行反代

- 服务器端
服务器端本地做好node的解析
~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.150.138 node2
~]# vim /etc/nginx/nginx.conf
location / {
       proxy_pass http://node2:8080/;
    }

修改完配置文件后验证并reload nginx服务
~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
~]# systemctl reload nginx.service


- 客户端访问测试,无需在添加8080端口号
~]# curl http://192.168.150.138


<html>
    <head>
        <title>JSP Test Page</title>
    </head>
    <body>
        hello world from /data/webapps

    </body>
</html>


动静分离测试:
- 服务器端:
~]# vim /etc/nginx/nginx.conf
修改nginx配置文件中的location
        location / {
    }
#如果用户访问jsp资源再进行反代,静态资源nginx自己解析
    location ~* \.(jsp|do)$ {
        proxy_pass http://node2:8080;
    }

nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx]# systemctl reload nginx.service

提供一个静态资源给nginx
~]# mv 18.png /usr/share/nginx/html/

访问静态资源,会自动去nginx资源目录:

Tomcat详解

访问动态jsp资源,会自动去访问tomcat资源目录:

Tomcat详解

LAMT:

Tomcat详解

使用httpd的reverse_proxy_module模块反代至tomcat

1、proxy_module, proxy_http_module

- httpd的安装
yum -y install httpd

- 修改httpd配置文档
注释掉主配置文档中的DocumentRoot
~]# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
创建http_tomcat.conf的子配置文档
~]# vim /etc/httpd/conf.d/http_tomcat.conf
<VirtualHost *:80>
ServerName tc1.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
    Require all granted
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
<Location />
    Require all granted
</Location>
</VirtualHost>

- 验证配置并启动httpd
~]# httpd -t
Syntax OK
~]# systemctl start httpd.service

访问此服务所有的网页均反代至tomcat 8080

Tomcat详解Tomcat详解

2、proxy_module, proxy_ajp_module
httpd反代至tomcat的ajp 8009端口
~]# cd /etc/httpd/conf.d/
conf.d]# ls
autoindex.conf  http_tomcat.conf  README  userdir.conf  welcome.conf
conf.d]# cp http_tomcat.conf ajp_tomcat.conf
conf.d]# mv http_tomcat.conf http_tomcat.conf.bak
conf.d]# vim ajp_tomcat.conf 
<VirtualHost *:80>
    ServerName tc1.com
    ProxyRequests Off
    ProxyVia On
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / ajp://localhost:8009/
    ProxyPassReverse / ajp://localhost:8009/
    <Location />
        Require all granted
    </Location>
</VirtualHost>

- 验证配置并启动httpd
~]# httpd -t
Syntax OK
~]# systemctl restart httpd.service

访问此服务所有的网页均反代至tomcat 8009

Tomcat详解Tomcat详解

5、tomcat以普通用户执行&服务脚本定制

出于安全考虑,tomcat一般均在普通用户下执行,下面进行在普通用户下执行tomcat测试

-tomcat用户创建,并授予权限
catalina.sh stop    修改前先关闭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
~]# cd /usr/local/tomcat/
tomcat]# ll
总用量 104
drwxr-xr-x 2 root root  4096 4月  26 10:34 bin
drwxr-xr-x 3 root root  4096 4月  26 16:15 conf
drwxr-xr-x 2 root root  4096 4月  26 10:34 lib
-rw-r--r-- 1 root root 56812 7月  18 2014 LICENSE
drwxr-xr-x 2 root root  4096 4月  27 10:28 logs
-rw-r--r-- 1 root root  1192 7月  18 2014 NOTICE
-rw-r--r-- 1 root root  8963 7月  18 2014 RELEASE-NOTES
-rw-r--r-- 1 root root 16204 7月  18 2014 RUNNING.txt
drwxr-xr-x 3 root root    58 4月  26 13:21 temp
drwxr-xr-x 9 root root   101 4月  26 13:10 webapps
drwxr-xr-x 3 root root    21 4月  26 10:40 work
tomcat]# useradd tomcat
tomcat]# chown -R tomcat.tomcat ./*
tomcat]# ll
总用量 104
drwxr-xr-x 2 tomcat tomcat  4096 4月  26 10:34 bin
drwxr-xr-x 3 tomcat tomcat  4096 4月  26 16:15 conf
drwxr-xr-x 2 tomcat tomcat  4096 4月  26 10:34 lib
-rw-r--r-- 1 tomcat tomcat 56812 7月  18 2014 LICENSE
drwxr-xr-x 2 tomcat tomcat  4096 4月  27 10:28 logs
-rw-r--r-- 1 tomcat tomcat  1192 7月  18 2014 NOTICE
-rw-r--r-- 1 tomcat tomcat  8963 7月  18 2014 RELEASE-NOTES
-rw-r--r-- 1 tomcat tomcat 16204 7月  18 2014 RUNNING.txt
drwxr-xr-x 3 tomcat tomcat    58 4月  26 13:21 temp
drwxr-xr-x 9 tomcat tomcat   101 4月  26 13:10 webapps
drwxr-xr-x 3 tomcat tomcat    21 4月  26 10:40 work

- 切换至tomcat用户进行环境变量查看
[root@localhost tomcat]# su - tomcat
[tomcat@localhost ~]$ echo $JAVA_HOME
/usr/java/latest
[tomcat@localhost ~]$ echo $CATALINA_BASE
/usr/local/tomcat

- 开启tomcat
[tomcat@localhost ~]$ 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.

进程均由tomcat用户在执行
[tomcat@localhost ~]$ ps aux | grep java
tomcat    19643 12.2  5.8 1081152 59028 pts/2   Sl   13:45   0:04 /usr/java/latest/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apa
che.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap starttomcat    19660  0.0  0.0 112660   960 pts/2    R+   13:46   0:00 grep --color=auto java

- catalina服务脚本制定
[root@localhost tomcat]# vim /etc/rc.d/init.d/tomcat

#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig:2345 96 14
# description:The Apache Tomcat servlet/JSP continer.
# JAVA_OPTS='-Xms64m -Xmx128m'
JAVA_HOME=/usr/java/latest
CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME CATALINA_HOME

case $1 in
start)
  exec $CATALINA_HOME/bin/catalina.sh start ;;
stop)
  exec $CATALINA_HOME/bin/catalina.sh stop;;
restart)
  $CATALINA_HOME/bin/catalina.sh stop
  sleep 2
  exec $CATALINA_HOME/bin/catalina.sh start ;;
*)
  echo "Usage: `basename $0` {start|stop|restart}"
  exit 1
  ;;
esac

添加脚本执行权限并将脚本添加至chkconfig
[root@localhost tomcat]# vim /etc/rc.d/init.d/tomcat
[root@localhost tomcat]# chmod +x /etc/init.d/tomcat 
[root@localhost tomcat]# chkconfig --add tomcat

原创文章,作者:N23-苏州-void,如若转载,请注明出处:http://www.178linux.com/74334

(0)
N23-苏州-voidN23-苏州-void
上一篇 2017-04-27
下一篇 2017-04-27

相关推荐

  • 简明 Vim 练级攻略

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的。下面的文章翻译自《Learn Vim Progressively》,我觉得这是给新手最好的VIM的升级教程了,没有列举所有的命令,只是列举了那些最有用的命令。非常不错。 ——————————正文开始—————————— …

    Linux干货 2015-04-04
  • Linux文件和目录管理类命令初识

    简述 在Linux操作系统使用过程中,因为运维需要频繁的对文件、目录进行操作,所以需要学习一些关于文件、目录的命令 Linux上文件是什么? 「文件」:存储空间存储的一段流式数据,可以按名「存取」这些数据 常见文件类型 -:常规文件d:目录文件b:块设备文件,以「block」为单位进行随机访问,常见设备「硬盘」c:字符设备文件,以「character」为单位…

    Linux干货 2016-11-06
  • vim的简单应用

    vim的简单应用         之前我们学习过Linux的思想,其中有一条就是一切皆文本,所以在这里大部分配置文件都是文本模式存在的,那么使用简单的文字编辑器就可以修改配置了,之前我们学习过nano文本编辑器,但是Vim会比nano使用的更为方便,也更强大。 基本现在所有的Li…

    2017-06-17
  • 第5周

    1, ~]# grep "^root\>" /etc/passwd | cut -d: -f7 4,~]# ifconfig | grep "\<[0-9][0-9][0-9]\>" 7, ~]# find /var -user root -group mail 8,~]# fin…

    Linux干货 2016-09-15
  • 网络N23期第一周(计算机的组成及功能及Linux的发行版等)

    一. 描述计算机的组成及功能 计算机由五大硬件组成:” 运算器 + 控制器 + 储存器 + 输入设备 + 输出设备” 。 运算器(Datapath):运算器的功能是对数据进行各种算术运算和逻辑运算,即对数据进行加工处理。 控制器(Control):是整个计算机的中枢神经,其功能是对程序规定的控制信息进行解释,根据其要求进行控制,调度程序、数据、地址,协调计算…

    Linux干货 2016-09-22
  • MySQL主从复制: MHA

                    MySQL主从复制: MHA 前言 MHA的架构 环境部署 实验步骤 总结 前言 上篇文章我们实现了MySQL的主从复制, 但是我们之前就说过, 主从复制是有很多问题的…

    Linux干货 2016-04-28

评论列表(1条)

  • 马哥教育
    马哥教育 2017-05-04 14:49

    非常非常详细的一篇关于tomcat的系统文档,相信一定会帮到很多人。