Tomcat deployment and virtual host configuration and optimization

Introduction to Tomcat

  • 1. Free, open source Web application server
  • 2. The Apache Software Foundation is a core project of the Jakarta project
  • 3. It is jointly developed by Apache, Sun and some companies and individuals
  • 4. It is deeply loved by Java lovers and recognized by some software developers
  • 5. The popular Web application server

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

Tomcat component

Tomcat consists of a series of components, of which the core components are three:

  • Web container: complete the functions of 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

Java Servlet

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

Full name of JSP: Java Server Pages

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.

Tomcat deployment steps

  • 1. Download and install JDK
  • 2. Install and start Tomcat
  • 3. Configure virtual host
    jdk must be installed before deploying tomcat, because jdk is the necessary environment for Tomcat to run.

1. Close the firewall and upload the software package required to install Tomcat to the / opt directory

The required installation packages are:
apache-tomcat-9.0.16.tar.gz  
jdk-8u201-linux-x64.rpm 

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

2. Install JDK

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

3. Setting 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

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: it is a class library about the running environment, mainly the package of swing.
tools.jar: mainly the class libraries of some jdk tools, including javac, java, javap, javadoc, etc.
JDK: Java Development Kit
JRE: Java runtime environment
JVM: java virtual machine, which enables java programs to run class files on a variety of platforms.

######################Write "Hello World" in JAVA
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      #Used to check whether the JDK environment is set successfully
[root@localhost?opt]#java abc
Hello World!

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

##start-up tomcat##
/usr/local/tomcat/bin/startup.sh
netstat -natp | grep  8080

At this point, use the browser to access Tomcat's home page http://192.168.121.10:8080

5. Optimize Tomcat startup speed

When you start tomcat for the first time, you may find that it 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 
------Modify line 117-----
securerandom.source=file:/dev/urandom
##/dev/random and / dev/urandom are pseudo terminals, but / dev/urandom provides faster data streams

/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
#You can also create a soft connection, and then you can directly use shutdown SH and startup SH command
ln -s /usr/local/tomcat/bin/* /usr/local/bin/

#View directory
ll /usr/local/tomcat/

Description of main catalogue:

  • 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

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.song.com COM and www.benet.com COM, hoping to access different project contents through these two domain names.

1. Create gcc and benet project directories and files

mkdir /usr/local/tomcat/webapps/song
mkdir /usr/local/tomcat/webapps/benet
echo "this is song page\!" > /usr/local/tomcat/webapps/song/index.jsp
echo "this is benet page\!" > /usr/local/tomcat/webapps/benet/index.jsp

2. Modify Tomcat master configuration file

vim /usr/local/tomcat/conf/server.xml 
      </Host>
      <Host name="www.song.com"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
   <Context docBase="/usr/local/tomcat/webapps/song" path="" reloadable="true" />
       </Host>
       <Host name="www.benet.com"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
   <Context docBase="/usr/local/tomcat/webapps/benet" path="" reloadable="true" />
       </Host>

Host name :host name
appBase : Tomcat Program working directory, relative path is webapps,Absolute path is/usr/local/tomcat/webapps
unpackWARs :Decompress war package
autoDeploy :instructions Tomcat When running, if there is a new WEB Whether the application allows automatic deployment
xmlValidation :Verify xml Sign of validity inspection of documents
xmlNamespaceAware :Enable xml Namespace, set the value and xmlValidation by true,Express right web.xml Document execution effectiveness inspection

docBase : WEB Application directory
path:Set access URI by WEB Root directory of application
reloadable :Whether to reload in case of program changes
/usr/local/tomcat/bin/shutdown.sh 
/usr/local/tomcat/bin/startup.sh 

3. Client browser access authentication

echo "192.168.121.10 www.song.com www.benet.com" >> /etc/hosts

Browser access http://www.song.com:8080 this is song page!
Browser access http://www.benet.com:8080 this is benet page!

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.

Common 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, which is the number of initialized threads when Tomcat starts. It means that so many empty threads can be 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] specify 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.

  • [connectiontimeout] network connection timeout, unit: millisecond. Setting it to 0 means never timeout. This setting has hidden dangers. Usually, the default is 20000 microseconds.

  • [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, 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 perform GZIP compression on the response data. Off: indicates that compression is prohibited; On: indicates that compression is allowed (the text will be compressed), force: indicates that compression is carried out in all cases, and the default value is off. After compressing the data, the page size 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 size of the response message 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 to compress data.

  • [noCompressionUserAgents = "gozilla, traviata"] for the following browsers, compression is not enabled

The above are some common configuration parameters, and there are many other parameter settings, which can be further optimized. For the parameter attribute values of HTTP Connector and AJP Connector, you can refer to the detailed instructions in the official documents.

vim /usr/local/tomcat/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
------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/jpg,image/png"

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

Keywords: Linux Tomcat

Added by barney0o0 on Wed, 02 Feb 2022 21:27:27 +0200