Tomcat basic installation and optimization methods

Brief introduction

Tomcat server is a free open source Web application server, are lightweight application server, it is not widely used in many occasions small and medium systems and concurrent user access is the preferred development and debugging JSP program. For a beginner, it can be considered that when the Apache server configured on a single machine, it responds to the access request can use HTML (under application of a Standard Generalized Markup Language) pages. Apache Tomcat server is actually an extension, but when it is run independently run, so when you run tomcat, it is in fact as a separate process with Apache running alone.

Tomcat installation

Installation preparation:

Before you install Tomcat must be installed JDK, JDK stands for Java Development Kit, SUN is provided free java language software development kit, which contains the Java Virtual Machine (JVM), write good java source code compiled to produce java byte code, as long as the JDK installation, you can use the JVM interprets the
bytecode files, thus ensuring the cross-platform nature of Java.

Platform compatibility, as the JDK bytecode interpretation based on this file and calls the operating system API implementation java virtual machine corresponding to the function, the operating system is closely related to the number of bits, so there are different kinds of versions, Tomcat also has the above characteristics, it is necessary to pre-download the JDK and Tomcat

JDK Download:
http://www.oracle.com/technetwork/java/javase/downloads/
Tomcat Download: http: //tomcat.apache.org

1. Uninstall system comes openjdk and install jdk
omitted here
2. Extract the Tomcat installation

[root@tomcat1 ~]# tar xf apache-tomcat-8.5.20.tar.gz [root@tomcat1 ~]# mv apache-tomcat-8.5.20 /usr/local/tomcat8

3. Set the environment variables JAVA

[root@tomcat1 ~]# vim /etc/profile.d/java.sh export JAVA_HOME="/usr/local/java" ##设置java根目录 export PATH=$PATH:$JAVA_HOME/bin:/usr/local/tomcat8/bin #在PATH环境变量中添加java跟目录的bin子目录 [root@tomcat1 ~]# . /etc/profile #将java.sh 脚本导入到环境变量,使其生效

4. Run java -version java or javac -version command to view the version

[root@tomcat1 ~]# java -version java version "1.7.0_65" Java(TM) SE Runtime Environment (build 1.7.0_65-b17) Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

5. Start Tomcat

[root@tomcat1 ~]# /usr/local/tomcat7/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat7 Using CATALINA_HOME: /usr/local/tomcat7 Using CATALINA_TMPDIR: /usr/local/tomcat7/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar Tomcat started.

Tomcat running on port 8080 by default

[root@tomcat1 ~]# netstat -anpt |grep :8080 tcp 0 0 :::8080 :::* LISTEN 3318/java

6. Close the Tomcat

[root@tomcat1 ~]# /usr/local/tomcat7/bin/shutdown.sh

Browser to access the test http: // IP: port

7. Modify the port number

[root@tomcat1 ~]# vim /usr/local/tomcat8/conf/server.xml

Modify the port:
8080 - 9528
8005 - 9529
8009 - 9530

[root@tomcat1 ~]# /usr/local/tomcat8/bin/startup.sh [root@tomcat1 ~]# netstat -anpt | grep java tcp 0 0 0.0.0.0:9528 0.0.0.0:* LISTEN 5625/java tcp 0 0 127.0.0.1:9529 0.0.0.0:* LISTEN 5625/java tcp 0 0 0.0.0.0:9530 0.0.0.0:* LISTEN 5625/java

Note: turn off the firewall and selinux security mechanism, if Ali cloud server, console security group requires the release of TCP 8080 port.

systemctl disable firewalld 永久关闭防火墙
systemctl stop firewalld 临时关闭防火墙 setenforce 0 关闭Selinux

8.Tomcat configuration instructions

/usr/local/tomcat Home directory (the installation directory)
bin stored to enable or disable the windows or linux platform Tomcat script file
conf to store a variety of global Tomcat configuration file, which is the most important server.xmland web.xml
lib storage needs to run Tomcat libraries (JARS)
logs stored at the Tomcat execution LOG file
webapps main Web Tomcat distribution directory (including application examples) htdocs (html php)
work generated after the deposit jsp compiled class files

