Tomcat installation and deployment, virtual host construction and optimization

One, Tomcat overview

1. The difference between web pages

Static webpage: Apache, Ngint support, webpage type suffix .html.htm
Dynamic webpage: Tomcat support, webpage type suffix .jsp

2.Tomcat introduction

Free, open source web application server
is jointly developed by Apache, Sun, and some companies and individuals, and this
is currently the more popular web application server

3.Tomcat core components

Insert picture description here

3.1 The difference between jsp and Servlet

jsp: is the html file containing java program, display page, generally used for front-end
Servlet: is the java file containing html, the component that actually performs specific operations in the background, generally used in the background

4. Tomcat processing request process

The connector that receives data is the Connector connector; what really handles is the Container container
Connector: the most important function is to receive the connection request 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 .

Process diagram:
Insert picture description here

connector负责接受用户请求和消息报文的,将消息报文发往container
container是容器对外映射的接口,用于封闭和管理servlet

engine:是一个容器,是用来将用户的请求发往它下面的虚拟主机的,engine是加载虚拟主机的一个容器
hosts:是虚拟主机的容器
context:管理servlet。是host虚拟主机的实体内容的指向
servlet:是包含html的java文件,其实是java语言的一个类

4.1 Detailed introduction of request processing

1.用户在浏览器中输入网址localhost:8080/ test/index. jsp, 请求被发送到本机端口8080, 被在那里监听的Coyote HTTP/1.1 Connector获得;

2.Connector 把该请求交给它所在的Service 的Engine (Container) 来处理,并等待Engine 的回应; 

3.Engine 获得请求localhost/test/ index. jsp, 匹配所有的虚拟主机Host;

