生产中tomcat的各种问题优化
1、修改或隐藏tomcat的版本号
cd /usr/local/tomcat/lib/
unzip catalina.jar
vim org/apache/catalina/util/ServerInfo.properties
16 server.info=Apache Tomcat/8.5.64
17 server.number=8.5.64.0
18 server.built=Mar 4 2021 23:14:16 UTC
jar uvf catalina.jar org/apache/catalina/util/ServerInfo.properties
#重启tomcat
systemctl restart tomcat
2、隐藏tomcat的错误信息
vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
#在Host位置内部插入下面
<Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false" />
#或者如下:
<Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false" errorCode.400="webapps/error.html"></Valve>
<Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false" errorCode.404="webapps/error.html"></Valve>
<Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false" errorCode.500="webapps/error.html"></Valve>
</Host>
#重启tomcat
systemctl restart tomcat
3、tomcat的jvm优化
vim /usr/local/tomcat/bin/catalina.sh
放在起始位置就可:
JAVA_OPTS="-Xms4096m -Xmx4096m -Xss512K -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./yzil_error.hprof"
或着全面如下:
JAVA_OPTS="-server -Xms4g -Xmx4g -Xss512k -Xmn1g -XX:CMSInitiatingOccupancyFraction=65 -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=10 -XX:NewRatio=2 -XX:PermSize=128m -XX:MaxPermSize=512m -XX:CMSFullGCsBeforeCompaction=5 -XX:+ExplicitGCInvokesConcurrent -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods"
4、tomcat加supervisor守护
yum install -y supervisor
systemctl enable --now supervisord
cat /etc/supervisord.d/tomcat.ini
[program:tomcat]
command=/usr/local/tomcat/bin/catalina.sh run
directory=/usr/local/tomcat/bin/
environment=JAVA_HOME="/usr/local/jdk/",JAVA_BIN="/usr/local/jdk/bin"
autostart=true
autorestart=true
startsecs=10
startretries=3
user=root
#stderr_logfile=/var/logs/tomcat/tomcat_out.log ;
#stdout_logfile=/var/logs/tomcat/tomcat_out.log ;
stopsignal=INT
stopasgroup=true