/usr/local/tomcat 主目錄(安裝目錄) bin 存放windows或linux平臺上啓動或關閉的Tomcat的腳本文件 conf 存放Tomcat的各類全局配置文件,其中最主要的是server.xml和web.xml lib 存放Tomcat運行須要的庫文件(JARS) logs 存放Tomcat執行時的LOG文件 webapps Tomcat的主要Web發佈目錄(包括應用程序事例)htdocs(html php) work 存放jsp編譯後產生的class文件 [root@tomcat1 ~]# ls /usr/local/tomcat/conf/ catalina.policy 權限控制配置文件 catalina.properties Tomcat屬性配置文件 context.xml 上下文配置文件(selinux) logging.properties 日誌log相關配置文件 server.xml 主配置文件 tomcat-users.xml manager-gui管理用戶配置文件(Tomcat安裝後生成的管理界面,該文件可開啓訪問) web.xml Tomcat的servlet,servlet-mapping,filter,MIME等相關配置 server.xml 主要配置文件,可修改啓動端口,設置網站根目錄,虛擬主機,開啓https等功能。 conf 存放Tomcat的各類全局配置文件,其中最主要的是server.xml和web.xml
yum install -y apr-devel openssl-devel gcc gcc-c++ cd $TOMCAT_HOME/bin tar -zxvf tomcat-native.tar.gz cd tomcat-native-1.2.16-src/native ./configure --with-apr=/usr/bin/apr-1-config --with-ssl=/usr --with-java-home=/usr/local/jdk make make install ln -s /usr/local/apr/lib/libtcnative* /usr/lib64/ 修改server.xml vim /home/tomcat8/conf/server.xml 把protocol改爲下面找個 <Connector port="8009" protocol="org.apache.coyote.ajp.AjpAprProtocol" redirectPort="8443" /> <Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol" connectionTimeout="20000" redirectPort="8443" /> 而後重啓tomcat,觀察日誌tailf /home/tomcat8/logs/catalina.out 2017-10-12 15:31:19,032 - Initializing ProtocolHandler ["http-apr-8080"] 2017-10-12 15:31:19,051 - Starting ProtocolHandler ["ajp-apr-8009"] 這樣就成功了
vim /home/tomcat8/bin/catalina.sh 編輯catalina.sh配置文件 [root@tomcat1 ~]# vim /home/tomcat/apache-tomcat-9.0.10/bin/catalina.sh # OS specific support. $var _must_ be set to either true or false.下面添加 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" 參數描述 -Xms 堆內存初始大小,單位m、g -Xmx 堆內存最大容許大小,通常不要大於物理內存的80% -XX:PermSize 非堆內存初始大小,通常應用設置初始化200m,最大1024m就夠了 -XX:MaxPermSize 非堆內存最大容許大小 -XX:+UseParallelGCThreads=8 並行收集器線程數,同時有多少個線程進行垃圾回收,通常與CPU數量相等 -XX:+UseParallelOldGC 指定老年代爲並行收集 -XX:+UseConcMarkSweepGC CMS收集器(併發收集器) -XX:+UseCMSCompactAtFullCollection 開啓內存空間壓縮和整理,防止過多內存碎片 查看網上說都是內存的1/2 #還有不少的參數,我也不甚瞭解,就不寫了
vim /home/tomcat8/conf/server.xml <!--The connectors can use a shared executor, you can define one or more named thread pools--> <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="1000" minSpareThreads="100"/>
Connector 參數優化配置javascript
<!--<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />--> <!-- A "Connector" using the shared thread pool-->
<Connector executor="tomcatThreadPool" port="8088" protocol="org.apache.coyote.http11.Http11Nio2Protocol" connectionTimeout="8000" maxConnections="10000" redirectPort="8443" enableLookups="false" acceptCount="100" maxPostSize="10485760" maxHttpHeaderSize="8192" compression="on" disableUploadTimeout="true" compressionMinSize="2048" acceptorThreadCount="2" compressableMimeType="text/html,text/plain,text/css,application/javascript,application/json,application/x-font-ttf,application/x-font-otf,image/svg+xml,image/jpeg,image/png,image/gif,audio/mpeg,video/mp4" URIEncoding="utf-8" processorCache="20000" tcpNoDelay="true" server="Server Version 1.0"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b %D" /> %D顯示爲毫秒 %T顯示爲秒 通常配置爲%D毫秒 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%{X-FORWARDED-FOR}i %l %u %t %r %s %b %D %q %{User-Agent}i %T" resolveHosts="false"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b %D" /> %D顯示爲毫秒 %T顯示爲秒 通常配置爲%D毫秒 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%{X-FORWARDED-FOR}i %l %u %t %r %s %b %D %q %{User-Agent}i %T" resolveHosts="false"/>
vim catalina.sh 第一步: 將 if [ -z "$CATALINA_OUT" ] ; then CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out fi 修改成 if [ -z "$CATALINA_OUT" ] ; then CATALINA_OUT="$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out fi 第二步: 將 touch "$CATALINA_OUT" 修改成 #touch "$CATALINA_OUT" 第三步: 將 org.apache.catalina.startup.Bootstrap "$@" start \ >> "$CATALINA_OUT" 2>&1 "&" 刪掉 增長如下 org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \ | /usr/sbin/cronolog "$CATALINA_OUT" >> /dev/null & 改上下兩處
cd /home/apache-tomcat-8.5.27/lib unzip catalina.jar
cd org/apache/catalina/util vim ServerInfo.properties server.info=Apache Tomcat server.number= server.built= 將修改後的信息壓縮回jar包 cd /home/apache-tomcat-8.5.27/lib jar uvf catalina.jar org/apache/catalina/util/ServerInfo.properties
vim /home/tomcat8/bin/catalina.sh CATALINA_HOME=/home/apache-tomcat-8.5.27 JAVA_HOME=/home/jdk1.8.0_181 直接在最前邊添加便可 #測試的時候沒有添加修改JAVA_HOME 和 CATALINA_HOME用systemctl 也能夠用暫時沒發現問題 vim /usr/lib/systemd/system/tomcat.service [Unit] Description=Tomcat After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/home/tomcat/bin/startup.sh ExecReload=/home/tomcat/bin/startup.sh ExecStop=/home/tomcat/bin/shutdown.sh [Install] WantedBy=multi-user.target 而後你就能夠用下面的命令了 systemctl start tomcat systemctl enable tomcat
telnet localhost 8005 而後輸入 SHUTDOWN 就能夠關閉 Tomcat,爲了安全咱們要修改(修改下8005端口)該功能 默認值: <Server port="8005" shutdown="SHUTDOWN"> 修改成: <Server port="port" shutdown="SHUTDOWN">
<Server port="-1" command="SHUTDOWN">
<filter> <filter-name>httpHeaderSecurity</filter-name> <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class> <init-param> <param-name>antiClickJackingOption</param-name> <param-value>SAMEORIGIN</param-value> </init-param> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>httpHeaderSecurity</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
方法一php
rm -rf webapps/ROOT mv project ROOT rm -rf project.war sh bin/shutdown.sh sh bin/startup.sh
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context debug="0" docBase="/home/tomcat/webapps/ROOT" path="" privileged="true" reloadable="true"/>
熱加載: 在server.xml -> context 屬性中 設置 reloadable="true"
熱部署: 在server.xml -> context 屬性中 設置 autoDeploy="true"
項目發佈目錄: 在server.xml docBase="/home/tomcat/webapps/ROOT"
vim autoDeploy.sh ####################JUST AS YOU NEED########################### outdir=ROOT tomcat_home=/home/tomcat #################### DO NOT MODIFY ################################## webapp_dir=${tomcat_home}/webapps if [ -z $1 ]; then echo 'enter the url: ' read url else url=$1 fi fname=`basename $url` echo $fname wget $url -P /tmp rm -rf $webapp_dir/$outdir/* unzip -q /tmp/$fname -d $webapp_dir/$outdir rm -f /tmp/$fname jid=`ps -ef | grep java | grep -v grep | awk '{print $2}'` kill -9 $jid #nohup $tomcat_home/bin/startup.sh >> /dev/null $tomcat_home/bin/startup.sh day=`date +%Y-%m-%d` catalinaLog="catalina."$day".log" /home/apps_deploy/tomcat_action.log tail -f ${tomcat_home}/logs/${catalinaLog}
#server.xml配置文件下: [root@tomcat1 ~]# vim /usr/local/tomcat8/conf/server.xml # <Connector executor="tomcatThreadPool"下面添加: 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" /> #參數說明: org.apache.coyote.http11.Http11NioProtocol:調整工做模式爲Nio maxThreads:最大線程數,默認150。增大值避免隊列請求過多,致使響應緩慢。 minSpareThreads:最小空閒線程數。 maxSpareThreads:最大空閒線程數,若是超過這個值,會關閉無用的線程。 acceptCount:當處理請求超過此值時,將後來請求放到隊列中等待。 disableUploadTimeout:禁用上傳超時時間 connectionTimeout:鏈接超時,單位毫秒,0表明不限制 URIEncoding:URI地址編碼使用UTF-8 enableLookups:關閉dns解析,提升響應時間 compression:啓用壓縮功能 compressionMinSize:最小壓縮大小,單位Byte compressableMimeType:壓縮的文件類型