Tomcat service deployment and virtual host configuration and parameter optimization

Posted by stevehaysom on Wed, 09 Mar 2022 16:47:14 +0100

1. Introduction to Tomcat

  • Tomcat server is a free open source Web application server. It is a lightweight application server. It is widely used in small and medium-sized systems and when there are not many concurrent access users. It is the first choice for developing and debugging JSP programs
  • Generally speaking, Tomcat has the same function of processing HTML pages as Apache or Nginx. However, because its ability to process static html is far less than Apache or Nginx, Tomcat usually runs on the back end as a Servlet and JSP container

Three core components of Tomcat

  • Web container: complete the function of web server

  • Servlet: the container name is catalina, which is used to process servlet code

    • A program running on a Web server or application server, which acts as an intermediate layer between requests from Web browsers or other HTTP clients and databases or applications on an HTTP server
    • Using Servlet, you can collect user input from web forms, present records from databases or other sources, and dynamically create web pages. Similar to CGI (Common Gateway Interface) function
  • JSP container: used to translate JSP dynamic web pages into Servlet code

    • Is a dynamic web development technology. It uses JSP tags to insert Java code into HTML web pages. Labels usually start with <% and end with% >
    • JSP is a Java servlet, which is mainly used to realize the user interface part of Java web application. JSP obtains user input data, accesses database and other data sources through web page form, and then dynamically creates web pages

2. Tomcat service deployment and installation

① Close the firewall and transfer the software package required to install Tomcat to the / opt directory

systemctl stop firewalld
setenforce 0

② Install JDK

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

java -version

③ Setting JDK environment variables

vim /etc/profile.d/java.sh          #It can also be written in / etc/profile, / etc/profile D is the subdirectory of / etc/profile, which stores the startup scripts required by some applications
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64     #Working directory of java
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar   #"." refers to the current directory environment and outputs the class files required by the specified java     
export PATH=$JAVA_HOME/bin:$PATH   #Output redefined environment variable, $JAVA_HOME/bin: let the system give priority to identification in the front

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



Little knowledge

  • CLASSPATH: when compiling and running a Java program, JRE will search the path specified by the variable for the required class (. Class) file
  • dt.jar is a class library about the running environment, mainly the package of swing
  • tools.jar is mainly the class library of some jdk tools, including javac (compile to generate. Class file), java (run class file), javap (jdk comes with decompile tool), javadoc, etc
  • JDK: java development kit
  • JRE: java runtime environment
  • JVM: java virtual machine, which enables java programs to run class files on multiple platforms

④ Install and start Tomcat

cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mv apache-tomcat-9.0.16 /usr/local/tomcat
##Start tomcat
#Background start
/usr/local/tomcat/bin/startup.sh 
or
/usr/local/tomcat/bin/catalina.sh start	
	
#Foreground start
/usr/local/tomcat/bin/catalina.sh run

netstat -natp | grep 8080

Browser access Tomcat Default home page for
http://192.168.20.10:8080


⑤ Optimize tomcat startup speed

  • The first time you start and check the log, you will find that Tomcat starts very slowly. By default, it may take tens of seconds. You can modify the jdk parameters
vim /usr/java/jdk1.8.0_201-amd64/jre/lib/security/java.security

----------117 that 's ok----------modify
securerandom.source=file:/dev/urandom

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

ll /usr/local/tomcat/

explain

  • /dev/urandom is a non blocking version of / dev/random, that is, when there is no desirable random number in the entropy pool, the device reading / dev/urandom will still return a random number, but the security of the random number is not high in theory. If your application requires high security, you should use / dev/random

Main catalog description

  • bin: it stores the script files for starting and closing Tomcat. Catalina is commonly used sh,startup.sh,shutdown.sh three files
  • conf: store various configuration files of Tomcat server, and the more commonly used one is server xml,context.xml,tomcat-users.xml,web.xml four files.
  • lib: the jar package that stores the Tomcat server. Generally, no changes are made. Unless you connect to a third-party service, such as redis, you need to add the corresponding jar package
  • Logs: store Tomcat logs
  • temp: store the files generated by Tomcat runtime
  • webapps: the directory where project resources are stored
  • work: Tomcat working directory, which is usually used when clearing Tomcat cache

