Nginx: Tomcat deployment and optimization (1)

1. Introduction to Tomcat

1.1 Introduction to Tomcat

1. Tomcat is developed in the Java language . The Tomcat server is a free and open source web application server . It is a core project in the Jakarta project of the Apache Software Foundation. become.
2. Tomcat is a lightweight application server . It is widely used in small and medium-sized systems and occasions where there are not many concurrent access users. It is the first choice for developing and debugging JSP0 programs. Generally speaking, although Tomcat has the same function as web servers such as Apache or Nginx, it has the function of processing HTML pages. However, because its ability to process static HTML is far less than that of Apache or Nginx, Tomcat is usually used as a Servlet and JSP container. rear end.

1.2 Components of the Tomcat core

1. Web container: complete the function of the Web server.
2. Servlet container: the name is catalina, which is used to process Servlet code.
3. JSP container: used to translate JSP dynamic web pages into Servlet codes.

  • So Tomcat is a Web application server and also a Servlet/JSP container. As a Servlet container, Tomcat is responsible for processing client requests, sending requests to Servlets, and sending Servlet responses back to clients.

1.2.1 What is a servlet

  • Servlet is the abbreviation of Java Servlet . It can be understood as a service connector , a server-side program written in Java, and has the characteristics of being independent of platforms and protocols. Simple understanding: servlet is a middleware, including interfaces and methods . Connect the client to the database to realize the creation of dynamic web pages.

1.2.2 What is JSP

1. The full name of JSP is Java Server Pages , which is a dynamic web page development technology . It uses JSP tags to insert Java code in HTML web pages. Tags usually start with <% and end with %>.
2. JSP is a Java servlet , which is mainly used to implement the user interface part of Java web applications .
3. JSP obtains user input data through web forms, accesses databases and other data sources, and then dynamically creates web pages .

1.3 Tomcat functional component structure

  • There are two core functions of Tomcat, namely 负责接收和反馈外部请求的连接器 Connector, and 负责处理请求的容器 Container. Among them, the connector and the container complement each other and together constitute the basic web service Service. Each Tomcat server can manage multiple Services.

1. Connector: Responsible for receiving and responding to external requests. It is the communication hub between Tomcat and the outside world. The listening port receives external requests, processes the requests and passes them to the container for business processing, and finally responds to the outside world with the processed results of the container.
2. Container: Responsible for internal processing of business logic. Its interior consists of four containers, Engine, Host, Context and Wrapper, which are used to manage and invoke Servlet-related logic.
3. Service: Web service provided externally. It mainly includes two core components, Connector and Container, and other functional components. Tomcat can manage multiple services, and each service is independent of each other.

1.3.1 Container structure analysis

  • Each Service will contain a Container container. There are 4 sub-containers inside the Container.

  • The functions of the four sub-containers are:
    1. Engine: Engine, used to manage multiple virtual hosts. A Service can only have one Engine at most;
    2. Host: Represents a virtual host, which can also be called a site, and can be configured by Host Add site;
    3. Context: represents a Web application, including multiple Servlet wrappers;
    4. Wrapper: wrapper, the bottom layer of the container. Each Wrapper encapsulates a Servlet, which is responsible for the creation, execution and destruction of object instances.

  • Engine, Host, Context and Wrapper, these four containers belong to parent-child relationship.

  • A container can manage multiple virtual hosts by one engine. Each virtual host can manage multiple web applications. Each web application will have multiple servlet wrappers.

1.4 Tomcat request process

1. The user enters the URL in the browser, and the request is sent to port 8080 of the machine, and is obtained by the Connector listening there; 2. The Connector hands
the request to the Engine (Container) of its Service for processing, and waits for the Engine 3. The
request is invoked layer by layer among the four containers of Engine, Host, Context and Wrapper, and finally executes the corresponding business logic and data storage in the Servlet.
4. After execution, the request response is returned layer by layer between the Context, Host, and Engine containers, and finally returned to the Connector, and returned to the client through the Connector.

2. Tomcat service deployment

#在部署 Tomcat 之前必须安装好 jdk,因为 jdk 是 Tomcat 运行的必要环境。
1.关闭防火墙,将安装 Tomcat 所需软件包传到/opt目录下
jdk-8u201-linux-x64.rpm
apache-tomcat-9.0.16.tar.gz

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

