1. Introduction to Tomcat server
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.
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 on the back end separately.
(1) Three core components of Tomcat
Tomcat consists of a series of components, of which there are three core components
- Web container: complete the function of the Web server.
- Servlet container: named catalina, used to process Servlet code.
- JSP container: used to translate JSP dynamic web pages into servlet code.
(2)Java Servlet
A program running on a Web server or application server, which 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.
(3) The full name of JSP Java Server Pages
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, which is 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 construction experiment
The experiment requires a software package: (the software package is placed in the /opt directory)
- Tomcat
- Jdk
(1) Turn off the firewall
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
(2) Install JDK
cd /opt
rpm -qpl jdk-8u201-linux-x64.rpm
rpm -ivh jdk-8u201-linux-x64.rpm
java -version
JAVA running 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
Display the JAVA version after refreshing:
(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
cd/usr/local/tomcat/bin/
./startup.sh
netstat -natp | grep 8080
Open the service and check whether it is open:
visit http://local IP:8080 on the browser to view the service
(5) Optimize the startup speed of tomcat
vim /usr/java/jdk1.8.0_201-amd64/jre/lib/security/java.security
-----117行修改-----
securerandom.source=file:/dev/urandom
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
ll /usr/local/tomcat/
Three, Tomcat virtual host configuration
The company will have multiple projects to run, so it is impossible to run multiple Tomcat services on one server. At this time, Tomcat virtual host is needed.
(1) Create project directories and files (at least two)
mkdir /usr/local/tomcat/webapps/lfp
mkdir /usr/local/tomcat/webapps/accp
echo "This is lfp page\!" > /usr/local/tomcat/webapps/lfp/index.jsp
echo "This is accp page\!" > /usr/local/tomcat/webapps/accp/index.jsp
(2) Modify the main Tomcat configuration file
vim /usr/local/tomcat/conf/server.xml
----165行前插入-----
<Host name="www.lfp.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/lic" path="" reloadable="true" />
</Host>
<Host name="www.accp.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/accp" path="" reloadable="true" />
</Host>
Restart the service:
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
Temporary resolution (Note: when DNS is not configured)
echo "192.168.90.10 www.lfp.com www.accp.com" >> /etc/hosts
Fourth, Tomcat service 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
- Java Virtual Machine (JVM) tuning
vim /usr/local/tomcat/conf/server.xml
-----69行显示-----
<Connector port="8080" protocol="HTTP/11.1"
connectionTimeout="20000"
redirectPort="8443"
-----往下插入-----
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"/>