3. 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 point, you need to use the Tomcat virtual host. For example, two new domain names are now added: www.lcure.com COM and www.pcure.com COM, hoping to access different project contents through these two domain names

① Create lcure and pcure project directories and files

mkdir /usr/local/tomcat/webapps/lcure
mkdir /usr/local/tomcat/webapps/pcure
echo "This is lcure page\!" > /usr/local/tomcat/webapps/lcure/index.jsp
echo "This is pcure page\!" > /usr/local/tomcat/webapps/pcure/index.jsp

② Modify Tomcat master configuration file

vim /usr/local/tomcat/conf/server.xml
-------165 Before departure------insert
<Host name="www.lcure.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/webapps/lcure" path="" reloadable="true" />
</Host>

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

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


explain

  • Host name: host name

  • appBase: Tomcat program working directory. The relative path is webapps and the absolute path is / usr/local/tomcat/webapps

  • unpackWARs: decompress the war package

  • autoDeploy: indicates whether the Tomcat runtime allows automatic deployment of new WEB applications if any

  • xmlValidation: Flag whether to validate xml files and perform validation

  • xmlNamespaceAware: whether to enable xml namespace. Set this value and xmlValidation to true, which means to support web xml file to perform validation

  • docBase: directory of WEB application

  • path: set the access URL to the root directory of the WEB application. / / it is equivalent to the alias directory of alins

  • reloadable: whether to reload in case of program changes

③ Access verification

echo "192.168.20.10 www.lcure.com www.pcure.com" >> /etc/hosts

4. Tomcat optimization

  • The default configuration under Tomcat's default installation is not suitable for the production environment. It may frequently fake death and need to be restarted. Only through continuous pressure measurement and optimization can it run with maximum efficiency and stability. Optimization mainly includes three aspects: operating system optimization (kernel parameter optimization), Tomcat configuration file parameter optimization and Java virtual machine (JVM) optimization
vim /usr/local/tomcat/conf/server.xml
......
<Connector port="8080" protocol="HTTP/11.1" 
connectionTimeout="20000" 
redirectPort="8443" 
12345 
-----71 Row insertion----- 
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 

explain

  • maxThreads: Tomcat uses threads to process each request received. This value indicates the maximum number of threads that Tomcat can create. The default value is 200
  • minSpareThreads: the minimum number of idle threads. It refers to the number of initialized threads when Tomcat is started. It means that so many empty threads are opened to wait even if no one is using them. The default value is 10
  • maxSpareThreads: the maximum number of spare threads. Once the created thread exceeds this value, Tomcat will close the socket thread that is no longer needed. The default value is - 1 (unlimited). Generally, it is not necessary to specify
  • URIEncoding: Specifies the URL encoding format of Tomcat container. The language encoding format is not as convenient as other Web server software configuration, and needs to be specified separately
  • Connexiontimeout: network connection timeout, unit: milliseconds. Setting it to 0 means never timeout. This setting has hidden dangers. Usually, the default is 20000 milliseconds
  • enableLookups: whether to backcheck the domain name to return the host name of the remote host. The value is: true or false. If it is set to false, the IP address will be returned directly. 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 can be adjusted according to your own business needs, so that the Servlet can take a long time to complete its execution. It will take effect only when it is used together with the previous parameter
  • 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 GZIP compress the response data. Off: it means to prohibit compression; on: indicates that compression is allowed (the text will be compressed), force: indicates that compression is carried out in all cases. The default value is off. After compressing the data, the size of the page can be effectively reduced, which can generally be reduced by about 1 / 3 to save bandwidth
  • compressionMinSize: indicates the minimum value of the compression response. The message will be compressed only when the response message size is greater than this value. If the compression function is enabled, the default value is 2048
  • compressableMimeType: compression type, which specifies which types of files are compressed
  • noCompressionUserAgents = "gozilla, traviata": compression is not enabled for the following browsers

Topics: Linux