2.安装JDK
cd /opt
rpm -qpl jdk-8u201-linux-x64.rpm 
rpm -ivh jdk-8u201-linux-x64.rpm 
java -version

3.设置JDK环境变量
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

source /etc/profile.d/java.sh
java -version
  • Small knowledge
    1. JDK: java development kit (java development tool)
    2. JRE: java runtime environment (java runtime environment)
    3. JVM: java virtuak machine (java virtual machine), so that java programs can run on multiple platforms class file.
    4. CLASSPATH: Tell the jvm where to put the class to be used or executed, so that the JVM can load the class file.
    5. tools.jar: It is used when the system is used to compile a class, that is, it is used when javac is executed.
    6. dt.jar: dt.jar is a class library about the operating environment, mainly the swing package.
#验证一下JDK运行环境安装是否OK
首先使用文本工具编写java源代码,比如 Hello.java ;
在命令行中,输入命令:javac Hello.java,对源代码进行编译,生成 class 字节码文件;
编译完成后,如果没有报错信息,输入命令:java Hello,运行 class 字节码文件,由 JVM 对字节码进行解释和运行,打印 “Hello World”。

vim Hello.java
#类名、接口名命令:英文大小写字母、数字字符、$和_,不能使用关键字和数字开头;
一个单词命名时第一个单词的首字母要大写;多单词组成时,所有单词的首字母大写:XxxYyyZzz(大驼峰命名法)
public class Hello {			
  public static void main(String[] args){
    System.out.println("Hello world!");
  }
}

javac Hello.java
java Hello

insert image description here

4.安装启动Tomcat
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mv apache-tomcat-9.0.16 /usr/local/tomcat
##启动tomcat ##
#后台启动
/usr/local/tomcat/bin/startup.sh 
或
/usr/local/tomcat/bin/catalina.sh start	
	
#前台启动
/usr/local/tomcat/bin/catalina.sh run		

netstat -natp | grep 8080

vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=tomcat server
Wants=network-online.target
After=network.target
[Service]
Type=forking
Environment="JAVA_HOME=/usr/java/jdk1.8.0_201-amd64"
Environment="PATH=$JAVA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
Environment="CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar"
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
Restart=on-failure
[Install]WantedBy=multi-user.target

浏览器访问Tomcat的默认主页 http://192.168.119.3:8080

insert image description here
insert image description here
insert image description here

5.优化tomcat启动速度
第一次启动tomcat可能会发现 Tomcat 启动很慢,默认情况下可能会需要几十秒,可以修改jdk参数进行改。
vim /usr/java/jdk1.8.0_201-amd64/jre/lib/security/java.security
--117行--修改
securerandom.source=file:/dev/urandom

/usr/local/tomcat/bin/shutdown.sh 
/usr/local/tomcat/bin/startup.sh

insert image description here

  • Main Directory Description
ll /usr/local/tomcat/
●bin:存放启动和关闭 Tomcat 的脚本文件,如 catalina.sh、startup.sh、shutdown.sh 
●conf:存放 Tomcat 服务器的各种配置文件,如主配置文件 server.xml 和 应用默认的部署描述文件 web.xml 
●lib:存放 Tomcat 运行需要的库文件的 jar 包,一般不作任何改动
●logs:存放 Tomcat 执行时的日志
●temp:存放 Tomcat 运行时产生的文件
●webapps:存放 Tomcat 默认的 Web 应用项目资源的目录
●work:Tomcat 的工作目录,存放 Web 应用代码生成和编译文件

3. Tomcat virtual host configuration

  • In many cases, the company will have multiple projects that need to be run. Generally, multiple Tomcat services will not be run on one server, which will consume too many system resources. At this point, you need to use the Tomcat virtual host.
    For example: Now add two domain names www.kgc.com and www.benet.com, hoping to access different project content through these two domain names.
1.创建 kgc 和 benet 项目目录和文件
mkdir /usr/local/tomcat/webapps/kgc
mkdir /usr/local/tomcat/webapps/benet
echo "This is kgc page\!" > /usr/local/tomcat/webapps/kgc/index.jsp
echo "This is benet page\!" > /usr/local/tomcat/webapps/benet/index.jsp

