Tomcat

Tomcat简介

是基于apache软件基金会的jakarta项目中的一个核心项目
tomcat是一个server和jsp容器,主要处理动态HTML,处理静态HTML能力不如nginx/apache服务器

tomcat安装

  1. 安装java
[root@test01 ~]# yum install java-1.8.0
[root@test01 ~]# java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)
  1. 下载tomcat
    在这里插入图片描述

  2. 安装tomcat

[root@test01 ~]# mkdir /data/soft
[root@test01 ~]# cd /data/soft/
[root@test01 soft]# ll
total 10336
-rw-r--r--. 1 root root 10581990 Nov 24 06:36 apache-tomcat-8.5.73.tar.gz
[root@test01 soft]# tar -xzvf apache-tomcat-8.5.73.tar.gz -C /opt/
[root@test01 opt]# ln -sf apache-tomcat-8.5.73 tomcat
[root@test01 opt]# ll
total 0
drwxr-xr-x. 9 root root 220 Nov 24 06:38 apache-tomcat-8.5.73
lrwxrwxrwx. 1 root root  20 Nov 24 06:39 tomcat -> apache-tomcat-8.5.73
#查看目录结构
[root@test01 opt]# tree -L 1 tomcat
tomcat
├── bin                #启动关闭tomcat的脚本
├── BUILDING.txt
├── conf               #配置tomcat的配置文件
├── CONTRIBUTING.md
├── lib                #存放web应用能访问的jar包
├── LICENSE
├── logs               #tomcat日志
├── NOTICE
├── README.md
├── RELEASE-NOTES
├── RUNNING.txt
├── temp
├── webapps         #类似nginx的站点目录
└── work            #用以产生有jsp编译出的servlet的.java和.class文件
#webapps目录
[root@test01 opt]# cd tomcat/webapps/
[root@test01 webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@test01 webapps]# ll
total 4
drwxr-x---. 15 root root 4096 Nov 24 06:38 docs     #说明文件
drwxr-x---.  7 root root   99 Nov 24 06:38 examples  #示例页面
drwxr-x---.  6 root root   79 Nov 24 06:38 host-manager  #管理页面
drwxr-x---.  6 root root  114 Nov 24 06:38 manager    #管理页面
drwxr-x---.  3 root root  223 Nov 24 06:38 ROOT        #默认网站的根目录
#bin目录
[root@test01 webapps]# cd ../bin/
[root@test01 bin]# ll
total 876
-rw-r-----. 1 root root  36412 Nov 11 21:14 bootstrap.jar
-rw-r-----. 1 root root  16840 Nov 11 21:14 catalina.bat
-rwxr-x---. 1 root root  25294 Nov 11 21:14 catalina.sh   #核心管理脚本,以后jvm优化参数以及相关配置,修改tomcat启动参数
-rw-r-----. 1 root root   1664 Nov 11 21:14 catalina-tasks.xml
-rw-r-----. 1 root root   2123 Nov 11 21:14 ciphers.bat
-rwxr-x---. 1 root root   1997 Nov 11 21:14 ciphers.sh
-rw-r-----. 1 root root  25357 Nov 11 21:14 commons-daemon.jar
-rw-r-----. 1 root root 207420 Nov 11 21:14 commons-daemon-native.tar.gz
-rw-r-----. 1 root root   2040 Nov 11 21:14 configtest.bat
-rwxr-x---. 1 root root   1922 Nov 11 21:14 configtest.sh
-rwxr-x---. 1 root root   9100 Nov 11 21:14 daemon.sh
-rw-r-----. 1 root root   2091 Nov 11 21:14 digest.bat
-rwxr-x---. 1 root root   1965 Nov 11 21:14 digest.sh
-rw-r-----. 1 root root   3460 Nov 11 21:14 setclasspath.bat
-rwxr-x---. 1 root root   3708 Nov 11 21:14 setclasspath.sh
-rw-r-----. 1 root root   2020 Nov 11 21:14 shutdown.bat
-rwxr-x---. 1 root root   1902 Nov 11 21:14 shutdown.sh  #停止tomcat
-rw-r-----. 1 root root   2022 Nov 11 21:14 startup.bat  
-rwxr-x---. 1 root root   1904 Nov 11 21:14 startup.sh   #启动tomcat
-rw-r-----. 1 root root  51182 Nov 11 21:14 tomcat-juli.jar
-rw-r-----. 1 root root 428057 Nov 11 21:14 tomcat-native.tar.gz
-rw-r-----. 1 root root   4574 Nov 11 21:14 tool-wrapper.bat
-rwxr-x---. 1 root root   5540 Nov 11 21:14 tool-wrapper.sh
-rw-r-----. 1 root root   2026 Nov 11 21:14 version.bat
-rwxr-x---. 1 root root   1908 Nov 11 21:14 version.sh
  1. 启动tomcat
