tomcat
一、安装
tomcat运行需要JDK,JDK可以编译安装或者yum安装Openjdk,安装后需设备java环境变量,
二、开启自启动,参考/usr/lib/systemd/system目录下的服务,拷贝一份,进行编辑
cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/tomcat.service
#对tomcat.service文件进行编辑,vim tomcat.service
[Unit] Description=tomcat.service #Documentation=man:sshd(8) man:sshd_config(5) After=network.target #Wants=sshd-keygen.target [Service] Type=notify EnvironmentFile=-/etc/sysconfig/tomcatd 要修改JFK环境,在/etc/sysconfig/新建tomcat文件,把java_home,PATH,CLASS_PATH等环境变量设置好 ExecStart=/tomcat/bin/startup.sh Execstop=/tomcat/bin/shutdown.sh
KillMode=process
Restart=on-failure RestartSec=42s
[Install] WantedBy=multi-user.target
#文件解析
#1BIN文件解析
root@b830137c824a:/usr/local/tomcat/bin# ls bootstrap.jar
catalina.sh #主要脚本,在start和shutdown里都有调用
commons-daemon-native.tar.gz
configtest.sh
digest.sh
shutdown.sh #关闭脚本
tomcat-juli.jar
version.sh catalina-tasks.xml
ciphers.sh
commons-daemon.jar
daemon.sh
setclasspath.sh
startup.sh #启动脚本
tool-wrapper.sh
#logs日志文件
root@b830137c824a:/usr/local/tomcat/logs# ls catalina.2025-12-11.log //主要日志
host-manager.2025-12-11.log
localhost.2025-12-11.log
localhost_access_log.2025-12-11.txt //访问日志
manager.2025-12-11.log root@b830137c824a:/usr/local/tomcat/logs#
#conf文件
root@b830137c824a:/usr/local/tomcat/conf# ls Catalina
catalina.properties
jaspic-providers.xml
logging.properties #日志配置文件,可以改变日志文件的路径和名称
tomcat-users.xml #tomcat管理员身份配置文件,包括管理员账号和密码
web.xml #默认文件的设置 catalina.policy
context.xml #全局context配置文件,包括JDNI
jaspic-providers.xsd
server.xml #主要配置文件
tomcat-users.xsd
#LIB库文件,包含tomcat可以访问的jar包
root@b830137c824a:/usr/local/tomcat/lib# ls annotations-api.jar catalina.jar jaspic-api.jar tomcat-dbcp.jar tomcat-i18n-ko.jar tomcat-util-scan.jar catalina-ant.jar ecj-4.6.3.jar jsp-api.jar tomcat-i18n-de.jar tomcat-i18n-ru.jar tomcat-util.jar catalina-ha.jar el-api.jar servlet-api.jar tomcat-i18n-es.jar tomcat-i18n-zh-CN.jar tomcat-websocket.jar catalina-storeconfig.jar jasper-el.jar tomcat-api.jar tomcat-i18n-fr.jar tomcat-jdbc.jar websocket-api.jar catalina-tribes.jar jasper.jar tomcat-coyote.jar tomcat-i18n-ja.jar tomcat-jni.jar
三、配置文件解析
#server.xml,文件中以<!-- ** -->是注释
root@b830137c824a:/usr/local/tomcat/conf# more server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
This connector uses the NIO implementation. The default
SSLImplementation will depend on the presence of the APR/native
library and the useOpenSSL attribute of the
AprLifecycleListener.
Either JSSE or OpenSSL style configuration may be used regardless of
the SSLImplementation selected. JSSE style configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
This connector uses the APR/native implementation which always uses
OpenSSL for TLS.
Either JSSE or OpenSSL style configuration may be used. OpenSSL style
configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
四、管理界面
#部署
#1以WAR包放在webapps目录,tomcat会自动解压
#2jar包部署
java -jar jar文件名
#管理界面报403处理,编辑conf/tomcat-users.xml,一般都关闭。
root@b830137c824a:/usr/local/tomcat/conf# more tomcat-users.xml
<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="<1qaz@WSX>" roles="manager-gui,admin-gui"/>
</tomcat-users>
#虚拟主机
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="test1.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="test2.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
#监控命令
##jps && jmap && jstack
root@b830137c824a:/usr/local/tomcat/conf# jps -lvm 1 org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp 100 sun.tools.jps.Jps -lvm -Dapplication.home=/usr/local/openjdk-8 -Xms8m
root@b830137c824a:/usr/local/tomcat/conf# jstack Usage: jstack [-l] <pid> (to connect to running process) jstack -F [-m] [-l] <pid> (to connect to a hung process) jstack [-m] [-l] <executable> <core> (to connect to a core file) jstack [-m] [-l] [server_id@]<remote server IP or hostname> (to connect to a remote debug server)
root@b830137c824a:/usr/local/tomcat/conf# jmap
Usage:
jmap [option] <pid>
(to connect to running process)
jmap [option] <executable <core>
(to connect to a core file)
jmap [option] [server_id@]<remote server IP or hostname>
(to connect to remote debug server)
where <option> is one of:
<none> to print same info as Solaris pmap
-heap to print java heap summary
-histo[:live] to print histogram of java object heap; if the "live"
##监控JMX可通过zabbix进行监控
root@b830137c824a:/usr/local/tomcat/bin# vim catalina.sh
CATALINA_OPTS=”$CATALINA_OPTS -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=192.168.0.1 -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false”
五、安全加固
六、性能优化
浙公网安备 33010602011771号