Tomcat deployment optimization and virtual host configuration

One, Tomcat introduction

  • Is a free, open source web application server
  • Apache Software Foundation (Apache Software Foundation), a core project in the Jakarta project
  • Developed by Apache, Sun, and some companies and individuals
  • Loved by Java enthusiasts and recognized by some software developers
  • Currently more popular web application server

1.1 The core components of Tomcat

  • web container: web server
  • Servlet container: named catalina, processing Servlet code
  • JSP container: Translate JSP dynamic web pages into Servlet web pages

1.2 Tomcat's request processing process

  • 1. The user enters the URL localhost:8080/test/index.jsp in the browser, and the request is sent to the local port 8080, which is obtained by the Coyote HTTP/1.1 Connector listening there;
  • 2. The Connector passes the request to the Engine (Container) of the Service where it is located, and waits for the Engine's response;
  • 3. Engine gets the request localhost/test/index.jsp, matching all virtual hosts Host;
  • 4. Engine matches to the Host named localhost (even if it fails to match, the request will be handed over to the Host, because the Host is defined as the default host of the Engine), and the Host named localhost gets the request /test/index.jsp , Match all Context it owns. Host matches the Context whose path is /test (if it fails to match, hand the request to the Context whose path name is "" for processing);
  • 5. Context with path="/test" obtains the request /index.jsp, and finds the corresponding Servlet in its mapping table. Context matches the Servlet whose URL Pattern is *.jsp, which corresponds to the JspServlet class;
  • 6. Construct HttpServletRequest object and HttpServletResponse object, call doGet() or doPost() of JspServlet as parameters, execute business logic, data storage, etc.;
  • 7. Context returns the HttpServletResponse object after execution to Host;
  • 8. Host returns the HttpServletResponse object to Engine;
  • 9.Engine returns the HttpServletResponse object to the Connector;
  • 10.Connector returns the HttpServletResponse object to the client Browser

1.2.1 Noun analysis during processing

  • connector: Connector
    A Connector component will listen to client requests on a specified port, receive the tcp connection request sent by the browser, create a Request and a Response object to exchange data with you, and then will Create a thread to process the request and pass the generated Request and Response objects to the Engine, get the response from the Engine and return it to the client. Tomcat has two classic Connectors, one directly listens to HTTP requests from the browser, and the other listens to requests from other WebServers. Cotote HTTP/1.1 Connector listens to HTTP requests from client browsers at port 8080, and Coyote JK2 Connector listens to Servlet/JSP requests from other WebServers at port 8009. The most important function of the Connector is to receive connection requests and then allocate threads for the Container to process the request, so this must be multi-threaded, and multi-threaded processing is the core of the connector design.


  • Container : Container is the parent interface of the container. The design of the container uses a typical responsibility chain design pattern. It consists of four self-container components, namely Engine, Host, Context, and Wrapper. These four components are responsible, and there is a containment relationship. Usually a Servlet class corresponds to a Wrapper. If there are multiple Servlets, define multiple Wrappers. If there are multiple Wrappers, define a higher Container, such as Context. Context is defined in the parent container Host, where Host is not necessary, but to run the war program, you must host, because there must be a web.xml file in the war, and the resolution of this file requires Host. If there are multiple Hosts It is necessary to define a top container Engine. Engine has no parent container anymore, and an Engine represents a complete Servlet engine.

  • engine: engine

  • The Engine container is relatively simple, it only defines some basic association relationships Host container

  • host: Virtual host
    Host is the word container of Engine. A Host represents a virtual host in Engine. The function of this virtual host is to run multiple applications. It is responsible for installing and deploying these applications, and identifying the applications so that they can be distinguished. Its child container is usually Context. In addition to associating child containers, it also stores information that a host should have

  • Context: The
    Context of the front page of the JSP represents the Context of the Servlet. It has the basic environment for the Servlet to run. In theory, the Servlet can be run as long as there is a Context. Simple Tomcat can have no Engine and Host. The most important function of Context is to manage the Servlet instances in it. Servlet instances appear as Wrapper in Context. Another point is how can Context find the correct Servlet to execute it? Tomcat5 was previously managed by a Mapper class. After Tomcat5, this function has been moved to request. In the previous sequence diagram, you can find that obtaining sub-containers is allocated through request.

  • servlet: processing code

1.3 Introduction to JDK

  • JDK is a software development kit for Java language
  • JDK is a necessary environment for Tomcat to run
  • According to the computer hardware configuration, select the installation package

1.4 Tomcat file introduction

Insert picture description here

Two, install Tomcat

2.1 Preparation

  • Prepare JDK compressed package jdk-8u201-linux-x64.rpm
  • Prepare the Tomcat installation package apache-tomcat-9.0.16.tar
[root@localhost ~]# rpm -ivh  jdk-8u201-linux-x64.rpm  ## 安装JDK

2.2 Set JDK environment variables

root@promote ~]# vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
[root@promote ~]# source /etc/profile  ## 重新加载全局环境变量文件  使刚刚的修改生效

[root@promote ~]#  java -version ## 查看版本
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

2.3 Install and optimize Tomcat

[root@promote ~]# tar zxvf apache-tomcat-9.0.16.tar.gz 
[root@promote ~]# mv  apache-tomcat-9.0.16  /usr/local/
[root@promote ~]# cd /usr/local/
[root@localhost ~]# mv  apache-tomcat-9.0.16  /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv apache-tomcat-9.0.16/  tomcat 
[root@localhost bin]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/bin/ ## 建立关闭服务脚本软连接到/usr/bin/里面
[root@localhost bin]#  ln -s /usr/local/tomcat/bin/startup.sh /usr/bin/ ## 建立开启服务脚本软连接到/usr/bin/里面