[root@test01 bin]# sh startup.sh 
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

测试访问
在这里插入图片描述

tomcat配置文件介绍

  • server.xml
[root@test01 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">         #开启8005端口作用的关闭tomcat,telnet连接tomcat输入SHUTDOWN就可以关闭你的tomcat
  <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
         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 an SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation. The default
         SSLImplementation will depend on the presence of the APR/native
         library and the useOpenSSL attribute of the
         AprLifecycleListener.
         Either JSSE or OpenSSL style configuration may be used regardless of
         the SSLImplementation selected. JSSE style configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
         This connector uses the APR/native implementation which always uses
         OpenSSL for TLS.
         Either JSSE or OpenSSL style configuration may be used. OpenSSL style
         configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
               maxThreads="150" SSLEnabled="true" >
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
            <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
                         certificateFile="conf/localhost-rsa-cert.pem"
                         certificateChainFile="conf/localhost-rsa-chain.pem"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <!--
    <Connector protocol="AJP/1.3"
               address="::1"
               port="8009"
               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">   #引擎,defaultHost可以配置域名

      <!--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"   #相当于nginx的server标签,appBase相当于站点根目录,尽量把webapps下的东西都移走
            unpackWARs="true" autoDeploy="true">  #unpackWARs和autoDeploy自动解压jar包和自动上线

        <!-- 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>

部署一个简单的界面

  1. 将war包放入/opt/tomcat/webapps目录下
[root@test01 webapps]# pwd
/opt/tomcat/webapps
[root@test01 webapps]# ls
memtest.war  ROOT
[root@test01 webapps]# ll
total 4
-rw-r--r--. 1 root root 643 Nov 24 21:58 memtest.war
  1. memtest.war包会被tomcat自动解压和部署
[root@test01 webapps]# ll
total 4
drwxr-x---. 3 root root  41 Nov 24 21:58 memtest
-rw-r--r--. 1 root root 643 Nov 24 21:58 memtest.war
drwxr-x---. 2 root root   6 Nov 24 21:49 ROOT
  1. 访问测试
    在这里插入图片描述

部署jpress

  1. 将jpress-web-newest.war包放入/opt/tomcat/webapps下就会自动解压部署
[root@test01 webapps]# mv jpress-web-newest.war jpress.war
[root@test01 webapps]# ll
total 20312
drwxr-x---. 7 root root      102 Nov 24 22:07 jpress
-rw-r--r--. 1 root root 20797013 Nov 24 22:05 jpress.war
drwxr-x---. 2 root root        6 Nov 24 21:49 ROOT
  1. 打开网页输入http://192.168.184.128:8080/jpress
    在这里插入图片描述
    在这里插入图片描述
  2. 在数据库里创建库和用户
[root@test01 webapps]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database jpress DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.10 sec)

MariaDB [(none)]> grant all on jpress.* to jpress@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit
Bye

在这里插入图片描述

  1. 继续安装
    在这里插入图片描述
  2. 成功进入
    在这里插入图片描述
  3. 输入http://192.168.184.128:8080/jpress/admin进入管理页面
    在这里插入图片描述
  4. 写文章测试
    在这里插入图片描述
  5. 查看文章中的图片位置
[root@test01 20211124]# ll
total 104
-rw-r-----. 1 root root 14526 Nov 24 22:22 b6a4c507cc3c4a92920821e1344f6473_780x240.jpg
-rw-r-----. 1 root root 88117 Nov 24 22:22 b6a4c507cc3c4a92920821e1344f6473.jpg
[root@test01 20211124]# pwd
/opt/tomcat/webapps/jpress/attachment/20211124
  1. 在数据库中查看上传的文章