/usr/local/tomcat/conf/:
catalina.policy Access control profile
catalina.properties Tomcat property profile
context.xml context configuration files (SELinux)
logging.properties log log related profiles
server.xml main configuration file
tomcat-users.xml manager-gui manage user profile (generated after Tomcat installation management interface, the file can be opened to access)
web.xml Tomcat a servlet, servlet -mapping, filter, MIME, and other related configuration
server.xml main configuration file, you can modify the startup port, set the root of the site, web hosting, open https and other functions.

Tomcat optimization

1. Disable the tomcat AJP protocol

[root@tomcat1 conf]# vim server.xml

Comment:

<!-- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> -->

2. Modify the communication mode BIO modified to improve NIO concurrent processing capability
# uncomment

<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"

3. Enable external connection pooling
uncommented and modified maxThreads:

<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="300" minSpareThreads="4"/>
-->

Uncomment and modify the protocol:

<Connector executor="tomcatThreadPool" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" redirectPort="8443" />

Delete two parameters:

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"/> (这是删除后的)

<Connector executor="tomcatThreadPool"Add the following:

port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="1000" minSpareThreads="100" maxSpareThreads="200" acceptCount="1000" disableUploadTimeout="true" connectionTimeout="20000" URIEncoding="UTF-8" enableLookups="false" compression="on" compressionMinSize="2048" compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg,image/png" redirectPort="8443" />

4.JVM tuning production configuration
to edit catalina.shprofiles

[root@tomcat1 ~]# vim /usr/local/tomcat8/bin/catalina.sh

OS specific support. $var _must_ be set to either true or false.Add the following

JAVA_OPTS="-server -Xms1024m -Xmx2048m -XX:PermSize=512m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC -XX:+UseParallelGCThreads=8 -XX:CMSInitiatingOccupancyFraction=80 -XX:
+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:-PrintGC -XX:-PrintGCDetails -XX:-PrintGCTimeStamps -Xloggc:../logs/gc.log"

Parameter Description
-Xmsheap initial size in m, g
-Xmxheap maximum allowable size, physical memory is generally not more than 80%
-XX:PermSizenon-initial heap size, the general application initialization settings 200m, 1024m enough maximum
-XX:MaxPermSizenon-heap memory maximum allowable size
-XX:+UseParallelGCThreads=8parallel collector the number of threads, while the number of threads for garbage collection, generally equal to the number of CPU
-XX:+UseParallelOldGCdesignated on behalf of older parallel collector
-XX:+UseConcMarkSweepGCCMS collector (concurrent collector)
-XX:+UseCMSCompactAtFullCollectionopen space compression and memory consolidation, to prevent excessive memory fragmentation

5. Production configuration example
server.xmlconfiguration file:

[root@tomcat1 ~]# vim /usr/local/tomcat8/conf/server.xml

<Connector executor="tomcatThreadPool"Add the following:

port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="1000" minSpareThreads="100" maxSpareThreads="200" acceptCount="1000" disableUploadTimeout="true" connectionTimeout="20000" URIEncoding="UTF-8" enableLookups="false" compression="on" compressionMinSize="2048" compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg,image/png" redirectPort="8443" />

Parameter Description:
org.apache.coyote.http11.Http11NioProtocol: adjustment mode Nio
maxThreads: maximum number of threads, the default 150. Avoid excessive increase the value of the request queue, resulting in slow response.
minSpareThreads: Minimum number of idle threads.
maxSpareThreads: Maximum number of idle threads, if more than this value, will close useless thread.
acceptCount: When the processing request exceeds this value, then the request will be queued up waiting.
disableUploadTimeout: Timeout Disable Upload
connectionTimeout: connection timeout, in milliseconds, 0 to not limit
URIEncoding: URI address. 8-encoded using UTF
enableLookups: Close dns resolution, improve the response time
compression: compression is enabled
compressionMinSize: the minimum compressed size, Unit Byte
compressableMimeType: compressed file type

Guess you like

Origin www.cnblogs.com/henrylinux/p/11516983.html