2.修改 Tomcat 主配置文件 server.xml
vim /usr/local/tomcat/conf/server.xml
--165行前--插入
<Host name="www.kgc.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/webapps/kgc" path="" reloadable="true" />
</Host>

<Host name="www.benet.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/webapps/benet" path="" reloadable="true" />
</Host>

/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh

3.客户端浏览器访问验证
echo "192.168.119.3 www.kgc.com www.benet.com" >> /etc/hosts

浏览器访问 http://www.kgc.com:8080/   页面显示This is kgc page\! 
浏览器访问 http://www.benet.com:8080/   页面显示This is benet page\!


insert image description here

  • Configuration file description
Host name:主机名
appBase:Tomcat程序工作目录,即存放web应用程序的目录;相对路径为webapps,绝对路径为 /usr/local/tomcat/webapps
unpackWARs:在启用此webapps时是否对WAR格式的归档文件先进行展开;默认为true
autoDeploy:在Tomcat处于运行状态时放置于appBase目录中的应用程序文件是否自动进行deploy;默认为true
xmlValidation:是否验证xml文件执行有效性检验的标志
xmlNamespaceAware:是否启用xml命名空间,设置该值与xmlValidation为true,表示对web.xml文件执行有效性检验

Context
docBase:相应的Web应用程序的存放位置;也可以使用相对路径,起始路径为此Context所属Host中appBase定义的路径;
path:相对于Web服务器根路径而言的URI;如果为空"",则表示为此webapp的根路径 / ;
reloadable:是否允许重新加载此context相关的Web应用程序的类;默认为false
  • HTTP request process
    1. The port that the Connector listens to is 8080. Since the requested port matches the listening port, the connector accepts the request.
    2. Because the default virtual host of the engine is www.kgc.com, and the directory of the virtual host is webapps. So the request finds the tomcat/webapps directory.
    3. The access path is the root path, and the URI is empty, that is, the empty is the application name of the Web program, that is, the context. At this time, the request finds the /usr/local/tomcat/webapps/kgc directory, parses index.jsp and returns it.

4. Tomcat multi-instance deployment

1.安装好 jdk
2.安装 tomcat
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mkdir /usr/local/tomcat
mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1
cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2

3.配置 tomcat 环境变量
vim /etc/profile.d/tomcat.sh
#tomcat1
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1

#tomcat2
export CATALINA_HOME2=/usr/local/tomcat/tomcat2
export CATALINA_BASE2=/usr/local/tomcat/tomcat2
export TOMCAT_HOME2=/usr/local/tomcat/tomcat2


source /etc/profile.d/tomcat.sh

4.修改 tomcat2 中的 server.xml 文件,要求各 tomcat 实例配置不能有重复的端口号
vim /usr/local/tomcat/tomcat2/conf/server.xml
<Server port="8006" shutdown="SHUTDOWN">		#22行,修改Server prot,默认为8005 -> 修改为8006
<Connector port="8081" protocol="HTTP/1.1"		#69行,修改Connector port,HTTP/1.1  默认为8080 -> 修改为8081
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />	#116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010

第一个连接器默认监听8080端口,负责建立HTTP连接。在通过浏览器访问Tomcat服务器的Web应用时,使用的就是这个连接器。 第二个连接器默认监听8009端口,AJP端口,即容器使用,如Apache能通过AJP协议访问Tomcat的8009端口。

5.修改各 tomcat 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量
vim /usr/local/tomcat/tomcat1/bin/startup.sh 
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1


vim /usr/local/tomcat/tomcat1/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1


vim /usr/local/tomcat/tomcat2/bin/startup.sh 
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2


vim /usr/local/tomcat/tomcat2/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2

6.启动各 tomcat 中的 /bin/startup.sh 
#防止失误可以先停止服务再启动
/usr/local/tomcat/tomcat1/bin/startup.sh 
/usr/local/tomcat/tomcat2/bin/startup.sh 

netstat -natp | grep java

7.浏览器访问测试
http://192.168.119.4.0:8080
http://192.168.119.4:8081

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/2301_76875445/article/details/131047449