Hello, today I will share with you Tomcat deployment and optimization. Please take out your notebook and write it down

I. Introduction to Tomcat

1, concept,

Tomcat is a free and open source Web application server. It is a lightweight application server and a core open source project of the Apache Software Foundation

Tomcat is an extension of Apache, a subproject of Apache, which has all the functions of a Web server. It can not only listen to accept requests and respond to static resources, but also can run Java code servlets with specific specifications on the back end, and write the results of execution back to the client in the form of HTML code

Is a development platform and environment, is a Web service to handle dynamic requests,

The official website is tomcat.apache.org/

Apache Software Foundation (ASF for short)

2. Tomcat core components

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

Web container: Complete the functions of the Web server; Such as responding to protocols such as TCP/IP, HTTP/HTTPS, and processing (nginx) application interactions that handle static pages

JSP container: as a component of the Java front end, it displays processing logic transactions in the form of index.jsp and acts as connection access

There are two aspects: processing dynamic interaction requests and data interaction

In particular, it is used to translate JSP dynamic pages into Servlet code (index.html, index.php, index.jsp).

Is a dynamic web development technology. It uses JSP tags to insert Java code into HTML web pages. Tags usually start with <% and end with %>

JSP is a Java servlet that is primarily used to implement the user interface portion of a Java Web application

JSP uses web forms to get user input data, access databases and other data sources, and create web pages on the fly.

JSP container provides multiple JAVA front-end page display ending in index.jsp, and index.jsp has many HTML tags, HTML tags will be nested in a lot of JAVA code, which will eventually be translated into Servlet code, (The JSP container itself does not have the ability to parse and analyze code, so it is handed over to the Servlet container.)

Servlet container: Mainly handles Javs back-end logic business and is a core component of back-end interaction

Servlet code is processed by a script named Catalina (Servlet code is written in Java)

Servlet container calls API interface, find the docking project, the docking project from mysql database to obtain the corresponding information, such as: database interaction, encryption, alipay, face recognition, etc., after processing will return these data to JSP, through JSP index.jsp display, and then return the corresponding information to the customer

If the user accesses Tomcat through HTTPS port 443, the mapped encrypted port of Tomcat is 8443

3. Tomcat handles the request process

The request is sent to port 8080, the Service as a process supports Tomcat, and the Connector as a Connector waits to listen on port 8080 in HTTP1.1. Can be seen as the environment in which the Connector runs.)

(2) Deliver it to the Engine in the backend Container

In the container supported by the engine, there will be a project host (representing the project, such as Alipay, Taobao, etc.) to interact with each other, and use the context to connect to the front and back ends of Java

(4) Hand over to servlet to handle Java back-end data and database interaction

The serlet returns to the context(connector)

⑥ The context is returned to the engine

The engine returns to the port, and finally presents the page to the customer by mapping the port

Second, Tomcat deployment

1. Deployment and management process

Download and install JDK (compiler similar to GCC gcc-C ++)

The JDK converts source files into execution files

Take JAVA as an example. Suppose: there is an abc.java file that can be translated into abc.class (executable) using the JDK compiler.

Environment variable: PATH

User environment variables: apply only to users

System environment variable: takes effect only for hosts

Install and start Tomcat

Configuring a Virtual Host

You must have the JDK installed before deploying Tomcat, because the JDK is the necessary environment for Tomcat to run.

2. Environment deployment

Upload the software package required for Tomcat installation to the /opt directory

The code is as follows (example) :

  • Install the JDK

The code is as follows:

CLASSPATH: When you compile and run a Java program (Tomcat), the JRE searches the path specified by this variable for the required class (.class) file

Dt.jar: a library of runtime environments, mainly swing packages.

Tools. jar: a library of JDK tools, including Javac, Java, Javap,javadoc, etc

JDK: Java Development Kit

JRE: Java Runtime Environment

JVM: Java Virtuak Machine (Java Virtual Machine), enables Java programs on a variety of platforms. Run the class file on

  • Install and start Tomcat

The code is as follows:

  • Bin: stores the script files for starting and stopping Tomcat. The commonly used files are catalina.sh, startup.sh, and shutdown.sh
  • Conf: Stores various configuration files of the Tomcat server. Server. XML (the main configuration file), context. XML, tomcat-users. XML, and web. XML are commonly used
  • Lib: Stores the jar packages of the Tomcat server. Generally, no changes are made unless connecting to a third-party service, such as Redis, which requires adding the corresponding JAR packages
  • Logs: Stores Tomcat logs
  • Temp: stores files generated when Tomcat is running
  • Webapps: Objects for storing project resources
  • Work: Tomcat. The working directory is used to clear the Tomcat cache