2.4 Start Tomcat

[root@localhost bin]# iptables -F
[root@localhost bin]# setenforce 0
[root@localhost bin]# startup.sh         ## 开启服务
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

2.5 Visit website

  • Visit http://192.168.233.127:8080/ ## The default port of tomcat is 8080. You must add it. If you do not add the default port 80, you will not be able to visit the website.
    Insert picture description here

Three, Tomcat optimization

3.1 Optimization of startup speed

  • The first time you start to check the log, you will find that Tomcat starts very slowly, by default it takes tens of seconds, you can modify the jdk parameters to optimize
[root@promote bin]# vim /usr/java/jdk1.8.0_201-amd64/jre/lib/security/java.security 
securerandom.source=file:/dev/urandom

3.2 Optimization of other parameters

<Connector port="8080" protocol="HTTP/11.1" 
connectionTimeout="20000" 
redirectPort="8443" 
minSpareThreads="50" 
enableLookups="false" 
disableUploadTimeout="true" 
acceptCount="300" 
maxThreads="500" 
processorCache="500"
URIEncoding="UTF-8" 
compression="on" 
compressionMinSize="2048" 
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image /jpg,image/png"/>
  • Common optimization parameters
    1. maxThreads: Tomcat uses threads to process each request received. This value represents the maximum number of threads that Tomcat can create. The default value is 200.
    2. minSpareThreads: The minimum number of idle threads, the number of initialization threads when Tomcat starts, which means that there are so many empty threads waiting even if no one is using it. The default value is
    10.
    3. maxSpareThreads: The maximum number of spare threads. Once the created threads exceed this value, Tomcat will close socket
    threads that are no longer needed . The default value is -1 (unlimited) and generally does not need to be specified.
    4. URIEncoding: Specify the URL encoding format of the Tomcat container. The Tomcat language encoding format is not as
    easy to configure as other Web server software and needs to be specified separately.
    5. connnectionTimeout: network connection timeout, unit: milliseconds, set to 0 means never time out, this setting has hidden dangers. Usually the default is 20000
    milliseconds.
    6. enableLookups: Whether to reverse check the domain name to return the host name of the remote host, the value is: true or false, if it is set to false, it will directly return the
    IP address. In order to improve the processing capacity, it should be set to false.
    7. disableUploadTimeout: Whether to use the timeout mechanism when uploading. Should be set to true.
    8. connectionUploadTimeout: upload timeout. After all, file upload may take more time. This parameter needs to be adjusted according to your own business needs, so that the Servlet has a longer time to complete its execution. It needs to be combined with the previous parameter It will take effect when used together.
    9. AcceptCount: Specify the
    maximum queue length of incoming connection requests when all available threads for processing requests are used. Requests exceeding this number will not be processed. The default is 100.
    10. Compression: Whether to perform GZIP compression on the response data, off means compression is forbidden, on means compression is allowed (the text will be compressed), force
    means compression is performed in all cases, the default value is off. After compressing the data, the page size can be effectively reduced, generally by about 1/3, thus saving bandwidth.
    11. CompressionMinSize: Represents the minimum value of the compressed response. The message will be compressed only when the response message size is greater than this value. If the compression function is enabled, the default value is 2048.
    12. compressableMimeType: compression type, which specifies which types of files are compressed.
    noCompressionUserAgents="gozilla, traviata": For the following browsers, compression is not enabled. If the code has been separated from dynamic and static, data such as static pages and images do not need to be processed by Tomcat, so there is no need to configure compression in Tomcat. Because there is only one Tomcat server, and the test is the Tomcat homepage, there will be pictures and static resource files, so compression is enabled here.

Fourth, the configuration of the virtual host

4.1 Tomcat configuration file modification

 [root@localhost ~]#  vim /usr/local/tomcat/conf/server.xml 
 <Host name="www.test01.com"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
         <Context docBase="/usr/local/tomcat/webapps/test01" path="" reloadable="true"/>
      </Host>
      <Host name="www.test02.com"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
         <Context docBase="/usr/local/tomcat/webapps/test02" path="" reloadable="true"/>
      </Host>

4.2 Tomcat site settings

[root@localhost ~]# cd /usr/local/tomcat/webapps/
[root@localhost webapps]# mkdir test01
[root@localhost webapps]# mkdir test02
[root@localhost webapps]# vim test01/index.jsp
<h1> this is test01 </h1>
[root@localhost webapps]# vim test02/index.jsp
<h1>this is test02 </h1>

4.3 dns resolution service configuration

[root@localhost webapps]# yum -y install bind
[root@localhost webapps]# vim /etc/named.conf
listen-on port 53 {
    
     any; };
allow-query     {
    
     any; };
[root@localhost webapps]# vim /etc/named.rfc1912.zones 
zone "test01.com" IN {
    
    
        type master;
        file "test01.com.zone";
        allow-update {
    
     none; };
};

zone "test02.com" IN {
    
    
        type master;
        file "test02.com.zone";
        allow-update {
    
     none; };
};
[root@localhost named]# vim test01.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www  IN  A     192.168.233.127
~                                   
[root@localhost named]# vim test02.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www  IN  A     192.168.233.127

4.4 Start service

[root@localhost named]# shutdown.sh 
[root@localhost named]# startup.sh
[root@localhost named]# systemctl start named

4.5 Testing

Insert picture description here

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_47219725/article/details/108006588