4.Engine 匹配到名为localhost 的Host (即使匹配不到也把请求交给该Host 处理,因为该Host被定义为该Engine的默认主机),名为localhost的Host获得请求/test/ index. jsp, 匹配它所拥有的所有Context。 Host 匹配到路径为/test 的Context (如果匹配不到就把该请求交给路径名为“”的Context去处理

5.path=/test”的Context 获得请求/ index. jsp, 在它的mapping table中寻找出对应的Servlet。Context 匹配到URL Pattern 为*. jsp 的Servlet, 对应于JspServlet类;

6.构造HttpServletRequest 对象和HttpServletResponse 对象,作为参数调用JspServlet的doGet ()或doPost(),执行业务逻辑、数据存储等;

7.Context把执行完之后的HttpServletResponse对象返回给Host;

8.Hos t把Ht tpServl etResponse对象返回给Engine;

9.Engine把Ht tpServletResponse对象返回Connector;

10.Connector把HttpServletResponse 对象返回给客户Browser。

Two, Tomcat deployment steps

1. Download and install 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 and
    use the xshell file to transfer the jdk installation package to the root directory

1. Unzip

[root@server1 ~]# tar zvxf jdk-8u91-linux-x64.tar.gz   #解压jdk安装包
[root@server1 ~]# mv jdk1.8.0_91/ /usr/local/java      #移动安装包
[root@server1 ~]# cd /usr/local/java/              
[root@server1 java]# ll

2...Set environment variables (settings are global variables that can be used by all users)

[root@server1 ~]# vi /etc/profile
[root@server1 ~]# source /etc/profile   #环境变量生效
[root@server1 ~]# echo $PATH         
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin:/usr/local/java/lib:/usr/local/java/jre/bin:/usr/local/java/jre/lib

添加
export JAVA_HOME=/usr/local/java    定义变量,设为全局变量(设置JAVA的根目录)
export JAVA_JRE=/usr/local/java/jre  
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/lib:$JAVA_JRE/bin:$JAVA_JRE/lib
环境变量

Insert picture description here
3. View version

[root@server1 ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)

4. Configure java script

[root@server1 ~]# vi abc.java
[root@server1 ~]# javac a.java 
[root@server1 ~]# java a

编辑:
public class a{
    
    

        public static void main (String[] args) {
    
    
               System.out.println ("Hello World!");
  }
}

2. Install and start Tomcat

Use the xshell file to transfer the tomcat installation package to the root directory
1. View the tomcat startup and shutdown scripts

[root@server1 ~]# cd /usr/local/tomcat/
[root@server1 tomcat]# ls -lh
[root@server1 tomcat]# cd bin/
[root@server1 bin]# ls -lh

Insert picture description here
2. Create a soft link

[root@server1 ~]# ln -s /usr/local/tomcat/bin/startup.sh /usr/bin/tomcatup
[root@server1 ~]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/bin/tomcatdown
[root@server1 ~]# tomcatup     服务启动
[root@server1 ~]# netstat -anpt | grep 8080   查看8080端口状态

Insert picture description here

3. Create a test page

1. Create a site directory

[root@server1 ~]# mkdir -p /web/app1
[root@server1 ~]# mkdir -p /web/app2
[root@server1 ~]# vi /web/app1/index.jsp
[root@server1 ~]# cd /web/app1        #进入app1目录中通过Xshell传输一张图片到目录中

添加:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>JSP Web 1</head>
<body>
<% out.println("This is Fa!");%>
<div>动态页面</div><br/><img src="a.jpg" />
</body>
</html>

%%:表示区间
这个jsp中引入的page标签
language表示语言是java
import类似于java类中的import就是把包导入进来,这样在jsp才可以调用包中的类
pageEncoding表示页面的编码格式

2. Create a directory connection

[root@server1 ~]# vi /usr/local/tomcat8/conf/server.xml 
[root@server1 ~]# tomcatdown     服务关闭
[root@server1 ~]# tomcatup         服务重启
[root@server1 ~]# netstat -anpt | grep 8080  查看端口状态

添加:
<Context docBase="/web/app1" path="" reloadable="false">
 </Context>

docBase:wed应用的文档基准目录
reloadable:本项为true时,当web.xml或者class有改动的时候都会自动重新加载不需要重新启动服务(所谓的支持热发布功能)
path=“”指定访问路径URI(虚拟目录名),访问url路径下是否跟内容

Insert picture description here
Insert picture description here
Test:
20.0.0.13:8080
Insert picture description here

4. Tomcat local access through domain name

1. Configuration file

[root@server1 ~]# systemctl stop firewalld
[root@server1 ~]# setenforce 0
[root@server1 ~]# vi /usr/local/tomcat/conf/server.xml 
[root@server1 ~]# tomcatdown 
[root@server1 ~]# tomcatup 
[root@server1 ~]# netstat -anpt | grep 8080

修改:
 localhost为www.fa.com

Insert picture description here
Add mapping on another client
2. Add mapping record

[root@server2 ~]# vi /etc/hosts
添加:
20.0.0.13   www.fa.com

Test:
www.fa.com:8080
Insert picture description here

Three, virtual host configuration

1. When multiple projects are running at the same time, it is not recommended to run multiple Tomcat services on one server, and virtual host needs to be configured
. 2. Access to different project contents through two domain names

1. Based on the domain name

[root@server1 ~]# vi /usr/local/tomcat8/conf/server.xml 
添加:
 <Host name="www.nb.com"  appBase="webapps"
         unpackWARs="true" autoDeploy="true">
  
   <Context docBase="/web/app2" path="" reloadable="false">
   </Context>
   </Host>

Insert picture description here
2. Configure the second app2 page

[root@server1 ~]# cd /web/app1
[root@server1 app1]# cp index.jsp /web/app2
[root@server1 app1]# cd /web/app2      #进入app2目录中,通过xshell传输图片到app2目录中
[root@server1 app2]# ls -lh

3. Modify app2 configuration

[root@server1 app2]# vi index.jsp 
[root@server1 app2]# tomcatdown 
[root@server1 app2]# tomcatup 
[root@server1 app2]# netstat -anpt | grep 8080 

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>JSP Web 2</head>
<body>
<% out.println("This  is  Nb!");%>
<div>动态页面2</div><br/><img src="b.jpg" />
</body>
</html>

Access test on another client
4. Add mapping

[root@server2 ~]# vi /etc/hosts
添加:
www.nb.com

test:
Insert picture description here

2. Based on port

[root@server1 ~]# vi /usr/local/tomcat8/conf/server.xml  
[root@server1 ~]# tomcatdown 
[root@server1 ~]# tomcatup 
[root@server1 ~]# netstat -anpt | grep java
[root@server1 ~]# netstat -anpt | grep 8090
[root@server1 ~]# netstat -anpt | grep 8080

先删除之前的配置文件,在添加
 <Service name="Catalina2">
   <Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
    <Engine name="Catalina2" defaultHost="localhost">
     <Host name="www.ab.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
      <Context docBase="/web/app2" path="" reloadable="false">
     </Context>
     </Host>
  </Engine>
 </Service>

Insert picture description here
Test:
www.fa.com:8080
Insert picture description here
www.nb.com:8090
Insert picture description here

Fourth, Tomcat optimization

1. Tomcat configuration file parameter optimization

1.maxThreads: Tomcat 使用线程来处理接收的每个请求,这个值表示Tomcat 可创建的  最大的线程数,默认值是2002.minSpareThreads: 最小空闲线程数,Tomcat 启动时的初始化线程数,表示即使没有人使用也开这么多空线程等待,默认值是103.maxSpareThreads: 最大备用线程数,一旦创建的线程超过这个值,Tomcat就会关闭不再需要的socket 线程。默认值是 -1 (无限制),一般不需要指定。

4.URIEncoding: 指定Tomcat 容器的URL编码格式,Tomcat 语言编码格式这块不如其它Web 服务器软件配置方便,需要分别指定。

5.connnectionTimeout: 网络连接超时,单位:毫秒,设置为0表示永不超时,这样设置有隐患的。通常默认20000毫秒(20秒)就可以。

6.enableLookups: 是否反查域名,以返回远程主机的主机名,取值为: truefalse,如果设置为false,则直接返回IP地址,为了提高处理能力,应设置为false7.disableUploadTimeout:上传时是否使用超时机制。应设置为true8.connectionUploadTimeout:上传超时时间,毕竟文件上传可能需要消耗更多的时间,该参数需要根据自己的业务需要自行调整,以使Servlet 有较长的时间来完成它的执行,需要与上一个参数一起配合使用才会生效。

9.acceptCount:指定当所有可以使用的处理请求的线程都被使用时,可传入连接请求的最大队列长度,超过这个数的请求将不予处理,默认为100个。

10.compression: 是否对响应的数据进行GZIP 压缩,off表示禁止压缩、on表示允许压缩(文本将被压缩)、force 表示所有情况下都进行压缩,默认值为off。压缩数据后可以有效的减少页面的大小,一般可以减小1/3 左右,因而节省带宽。

11.compressionMinSize: 表示压缩响应的最小值,只有当响应报文大小大于这个值的时候才会对报文进行压缩,如果开启了压缩功能,默认值就是2048。(1k)

12.compressableMimeType:压缩类型,指定对哪些类型的文件进行数据压缩。

13.noCompressionUserAgents=” gozilla, traviata": 对于以下的浏览器,不启用压缩

Guess you like

Origin blog.csdn.net/F2001523/article/details/110772456