The deployment and optimization of Tomcat (detailed graphic and text!)

Tomcat deployment and optimization

1. Introduction to Tomcat server

Tomcat server is a free and open source web application server. It is a lightweight application server. It is commonly used in small and medium-sized systems and where there are not many concurrent users. It is the first choice for developing and debugging JSP programs.
Generally speaking, although Tomcat is the same as Apache or Nginx web servers, it has the function of processing HTML pages. However, because its ability to process static HTML is far less than Apache or Nginx, Tomcat is usually used as a Servlet and JSP container, running on its own rear end.

Summary:

1) Free and open source web application server
2) A core project in the Apache Software Foundation Jakarta project
3) Jointly developed by Apache, Sun and some companies and individuals
4) Java Loved by enthusiasts and recognized by some software developers
5) Currently more popular web application servers

1.Java Servlet

A program running on a Web server or application server, which acts as an intermediate layer between the request from a Web browser or other HTTP client and the database or application on the HTTP server. Using Servlet, you can collect user input from web forms, present records from databases or other sources, and create web pages dynamically. Similar to CGI (Common Gateway Interface) function.

2. The full name of JSP Java Server Pages

A dynamic web development technology. It uses JSP tags to insert Java code in HTML pages. Tags usually start with <% and end with %>. JSP is a Java servlet, mainly used to implement the user interface part of a Java web application. JSP obtains user input data through web forms, accesses databases and other data sources, and then dynamically creates web pages.

3. Three core components of Tomcat

Tomcat consists of a series of components, of which there are three core components

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

Two, Tomcat deployment and installation

1) Download and install JDK

2) Install and start Tomcat

3) Configure virtual host

Note: jdk must be installed before Tomcat is deployed, because jdk is a necessary environment for Tomcat to run.

1. Turn off the firewall and upload the required software packages

Upload the software packages required to install Tomcat to the /opt directory

所需安装包为:
apache-tomcat-9.0.16.tar.gz  
jdk-8u201-linux-x64.rpm 

systemctl stop firewalld.service 
systemctl disable firewalld.service 
setenforce 0

Insert picture description here

2. Install JDK

rpm -qpl jdk-8u201-linux-x64.rpm
rpm -ivh jdk-8u201-linux-x64.rpm 
java -version

Insert picture description here

3. Set JDK environment variables

vim /etc/profile.d/java.sh
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

source /etc/profile.d/java.sh
java -version

Insert picture description here

Related explanation:

CLASSPATH: When compiling and running a Java program, JRE will search for the required class (.class) file in the path specified by the variable.
dt.jar: It is the class library about the operating environment, mainly the swing package.
tools.jar: It is mainly a class library of some jdk tools, including javac, java, javap, javadoc, etc.
JDK: java development kit (java development tool)
JRE: java runtime environment (java runtime environment)
JVM: java virtuak machine (java virtual machine), so that java programs can run class files on multiple platforms.

Write a java script to verify

cd /opt
vim abc.java
public class abc {
  public static void main(String[] args){
    System.out.println("Hello World!")
  }
}

[root@localhost?opt]#javac abc.java      #用来检测JDK环境是否设置成功
[root@localhost?opt]#java abc
Hello World!

Insert picture description here

4. Install and start 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
netstat -natp | grep  8080

Insert picture description here

Virtual machine browser visit http://192.168.2.7:8080

Insert picture description here

5. Optimize Tomcat startup speed

When you start tomcat for the first time, you may find that tomcat starts very slowly. By default, it may take tens of seconds. At this time, you can modify the jdk parameters for optimization.

vim /usr/java/jdk1.8.0_201-amd64/jre/lib/security/java.security 
------修改117行-----
securerandom.source=file:/dev/urandom
##/dev/random和/dev/urandom都是伪终端,但是/dev/urandom提供的数据流更快

