2012-08-03

Tags: tomcat , java

這篇是快速配置 Tomcat7 的心得筆記。因為是快速配置,所以只寫重點部份,實務上如有其它需要,請自行查閱官方文件。


我的系統環境如下


設定 Tomcat 啟動時用到的 JVM 相關參數(java home path,gc parameter...etc)
  1. 在 Z:\apache-tomcat-7.0.29\bin 目錄裡,自行建立 setenv.bat 與 setenv.sh 。setenv.bat 是給 windows 使用,setenv.sh是給 linux 使用。
  2. setenv.bat 的設定可參考下例
    set JAVA_HOME=C:\Java\jdk1.7.0_05
    set JAVA_OPTS=-Xms64m -Xmx384m -XX:+UseParallelGC -Duser.timezone=Asia/Taipei
  3. setenv.sh 的設定可參考下例
    export JAVA_HOME="/usr/lib/jvm/java-7-sun-1.7.0_05"
    export JAVA_OPTS="-Xms64m -Xmx384m -XX:+UseParallelGC -Duser.timezone=Asia/Taipei"

設定 HTTP、HTTPS 連線相關參數
  1. 開啟  Z:\apache-tomcat-7.0.29\conf\server.xml
  2. 參考下例修改成符合自己所需的設定
    <!--
    port : The TCP port number on which this Connector will create a server socket and await incoming connections.
    protocol : Sets the protocol to handle incoming traffic.
    connectionTimeout : The number of milliseconds this Connector will wait.
    compression : The Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth.
    compressionMinSize : It is used to specify the minimum amount of data before the output is compressed.
    compressableMimeType : The value is a comma separated list of MIME types for which HTTP compression may be used.
    -->
    <Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443"
    compression="on"
    compressionMinSize="2048"
    compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css" />

    <!--
    port : The TCP port number on which this Connector will create a server socket and await incoming connections.
    protocol : Sets the protocol to handle incoming traffic.
    SSLEnabled : Use this attribute to enable SSL traffic on a connector.
    keystoreFile : The pathname of the keystore file where you have stored the server certificate to be loaded.
    keystorePass : The password used to access the specified keystore file.
    -->
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
    maxThreads="150" scheme="https" secure="true"
    clientAuth="false" sslProtocol="TLS"
    keystoreFile="${catalina.base}/conf/.keystore" keystorePass="keystore_password"/>
  3. 可參考官方文件,進行更仔細的設定

設定 MIME Type Mappings 相關參數
  1. 開啟  Z:\apache-tomcat-7.0.29\conf\web.xml
  2. 參考下例增加或修改所需的 MIME Type Mappings
    <mime-mapping>
    <extension>pdf</extension>
    <mime-type>application/pdf</mime-type>
    </mime-mapping>

設定 Tomcat 系統 Log 要記錄到哪個 level
  1. 開啟  Z:\apache-tomcat-7.0.29\conf\logging.properties
  2. 設定 Log level。官方文件的重點是這一句「A handler's log level threshold is INFO by default and can be set using SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST or ALL.」
  3. 可參考官方文件,進行更仔細的設定

Tomcat官方文件的 首頁在這裡,如果還有其它需要,可以去查相關使用方式。