Using a browser to access the Tomcat home page:http://192.168.35.40:8080/

Third, Tomcat optimization

1. Optimize startup speed

Tomcat may start slowly for the first time in the production environment, which may take tens of seconds by default. In this case, you can modify JDK parameters for optimization

Key: server. XML: default configuration (search 8080 to locate); The main function is to connect to NGINx, page requests

2. Configure Tomcat virtual host

In many cases, companies will have multiple projects to run, so it is definitely not possible to run multiple Tomcat services on one server, which will consume too much system resources. At this point, you need to use Tomcat virtual host

For example, now two new domain names www. zyt.com and www.zw.com are added, and we hope to access different project contents through these two domain names (it should be noted that Tomcat implements different project contents through the virtual host).

# Configuration details

Host name: indicates the Host name

AppBase: Tomcat application working directory, relative paths for webapps, the absolute path to/usr/local/Tomcat/webapps

UnpackWARs: Specifies whether Tomcat automatically decompresses the WAR file when it finds it in the Webapps folder

AutoDeploy: If this parameter is set to true, Tomcat automatically redeploys the program when web. XML changes. This functionality must allow for background processing

XmlValidation: Whether to enable validation of XML files

XmlNamespaceAware: Whether XML namespace is enabled and sets this value, together with xmlValidation to true, to validate the web.xml file

DocBase: directory for WEB application (local path)

Path: Set the access URI to the root directory (URL path) of the WEB application. “” indicates the default. Load the classes (class files) in the system.

Reloadable: Whether to reload the program when changes are made

The following mapping needs to be added on the local PC: Disk C – Windows – System32 – drivers – etc – hosts – CTRL + S (Save)

Browser accesswww.zyt.com:8080/ The page displays: this is zyt page!

www.zw.com:8080/ page display: This is ZW page!

3. Common optimization parameters of Tomcat coordination file are as follows:

[maxThreads] : Tomcat uses threads to process each request it receives. This value represents the maximum number of threads Tomcat can create. The default is 200.

[minSpareThreads] : Specifies the minimum number of idle threads to be initialized when Tomcat is started. The default value is 10.

[maxSpareThreads] : Specifies the maximum number of spare threads that Tomcat will close if no longer needed. The default value is -1 (unrestricted). Generally, you do not need to specify.

[URIEncoding] : Specifies the URL encoding format of the Tomcat container. The language encoding format is easier to configure than other Web server software.

[connnectionTimeout] : indicates that the network connection times out, in millimeter seconds. If the value is set to 0, it never times out. The default is usually 20000 milliseconds.

[enableLookups] : specifies whether to reverse search the domain name to return the host name of the remote host. The value can be true or false. If this parameter is set to false, the IP address is returned.

[disableUploadT imeout] : indicates whether to use the timeout mechanism during upload. It should be set to true. (Unlock and open)

[Connect ionUploadTimeout] : upload timeout. After all, file uploadTimeout may take more time. This parameter is adjusted according to your business needs so that the Servlet has a longer time to complete its execution. (Specific mechanism)

[acceptCount] : Specifies the maximum queue length for incoming connection requests when all available threads are used. Any requests exceeding this number will not be processed. The default value is 100.

[compression] : indicates whether to perform GZIP compression on the response data. Off: indicates that compression is disabled. On: allows compression (text will be compressed). Force: enables compression in all cases. The default value is off. After data compression, the page size can be reduced by 1/3, saving bandwidth. Force: the command is forcibly executed

[compressionMinSize] : indicates the minimum value for compressed responses. Packets are compressed only when the size of response packets is larger than this value. If the compression function is enabled, the default value is 2048

[compressableMimeType] : indicates the compression type. Specifies which types of files are compressed.

[noCompressionUserAgents= “gozilla, Traviata”] : For the following browsers, compression is not enabled

Fourth, Ningx+Tomcat integration

In LNMP, for example, the most basic architecture in an enterprise requires a page for static Web services, a page for dynamic Web services, and a database. And we implemented Nginx + PHP on Linux. However, in actual production, one Nginx often needs to “correspond” to multiple dynamically processed services (namely Tomcat), so how to transfer the dynamic requests received from the front-end to multiple tomcat processes at the back-end is what we study here

Environment: two Tomcat servers and one Nginx

Nginx 192.168.35.40

Tomcat1 192.168.35.10

Tomcat2 192.168.35.131

For security reasons: When Tomcat receives a request from nginx service, the source IP address is nginx service. For security reasons, it is necessary to assign the IP address of the real client to the source IP address of Tomcat, so that the source of the attack can be known and the blacklist and whitelist can also be made

Well, this is the end of today’s article, I hope to help you confused in front of the screen