tomcat安装

一、Tomcat安装

1、解压jdk安装包

tar zvxf jdk-8u60-linux-x64.tar.gz 
ln -s /usr/local/jdk1.8.0_60 /usr/local/jdk

2、配置环境变量
vim /etc/profile

添加
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
source /etc/profile

[root@web03 local]# java -version 
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

3、解压tomcat安装包

tar zvxf apache-tomcat-8.0.27.tar.gz
ln -s /usr/local/apache-tomcat-8.0.27 /usr/local/tomcat

4、添加环境变量,增加启动脚本可执行权限

echo 'export TOMCAT_HOME=/application/tomcat'>>/etc/profile
source /etc/profile

chmod +x /usr/local/tomcat/bin/* 

5、启动tomcat

启动程序:/usr/local/tomcat/bin/startup.sh
关闭程序:/usr/local/tomcat/bin/shutdown.sh

6、查看端口号,输入地址端口查看

[root@web03 application]# netstat -tlunp | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      6377/java 

在这里插入图片描述
7、日志文件

[root@web03 logs]# ls
catalina.2019-04-26.log  host-manager.2019-04-26.log  localhost_access_log.2019-04-26.txt
catalina.out             localhost.2019-04-26.log     manager.2019-04-26.log

8、目录介绍

[root@tomcat tomcat]# tree -L 1
.
├── bin #→用以启动、关闭Tomcat或者其它功能的脚本(.bat文件和.sh文件)
├── conf #→用以配置Tomcat的XML及DTD文件
├── lib #→存放web应用能访问的JAR包
├── LICENSE
├── logs #→Catalina和其它Web应用程序的日志文件
├── NOTICE
├── RELEASE-NOTES
├── RUNNING.txt
├── temp #→临时文件
├── webapps #→Web应用程序根目录
└── work #→用以产生有JSP编译出的Servlet的.java和.class文件

10、webapps目录

[root@tomcat tomcat]# cd webapps/
[root@tomcat webapps]# ll
total 20
drwxr-xr-x 14 root root 4096 Oct 5 12:09 docs #→tomcat帮助文档
drwxr-xr-x 6 root root 4096 Oct 5 12:09 examples #→web应用实例
drwxr-xr-x 5 root root 4096 Oct 5 12:09 host-manager #→管理
drwxr-xr-x 5 root root 4096 Oct 5 12:09 manager #→管理
drwxr-xr-x 3 root root 4096 Oct 5 12:09 ROOT #→默认网站根目录

9、tomcat配置文件

[root@tomcat conf]# ll -h /usr/local/tomcat/conf
total 216K
drwxr-xr-x 3 root root 4.0K Jan 26 06:10 Catalina
-rw------- 1 root root 13K Sep 28 16:19 catalina.policy
-rw------- 1 root root 7.0K Sep 28 16:19 catalina.properties
-rw------- 1 root root 1.6K Sep 28 16:19 context.xml
-rw------- 1 root root 3.4K Sep 28 16:19 logging.properties
-rw------- 1 root root 6.4K Sep 28 16:19 server.xml #→主配置文件
-rw------- 1 root root 1.8K Sep 28 16:19 tomcat-users.xml #→Tomcat管理用户配置文件
-rw------- 1 root root 1.9K Sep 28 16:19 tomcat-users.xsd
-rw------- 1 root root 164K Sep 28 16:19 web.xml

10、webapps目录

[root@tomcat tomcat]# cd webapps/
[root@tomcat webapps]# ll
total 20
drwxr-xr-x 14 root root 4096 Oct 5 12:09 docs #→tomcat帮助文档
drwxr-xr-x 6 root root 4096 Oct 5 12:09 examples #→web应用实例
drwxr-xr-x 5 root root 4096 Oct 5 12:09 host-manager #→管理
drwxr-xr-x 5 root root 4096 Oct 5 12:09 manager #→管理
drwxr-xr-x 3 root root 4096 Oct 5 12:09 ROOT #→默认网站根目录

二、Tomcat主配置文件server.xml详解

1、server.xml配置文件示例

[root@web03 conf]# cat 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">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- 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" />
  <!-- 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/TLS 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/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" 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 &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

2、server.xml组件类别

顶级组件:位于整个配置的顶层,如server。
容器类组件:可以包含其它组件的组件,如service、engine、host、context。
连接器组件:连接用户请求至tomcat,如connector。
被嵌套类组件:位于一个容器当中,不能包含其他组件,如Valve、logger。

<server>
<service>
<connector />
<engine>
<host>
<context></context>
</host>
<host>
<context></context>
</host>
</engine>
</service>
</server>

3、组件详解

  • engine:核心容器组件,catalina引擎,负责通过connector接收用户请求,并处理请求,将请求转至对应的虚拟主机host。
  • host:类似于httpd中的虚拟主机,一般而言支持基于FQDN的虚拟主机。
  • context:定义一个应用程序,是一个最内层的容器类组件(不能再嵌套)。配置context的主要目的指定对应对的webapp的根目录,类似于httpd的alias,其还能为webapp指定额外的属性,如部署方式等。
  • connector:接收用户请求,类似于httpd的listen配置监听端口的。
  • service(服务):将connector关联至engine,因此一个service内部可以有多个connector,但只能有一个引擎engine。service内部有两个connector,一个engine。因此,一般情况下一个server内部只有一个service,一个service内部只有一个engine,但一个service内部可以有多个connector。
  • server:表示一个运行于JVM中的tomcat实例。
  • Valve:阀门,拦截请求并在将其转至对应的webapp前进行某种处理操作,可以用于任何容器中,比如记录日志(access log valve)、基于IP做访问控制(remote address filter valve)。
  • logger:日志记录器,用于记录组件内部的状态信息,可以用于除context外的任何容器中。
  • realm:可以用于任意容器类的组件中,关联一个用户认证库,实现认证和授权。可以关联的认证库有两种:UserDatabaseRealm、MemoryRealm和JDBCRealm。
  • UserDatabaseRealm:使用JNDI自定义的用户认证库。
  • MemoryRealm:认证信息定义在tomcat-users.xml中。
  • JDBCRealm:认证信息定义在数据库中,并通过JDBC连接至数据库中查找认证用户。

4、配置文件注释

<?xml version='1.0' encoding='utf-8'?>
<!--
<Server>元素代表整个容器,是Tomcat实例的顶层元素.由org.apache.catalina.Server接口来定义.它包含一个<Service>元素.并且它不能做为任何元素的子元素.
port指定Tomcat监听shutdown命令端口.终止服务器运行时,必须在Tomcat服务器所在的机器上发出shutdown命令.该属性是必须的.
shutdown指定终止Tomcat服务器运行时,发给Tomcat服务器的shutdown监听端口的字符串.该属性必须设置
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<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>
<!--service服务组件-->
<Service name="Catalina">
<!--
connector:接收用户请求,类似于httpd的listen配置监听端口.
port指定服务器端要创建的端口号,并在这个端口监听来自客户端的请求。
address:指定连接器监听的地址,默认为所有地址(即0.0.0.0)
protocol连接器使用的协议,支持HTTP和AJP。AJP(Apache Jserv Protocol)专用于tomcat与apache建立通信的, 在httpd反向代理用户请求至tomcat时使用(可见Nginx反向代理时不可用AJP协议)。
minProcessors服务器启动时创建的处理请求的线程数
maxProcessors最大可以创建的处理请求的线程数
enableLookups如果为true,则可以通过调用request.getRemoteHost()进行DNS查询来得到远程客户端的实际主机名,若为false则不进行DNS查询,而是返回其ip地址
redirectPort指定服务器正在处理http请求时收到了一个SSL传输请求后重定向的端口号
acceptCount指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理
connectionTimeout指定超时的时间数(以毫秒为单位)
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<!--engine,核心容器组件,catalina引擎,负责通过connector接收用户请求,并处理请求,将请求转至对应的虚拟主机host
defaultHost指定缺省的处理请求的主机名,它至少与其中的一个host元素的name属性值是一样的
-->
<Engine name="Catalina" defaultHost="localhost">
<!--Realm表示存放用户名,密码及role的数据库-->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<!--
host表示一个虚拟主机
name指定主机名
appBase应用程序基本目录,即存放应用程序的目录.一般为appBase="webapps" ,相对于CATALINA_HOME而言的,也可以写绝对路径。
unpackWARs如果为true,则tomcat会自动将WAR文件解压,否则不解压,直接从WAR文件中运行应用程序
autoDeploy:在tomcat启动时,是否自动部署。
xmlValidation:是否启动xml的校验功能,一般xmlValidation="false"。
xmlNamespaceAware:检测名称空间,一般xmlNamespaceAware="false"。
-->
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!--
Context表示一个web应用程序,通常为WAR文件
docBase应用程序的路径或者是WAR文件存放的路径,也可以使用相对路径,起始路径为此Context所属Host中appBase定义的路径。
path表示此web应用程序的url的前缀,这样请求的url为http://localhost:8080/path/****
reloadable这个属性非常重要,如果为true,则tomcat会自动检测应用程序的/WEB-INF/lib 和/WEB-INF/classes目录的变化,自动装载新的应用程序,可以在不重启tomcat的情况下改变应用程序
-->
<Context path="" docBase="" debug=""/>

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>
</Engine>
</Service>
</Server>

三、WEB站点部署

上线的代码有两种方式,第一种方式是直接将程序目录放在webapps目录下面。第二种方式是使用开发工具将程序打包成war包,然后上传到webapps目录下面。

1 使用war包部署web站点,在webapps目录下上传 memtest.war文件,一开始还没有自动解压。
然后在浏览器上访问http://10.0.0.9:8080/memtest/meminfo.jsp后war包自动解压了

[root@web03 webapps]# pwd
/usr/local/tomcat/webapps
[root@web03 webapps]# rz            #上传 memtest.war
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring memtest.war...
  100%     643 bytes  643 bytes/sec 00:00:01       0 Errors  

[root@web03 webapps]# ls
docs  examples  host-manager  manager  memtest.war  ROOT
[root@web03 webapps]# ls
docs  examples  host-manager  manager  memtest  memtest.war  ROOT

在这里插入图片描述
在这里插入图片描述
2、自定义默认网站目录
上面访问的网址为http://10.0.0.9:8080/memtest/meminfo.jsp
现在我想访问格式为http://10.0.0.9:8080/meminfo.jsp

方法一
将meminfo.jsp或其他程序放在tomcat/webapps/ROOT目录下即可。因为默认网站根目录为tomcat/webapps/ROOT

方法二

[root@tomcat ~]# vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/application/tomcat/webapps/memtest" debug="0" reloadable="false" crossContext="true"/>

<!--
Context表示一个web应用程序,通常为WAR文件
docBase应用程序的路径或者是WAR文件存放的路径,也可以使用相对路径,起始路径为此Context所属Host中appBase定义的路径。
path表示此web应用程序的url的前缀,这样请求的url为http://localhost:8080/path/****
reloadable这个属性非常重要,如果为true,则tomcat会自动检测应用程序的/WEB-INF/lib 和/WEB-INF/classes目录的变化,自动装载新的应用程序,可以在不重启tomcat的情况下改变应用程序
-->

[root@tomcat ~]# /application/tomcat/bin/shutdown.sh
[root@tomcat ~]# /application/tomcat/bin/startup.sh

参考:https://blog.oldboyedu.com/java-tomcat/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值