[root@test01 20211124]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 111
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> select * from  jpress.jpress_content\G
*************************** 1. row ***************************
              id: 1
           title: test
            text: <p>test<img src="/jpress/attachment/20211124/b6a4c507cc3c4a92920821e1344f6473.jpg" alt="" width="1000" height="600"></p>
         summary: NULL
         link_to: NULL
 markdown_enable: 0
       thumbnail: NULL
          module: article
           style: NULL
         user_id: 1
          author: NULL
      user_email: NULL
         user_ip: NULL
      user_agent: NULL
       parent_id: NULL
       object_id: NULL
    order_number: 0
          status: normal
         vote_up: 0
       vote_down: 0
            rate: NULL
      rate_count: 0
           price: 0.00
  comment_status: NULL
   comment_count: 0
    comment_time: NULL
      view_count: 0
         created: 2021-11-24 22:22:41
        modified: 2021-11-24 22:22:41
            slug: test
            flag: NULL
             lng: NULL
             lat: NULL
   meta_keywords: NULL
meta_description: NULL
         remarks: NULL
1 row in set (0.00 sec)

nginx反向代理tomcat

tomcat多实例

  1. 复制两个tomcat
[root@test01 20211124]# cd /opt/
[root@test01 opt]# ls
apache-tomcat-8.5.73  tomcat
[root@test01 opt]# ll
total 0
drwxr-xr-x. 9 root root 220 Nov 24 06:38 apache-tomcat-8.5.73
lrwxrwxrwx. 1 root root  20 Nov 24 06:39 tomcat -> apache-tomcat-8.5.73
[root@test01 opt]# cp -a apache-tomcat-8.5.73 tomcat_01
[root@test01 opt]# cp -a apache-tomcat-8.5.73 tomcat_02
  1. 修改配置文件
[root@test01 opt]# vim tomcat_01/conf/server.xml 
<Server port="8006" shutdown="SHUTDOWN">
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
[root@test01 opt]# vim tomcat_01/conf/server.xml 
<Server port="8007" shutdown="SHUTDOWN">
    <Connector port="8082" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
  1. 启动并查看
[root@test01 bin]# ps aux|grep java
root       3202  0.8  8.1 2552152 152392 pts/0  Sl   22:17   0:11 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat -Dcatalina.home=/opt/tomcat -Djava.io.tmpdir=/opt/tomcat/temp org.apache.catalina.startup.Bootstrap start
root       3336 13.4  5.6 2520120 105824 pts/0  Sl   22:40   0:05 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat_01/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /opt/tomcat_01/bin/bootstrap.jar:/opt/tomcat_01/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat_01 -Dcatalina.home=/opt/tomcat_01 -Djava.io.tmpdir=/opt/tomcat_01/temp org.apache.catalina.startup.Bootstrap start
root       3405 88.0  5.8 2521160 109556 pts/0  Sl   22:40   0:06 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat_02/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /opt/tomcat_02/bin/bootstrap.jar:/opt/tomcat_02/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat_02 -Dcatalina.home=/opt/tomcat_02 -Djava.io.tmpdir=/opt/tomcat_02/temp org.apache.catalina.startup.Bootstrap start
root       3456  0.0  0.0 112704   980 pts/0    R+   22:40   0:00 grep --color=auto java
[root@test01 bin]# netstat -anp|grep 808
tcp6       0      0 :::8080                 :::*                    LISTEN      3202/java           
tcp6       0      0 :::8081                 :::*                    LISTEN      3336/java           
tcp6       0      0 :::8082                 :::*                    LISTEN      3405/java  

nginx反向代理

  1. 配置一个nginx文件
[root@test01 conf.d]# cat tomcat.conf 
upstream java {
    server 192.168.184.128:8081;
    server 192.168.184.128:8082;
}
server {
    listen       80;
    server_name  www.xcnnnnnn.com;
    root   html;
    index  index.html index.htm;
    location / {
        proxy_pass http://java;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

2.重启nginx测试访问
在这里插入图片描述
2. 查看日志,两边都有访问
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值