Tomcat
1. Introduction to tomcat
- Tomcat server is a free and open source web application server, belonging toLightweightApplication server is commonly used in small and medium-sized systems and occasions where there are not many concurrent access 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 due to its processingStatic HTMLIts capabilities are far less than Apache or Nginx, so Tomcat is usually used as a Servlet and JSP container to run on the back end separately.
Java Servlet
- It 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
- The full name is 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
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.
Two, install Tomcat service
①Turn off the firewall and security mechanism, and import the software package
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
cd /opt
②Install JDK
rpm -qpl jdk-8u201-linux-x64.rpm
rpm -ivh jdk-8u201-linux-x64.rpm
java -version
③Set JDK environment variables
cd /etc/profile.d/
vim 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
. java.sh
java -version
test
vim test.java
public class test {
public static void main(String[] args){
System.out.println("This is the test");
}
}
javac abc.java
java abc
④ Install and start Tomcat
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mv apache-tomcat-9.0.16 /usr/local/tomcat
/usr/local/tomcat/bin/startup.sh #启动tomcat
netstat -natp| grep 8080
⑤Browser access verification
http://192.168.153.10:8080 #浏览器访问Tomcat的默认主页
Three, optimize tomcat startup speed
The first time you start tomcat, you may find that Tomcat is very slow to start. 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
securerandom.source=file:/dev/urandom #117行
change into:
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
ll /usr/local/tomcat/
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 the Tomcat server, the more commonly used are server.xml, context.xml, tomcat-users.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 | Directory for storing project resources |
work | Tomcat working directory, generally used when clearing Tomcat cache |
Four, Tomcat virtual host configuration
- Many times a 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.wt.com and www.dw.com, are now added, and I hope to access different project contents through these two domain names.
①Create wt and dw project directories and files
mkdir /usr/local/tomcat/webapps/wt
mkdir /usr/local/tomcat/webapps/dw
echo "wt" > /usr/local/tomcat/webapps/wt/index.jsp
echo "dw" > /usr/local/tomcat/webapps/dw/index.jsp
②Modify the main Tomcat configuration file
vim /usr/local/tomcat/conf/server.xml
--165行前--插入
<Host name="www.wt.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false"
xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/wt" path="" reloadable="true" />
</Host>
<Host name="wWW.dw.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false"
xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/dw" path="" reloadable="true" />
</Host>
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
③Browser access verification
echo "192.168.153.10 www.wt.com www.dw.com" >> /etc/hosts
浏览器访问 http://www.wt.com:8080
浏览器访问 http://www.dw.com:8080
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.
vim /usr/local/tomcat/conf/server.xml #编辑Tomcat主配置文件
......
<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"/>
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh