Tomcat deployment and optimization

Narrator

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, but because its ability to handle static HTML is far less than Apache or Nginx, Tomcat is usually used as a Servlet and JSP container to run on the back end.
Insert picture description here

One, Tomcat core components

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

2. What is Java Servlet? JSP?

Java Servlet is a program running on a web server or application server. It serves as an intermediate layer between a request from a web browser or other HTTP client and a 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,
JSP full name Java Server Pages
is a dynamic web development technology, which uses JSP tags to insert Java code in HTML web pages. The tags usually start with <% and end with %>. JSP is a kind of Java servlet, mainly used to implement the user interface part of Java web applications.
JSP obtains user input data through web forms, accesses databases and other data sources, and then dynamically creates web pages

Three, Tomcat service deployment

Experimental environment:
host: tomcat server
operating system: centos 7
ip address: 192.168.241.3
main software: jdk-8u201-linux-x64.rpm
apache-tomcat-9.0.16.tar.gz

Before deploying Tomcat, you must install jdk, because jdk is a necessary environment for Tomcat to run
1. Turn off the firewall, and transfer the software packages required to install Tomcat to the /opt directory

jdk-8u201-linux-x64.rpm
apache-tomcat-9.0.16.tar.gz

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

Insert picture description here

2. Install JDK

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

Insert picture description here
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
Insert picture description here

Expand small knowledge

CLASSPATH:编译、运行Java程序时,JRE会去该变量指定的路径中搜索所需的类(.class)文件
dt.jar:是关于运行环境的类库,主要是swing的包
tools.jar:主要是一些jdk工具的类库,包括javac,java,javap,javadoc等
JDK:java development kit (java开发工具)
JRE:java runtime environment (java运行时环境)
JVM:java virtuak machine (java虚拟机),使java程序可以在多种平台上运行class文件

4. Check whether the jdk is installed successfully

vim abc.java
public class abc {
    
    
	public static void main(String[] args){
    
    
		System.out.println("Hello world!");
    }
}
javac abc.java
java abc

Insert picture description here
Insert picture description here

5. 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
这里可以优化以下路径
ln -s /usr/local/tomcat/bin/*.sh /usr/local/bin
shutdown.sh
startup.sh
netstat -natp | grep 8080

The browser visits the default homepage of Tomcat http://192.168.241.3:8080 to
Insert picture description here
Insert picture description here
Insert picture description here
see the above picture, indicating that your Tomcat has been deployed.
6. Optimize the startup speed of tomcat The
first time you start tomcat, you may find that Tomcat startup is very slow, by default It may take tens of seconds to download, you can modify the jdk parameters to change

vim /usr/java/jdk1.8.0_201-amd64/jre/lib/security/java.security
##117行修改
securerandom.source=file:/dev/urandom

shutdown.sh
startup.sh

ll /usr/local/tomcat


Insert picture description here
Main catalog description:

  • bin: Store the script files for starting and shutting down Tomcat. The three files catalina.sh, startup.sh and shutdown.sh are more commonly used.
  • conf: store various configuration files of Tomcat server, the more commonly used are server.xml, context.xml, tomcat-user.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 jar package
  • logs: store Tomcat logs
  • temp: store files generated when Tomcat is running
  • webapps: a directory for storing project resources
  • work: Tomcat working directory, generally used when clearing Tomcat cache

Four, examples

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 many system resources. At this time, Tomcat virtual hosts need to be used. For example, two new domain names, www.abc.com and www.qwe.com, are now added. We hope to access different project contents through these two domain names.

Experimental environment:
host: tomcat server
operating system: centos 7
ip address: 192.168.241.3
main software: jdk-8u201-linux-x64.rpm
apache-tomcat-9.0.16.tar.gz
1. Create abc and qwe project directories and file

mkdir /usr/local/tomcat/webapps/abc
mkdir /usr/local/tomcat/webapps/qwe
echo "this is abc page" > /usr/local/tomcat/webapps/abc/index.jsp
echo "this is qwe page" > /usr/local/tomcat/webapps/qwe/index.jsp

2. Modify the Tomcat main configuration file

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

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

Insert picture description here

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

3. Client browser access verification

echo "192.168.241.3 www.abc.com www.qwe.com" >> /etc/hosts

Browser visit http://www.abc.com:8080 page displays This is abc page
Insert picture description here

The browser visits http://www.qwe.com:8080 and the page displays This is qwe page
Insert picture description here

Five, 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 efficiently and stably. Optimization mainly includes three aspects: operating system optimization (kernel parameter optimization), Tomcat configuration file parameter optimization, and Java virtual machine (JVM) tuning.

1. Tomcat configuration file parameter optimization

[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 idle 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. This is adjusted 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. The default is 100.
[Compression] Whether to perform GZIP compression on the response data, off: means that compression is prohibited; 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 page size, generally it can be reduced by about 1/3 to save 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, do not enable compression. The
above are some commonly used configuration parameters, and there are many other parameter settings. You can continue to optimize the parameter attribute values ​​of HTTP Connector and AJP Connector. , You can go to the detailed description of the official search document to learn.

vim /usr/local/tomcat/conf/server.xml
......
<Connector port="8080" protocol="HTTP/11.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"/>

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51432789/article/details/112661757