/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
#也可以创建一个软连接,之后就能直接使用shutdown.sh和startup.sh命令了
ln -s /usr/local/tomcat/bin/* /usr/local/bin/

#查看目录
ll /usr/local/tomcat/

Insert picture description here

Main directory description:
●bin: store the script files for starting and shutting down Tomcat, the more commonly used are catalina. sh, startup.sh, shutdown.sh three files
●conf: store various configuration files of the Tomcat server, the more commonly used are server. xml, context. xml, tomcat-users.xml, web. xml four files.
●lib: store the jar package of the Tomcat server, generally do not make any changes, unless you connect to a third-party service, such as redis, then you need to add the corresponding Corresponding jar package
●logs: store Tomcat logs
●temp: store files generated when Tomcat is running
●webapps: store project resources
●work: Tomcat working directory, generally used when clearing Tomcat cache

Three, Tomcat virtual host configuration

Many times the company will have multiple projects to run, so it is certainly impossible to run multiple Tomcat services on one server, which will consume too much system resources. At this point, you need to use the Tomcat virtual host. For example, two new domain names, www.mhh.com and www.accp.com, are now added, and it is hoped that different project contents can be accessed through these two domain names.

1. Create mhh and accp working directories and their home page files

mkdir /usr/local/tomcat/webapps/mhh
mkdir /usr/local/tomcat/webapps/accp
echo "This is lic page\!" > /usr/local/tomcat/webapps/mhh/index.jsp
echo "This is accp page\!" > /usr/local/tomcat/webapps/accp/index.jsp

Insert picture description here

2. Modify the Tomcat main configuration file

vim /usr/local/tomcat/conf/server.xml
--------165行附近插入---------
<Host name="www.mhh.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/mhh" path="" reloadable="true" />
</Host>

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

Insert picture description here

Related notes:

Host name :主机名
appBase : Tomcat程序工作目录,相对路径为webapps,绝对路径为/usr/local/tomcat/webapps
unpackWARs :是否解压war包
autoDeploy :指示Tomcat运行时,如有新的WEB应用是否允许自动部署
xmlValidation :是否验证xml文件执行有效性检验的标志
xmlNamespaceAware :是否启用xml命名空间,设置该值与xmlValidation为true,表示对web.xml文件执行有效性检验

docBase : WEB应用的目录
path:设置访问的URI为WEB应用的根目录
reloadable :是否在程序有改动时重新载入
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh

Insert picture description here

3. Client browser access verification

echo "192.168.2.7 www.mhh.com www.accp.com" >> /etc/hosts

Insert picture description here

Browser access http://www.mhh.com:8080 page displays This is mhh page!
Browser access http://www.accp.com:8080 page displays This is accp page!

Insert picture description here

Fourth, Tomcat optimization

The default configuration under Tomcat's default installation is not suitable for the production environment. It may frequently appear suspended and needs to be restarted. Only through continuous stress testing and optimization can it run with the highest efficiency and stability. Optimization mainly includes three aspects: operating system optimization (kernel parameter optimization), Tomcat configuration file parameter optimization, and Java virtual machine (JVM) tuning.

-------Tomcat configuration file parameter optimization-------
Commonly used optimization related parameters are as follows:

[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.

[MinSpareThreads] The minimum number of idle threads, the number of threads initialized when Tomcat starts, means that there are so many empty threads waiting even if no one is using it. The default value is 10.

[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). Generally do not need to be specified.

[URIEncoding] Specify the URL encoding format of the Tomcat container. The language encoding format is not as easy to configure as other Web server software, and needs to be specified separately.

[ConnnectionTimeout] Network connection timeout, unit: milliseconds, set to 0 means never time out, this setting has hidden dangers. Usually the default is 20000 milliseconds.

[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.

[DisableUploadTimeout] Whether to use the timeout mechanism when uploading. Should be set to true.

[ConnectionUploadTimeout] Upload timeout. After all, file upload may take more time. Adjust this according to your own business needs, so that the Servlet has a longer time to complete its execution. It needs to be used in conjunction with the previous parameter. Will take effect.

[AcceptCount] Specifies 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, and the default is 100.

[Compression] Whether to perform GZIP compression on the response data, off: means that compression is forbidden; on: means that compression is allowed (the text will be compressed), force: means that compression is carried out in all cases, the default value is off, which can be effective after compressed data To reduce the size of the page, generally it can be reduced by about 1/3, which saves bandwidth.

[CompressionMinSize] indicates the minimum value of the compressed response. Only when the size of the response message is larger than this value will the message be compressed. If the compression function is enabled, the default value is 2048.

[CompressableMimeType] Compression type, specify which types of files are compressed.

[NoCompressionUserAgents="gozilla, traviata"] For the following browsers, compression is not enabled

The above are some commonly used configuration parameters, and there are many other parameter settings, and you can continue to optimize in depth. For the parameter attribute values ​​of HTTP Connector and AJP Connector, you can refer to the detailed description of the official document for learning.

vim /usr/local/tomcat/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
------71行插入-----------
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"

/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh #参数修改后重启服务

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_35456705/article/details/113487955