Tomcat server installation and optimization detailed steps

Tomcat server installation and optimization detailed steps

tomcat installation package

One, Tomcat overview

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 process static HTML is far less than Apache or Nginx, Tomcat is usually used as a Servlet and JSP container, running separately rear end.

Tomcat is composed of a series of components, of which there are three core components:
(1) Web container: completes the function of a Web server.
(2) Servlet container: the name is catalina, used to process Servlet code.
(3) JSP container: used to translate JSP dynamic web pages into Servlet code.

Java Servlet is a program running on a Web server or application server. It acts 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 stands for Java Server Pages, which is 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

Two, Tomcat service deployment

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

---------------------------------tips--------------- -------------------------------------------------- -
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 the 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 program

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

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 

或ln -s /usr/local/tomcat/bin/*.sh /usr/bin

可以直接使用startup.sh启动

netstat -natp | grep 8080
浏览器访问Tomcat的默认主页 http://192.168.126.10:8080

Insert picture description here

5. Optimize the startup speed of tomcat The
first time you start to check the log, you will find that Tomcat starts very slowly. By default, it may take tens of seconds. You can modify the jdk parameter to change it.
vim /usr/java/jdk1.8.0_201-amd64/jre/lib/security/java.security-line
117-modify
securerandom.source=file:/dev/urandom

停止:/usr/local/tomcat/bin/shutdown.sh或 shutdown.sh
启动:/usr/local/tomcat/bin/startup.sh 或 startup.sh
ll /usr/local/tomcat/

------Main catalog description----------------------------------------- -------------------------------------------------- ---

●bin :存放启动和关闭 Tomcat 的脚本文件,比较常用的是 catalina.sh、startup.sh、shutdown.sh 三个文件
●conf :存放 Tomcat 服务器的各种配置文件,比较常用的是 server.xml、context.xml、tomcat-users.xml、web.xml 四个文件。
●lib :存放 Tomcat 服务器的 jar 包,一般不作任何改动,除非连接第三方服务,比如 redis,那就需要添加相对应的 jar 包
●logs :存放 Tomcat 日志
●temp :存放 Tomcat 运行时产生的文件
●webapps :存放项目资源的目录
●work :Tomcat 工作目录,一般清除 Tomcat 缓存的时候会使用到

Insert picture description here
Insert picture description here

Three, Tomcat virtual host configuration

Many times the company has 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.chenwei.com and www.mm.com are now added, and I hope to access different project contents through these two domain names.

1. Create chenwei and mm project directories and files

mkdir /usr/local/tomcat/webapps/chenwei
mkdir /usr/local/tomcat/webapps/mm
echo "My name is chenwei\!" > /usr/local/tomcat/webapps/chenwei/index.jsp
echo "My name is mm\!" > /usr/local/tomcat/webapps/mm/index.jsp

2. Modify the Tomcat main configuration file

vim /usr/local/tomcat/conf/server.xml
--165行前--插入
<Host name="www.chenwei.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.mm.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/webapps/benet" path="" reloadable="true" />
</Host>

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 :是否在程序有改动时重新载入
停止:/usr/local/tomcat/bin/shutdown.sh或 shutdown.sh
启动:/usr/local/tomcat/bin/startup.sh 或 startup.sh

Insert picture description here

Insert picture description here
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 at 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 Parameters 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 is started, 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. 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, 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/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"/>
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_51573771/article/details/112687667