Linux CentOS 6.x 開發配置文檔 3 - Tomcat

,下載:wget  http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.40/bin/apache-tomcat-7.0.40.tar.gz
    (MD5校驗方法: md5sum  apache-tomcat-7.0.37.tar.gz,看看顯示的dm5是否是與官方的同樣)

二,解壓:tar zxvf apache-tomcat-7.0.26.tar.gz

三,移動:mv apache-tomcat-7.0.26/* /home/Tomcat/

四,配置最簡潔的server.xml
<?xml version='1.0' encoding='utf-8'?>
    <Server port=" 8005" shutdown=" SHUTDOWN">
    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
    <Service name="Catalina">
        <Connector port=" 8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"  URIEncoding="UTF-8" />
        <Engine name="Catalina" defaultHost="localhost">
            <Host name="localhost" appBase="" unpackWARs="true" autoDeploy="true">
                 <Context path="" docBase="/home/Workspace/project/WebContent" reloadable="false" />
            </Host>
        </Engine>
    </Service>
</Server>
多端口配置:
<?xml version="1.0" encoding="utf-8"?>
<Server port=" 8005" shutdown=" SHUTDOWN">
    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>
    <Service name=" Catalina">
        <Connector port=" 80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"  URIEncoding="UTF-8" />
        <Engine name=" Catalina" defaultHost=" localhost">
            <Host name=" localhost" appBase="C:\Program Files\jiaoyan\groups\empty" unpackWARs="false" autoDeploy="false">
                 <Context path="" docBase="/home/Workspace/project1/WebContent" reloadable="false" />
            </Host>
        </Engine>
    </Service>
    <Service name=" Catalina1">
        <Connector port=" 8081" protocol="HTTP/1.1" redirectPort="8443" connectionTimeout="20000"  URIEncoding="UTF-8" />
        <Engine name=" Catalina1" defaultHost=" localhost1">
            <Host name=" www.abc.com" appBase="" xmlValidation="false" xmlNamespaceAware="false">
                 <Context path="" docBase="/home/Workspace/project2/WebContent" reloadable="false" />
            </Host>
        </Engine>
    </Service>
</Server>


五,將 Tomcat 做爲系統服務啓動:
        1,先在/etc/profile中添加:
                        export JAVA_HOME=/home/JDK
                        export PATH=$PATH:$JAVA_HOME/bin
                        export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

        2,編輯:vim /etc/rc.local:(個人rc.local中的內容以下:)
# 關閉防火牆
/etc/init.d/iptables stop

# 配置java
export JAVA_HOME=/home/JDK
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

# 啓動Tomcat
/home/Tomcat/bin/startup.sh


# 必須添加在下面代碼以前
touch /var/lock/subsys/local




六,增大Tomcat內存:
         0)Java 虛擬機內存介紹:
                1,Java 雖然是自動回收內存的,可是服務器上的應用程序最好根據本身的業務去分配內存,不然就會宕機;
                2,-Xms1024m,表示:JVM Heap(堆內存)的初始化值;
                3,
-Xmx1024m,表示:
JVM Heap(堆內存)的最大容許值,
                4,
PermSize 和  MaxPermSize:JVM 爲 Java 永久生成對象(Permanate generation),如:Java 中的 Class對象,方法對象這些可反射的對象分配的內存限制, 這些內存都不包括在 JVM Heap(堆內存)中
                5,經驗1: 
-Xmx 和   MaxPermSize  若是不設置或 設置太小的話,就會形成內存溢出(OutOfMemory),由於此錯誤來自JVM不是Throwable的,沒法用try...catch捕捉。
                6,經驗2:
-Xms 和  -Xmx 最好設置相同,  以免每次垃圾回收完成後JVM從新分配內存
                7,經驗3:
PermSize 和  MaxPermSize 也最好設置相同, 由於 PermSize 在不斷的變化中會須要轉移其中的數據。若是固定了之後,則能夠減小每次擴大  PermSize 帶來的性能損失;
 

         1)判斷Tomcat支持的最大內存:
                使用: java -Xms10240m -version,增長中間的值,直到出現:Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.

        2)For Linux:
                方法1,編輯catalina.sh,添加 :JAVA_OPTS="$JAVA_OPTS -Xms2048m -Xmx2048m -XX:PermSize=1024M -XX:MaxPermSize=1024m"
                方法2,在bin下新建setenv.sh,文件內容: JAVA_OPTS="-server -Xms2048M -Xmx2048M -Xss128k -XX:NewSize=600M -XX:MaxNewSize=600M -XX:MaxPermSize=512M -XX:+UseParallelGC"

============================================================================
參考:
CATALINA_OPTS='-Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.EPollSelectorProvider'
JAVA_OPTS= -Xms800M -Xmx1500M -XX:PermSize=128M -XX:MaxPermSize=300M
server.xml
<Connector port="7080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               redirectPort="7443" 
               compression="on"
               compressionMinSize="2048"
               noCompressionUserAgents="gozilla,traviata" 
               compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"/>
  ============================================================================
 
 
 

        3)For Windows 
-Xms1536m
-Xmx1536m
-XX:PermSize=512M
-XX:MaxPermSize=512m
-XX:ReservedCodeCacheSize=512m
-Duser.timezone=GMT+08
或:
-XX:PermSize=64M
-XX:MaxPermSize=192m
-XX:ReservedCodeCacheSize=48m


七,定時重啓 Tomcat:
        1,在home下新建: tomcat.sh
        2,驗證腳本是否正確: chmod 777 /home/tomcat.sh
        3,將該shell加到定時啓動服務中: crontab -e,此時會打開vim,輸入 進入INSERT模式,輸入:  0 6 * * * /home/tomcat.sh,不一樣字段間用 空格或tab鍵分隔,按Esc退出INSERT模式,再輸入: :wq!,保存退出。
        4,寫入成功的話,會出現:crontab: installing new crontab
        5,crond是系統的內置服務,寫了新任務以後須要重啓crond服務: service crond restart【參數:start, stop, restart, reload】
        6,查看系統中全部的任務調度: crontab -l,【查詢某用戶的:crontab -u root -l】【刪除某用戶的:crontab -u root -r】
        7,查看執行狀況: tail -100 /var/log/cron 
        8,查看執行日誌: tail -100 /var/spool/mail/root
        9,附上tomcat.sh:
#!/bin/sh
export JAVA_HOME=/home/JDK
echo ===== [Tomcat shutdown ...] =====
/home/Tomcat/bin/shutdown.sh
date
sleep 2m
ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs kill -9
sleep 1m
date
echo ===== [Tomcat start ...] =====
/home/Tomcat/bin/startup.sh

或:
#!/bin/sh
export JAVA_HOME=/home/JDK
pid = ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs
logs = /home/tomcat.log
echo ===== java pid:$pid !>> "$logs" 2>&1 =====
if [ -n "$pid" ]; then
    echo ===== [Tomcat shutdown ...] =====
    /home/Tomcat/bin/shutdown.sh
    date
    sleep 1m
    ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs kill -9
    date
    sleep 1m
    echo ===== [Tomcat start ...] =====
    /home/Tomcat/bin/startup.sh
else
    echo ===== [Tomcat is stoped ... Now Restart Tomcat ...] =====
    /home/Tomcat/bin/startup.sh
fi
============================================================================
解釋1:獲取Tomcat的java進程:ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs【或:ps -ef | grep -v grep | grep -w java | awk '{print $2}'】
解釋2:殺死java進程:ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs kill -9
解釋3:定時的表示方法:0-59表示分鐘,1-23表示小時,1-31表示日,1-12表示月,0-6表示星期(0是星期日)
解釋4:sleep後面的時間是:s表示秒,m表示分鐘,h表示小時,d表示日數
解釋5:-是區間、*是通配符、?是你不想設置那個字段


八,自動監控 Tomcat:
        思路1,使用系統的crond服務定時監控
        思路2,獲取Tomcat的java進程
        思路3,要監控的URL
        思路4,輸出日誌,最後能發郵件給監控的人
        思路5,若是沒有進程則重啓Tomcat
Shell腳本以下:(一邊寫,一邊驗證)
#!/bin/sh
export JAVA_HOME=/home/JDK
# 獲取Java進程
pid = ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs
# 輸入的日誌
logs = /home/tomcat.log
# 寫日誌
echo ===== java pid:$pid !>> "$logs" 2>&1 =====
# 須要監控的URL
url = http://wljy.jzedu.net/jvm.jsp






windows下刪除 Tomcat服務:sc delete Tomcat7



九,配置 APR:
        1,前言,Tomcat的三種運行模式: bionioaprbio是默認的模式,blocking IO,性能很是低下,沒有通過任何優化處理和支持;nio no blocking IO技術, 利用java的異步io護理技術,性能有初步的優化; apr是使用操做系統級別來解決異步的IO問題,能大幅度的提升性能。
        2,APR介紹:Tomcat可使用APR來提供超強的可伸縮性和性能,更好地集成本地服務器技術。APR(Apache Portable Runtime)是一個高可移植庫,它是Apache HTTP Server 2.x的核心。APR有不少用途,包括訪問高級IO功能(例如sendfile,epoll和OpenSSL),OS級別功能(隨機數生成,系統狀態等等),本地進程管理(共享內存,NT管道和UNIX sockets)。這些功能可使Tomcat做爲一個一般的前臺WEB服務器,能更好地和其它本地web技術集成,整體上讓Java更有效率做爲一個高性能web服務器平臺而不是簡單做爲後臺容器。  是Apache提供的一組通用的JNI接口,經過本地化的方法來提升應用與系統的交互,好比磁盤io,網絡io等。主要提升web容器(Tomcat,Jboss等)對靜態文件的處理性能,同時也能夠提升tomcat處理靜態資源的能力。 
        3,安裝前提:yum -y install openssl-devel,yum -y install gcc
        4,在apr的./configure時,出現:cannot remove libtoolt' no such file or directory,解決方法:編輯configure,在33008行,將$RM "$cfgfile"註釋
        5,安裝 apr
                1. wget  http://labs.renren.com/apache-mirror/apr/apr-1.4.6.tar.gz  
                2. tar zxvf apr-1.4.6.tar.gz  
                3. cd apr-1.4.6 
                4. ./configure  
                5. make  
                6. make install
        6,安裝 apr-iconv
                1. wget  http://mirror.bit.edu.cn/apache/apr/apr-iconv-1.2.1.tar.gz
                2. tar -zxvf apr-iconv-1.2.1.tar.gz
                3. cd apr-iconv-1.2.1
                4. ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
                5. make
                6. make install
        7,安裝 apr-util
                1. wget  http://apache.etoak.com/apr/apr-util-1.4.1.tar.gz 
                2. tar zxvf apr-util-1.4.1.tar.gz  
                3. cd apr-util-1.4.1 
                4. ./configure --with-apr=/usr/local/apr  
                5. make  
                6. make install
        8   ,安裝 tomcat-native(tomcat-native.tar.gz 在Tomcat/bin目錄下)
                1. cd /home/Tomcat/bin  
                2. tar zxvf tomcat-native.tar.gz  
                3. cd tomcat-native-1.1.22-src/jni/native  
                4. ./configure --with-apr=/usr/local/apr --with-java-home=/home/JDK
                5. make  
                6. make install
        9,設置 apr 的環境變量:
                1. gedit /etc/profile
                2. Add: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib  
                3. source /etc/profile
        10,啓動 tomcat 後, 看日誌:
                1. bin/startup.sh  
                2. head logs/catalina.out
                        重啓Tomcat以後,能夠看到如下結果:
                        四月 01, 2012 11:26:28 上午 org.apache.catalina.core.AprLifecycleListener init
                        信息:  Loaded APR based Apache Tomcat Native library 1.1.22.
                        四月 01, 2012 11:26:28 上午 org.apache.catalina.core.AprLifecycleListener init
                        信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
                        四月 01, 2012 11:26:29 上午 org.apache.coyote.AbstractProtocol init
                        信息: Initializing ProtocolHandler ["http- apr-8080"]
                        四月 01, 2012 11:26:29 上午 org.apache.coyote.AbstractProtocol init
                        信息: Initializing ProtocolHandler [" ajp-apr-8009"]
                        四月 01, 2012 11:26:29 上午 org.apache.catalina.startup.Catalina load
                        信息: Initialization processed in 1056 ms
                        四月 01, 2012 11:26:29 上午 org.apache.catalina.core.StandardService startInternal
                        信息: Starting service Catalina
                        四月 01, 2012 11:26:29 上午 org.apache.catalina.core.StandardEngine startInternal
                        信息: Starting Servlet Engine: Apache Tomcat/7.0.25
                        四月 01, 2012 11:26:29 上午 org.apache.coyote.AbstractProtocol start
                        信息: Starting ProtocolHandler ["http-apr-8080"]
                        四月 01, 2012 11:26:29 上午 org.apache.coyote.AbstractProtocol start
                        信息: Starting ProtocolHandler ["ajp-apr-8009"]
                        四月 01, 2012 11:26:29 上午 org.apache.catalina.startup.Catalina start
                        信息: Server startup in 187 ms
            11,更新 tomcat 後, 須要同時更新 APR:
                    1. 啓動後出現:  An older version 1.1.24 of the APR based Apache Tomcat Native library is installed ......
                    2. 解決方法:執行第8步:「安裝 tomcat-native......」
                    3.Windows下到 http://archive.apache.org/dist/tomcat/tomcat-connectors/native/相應版本/binaries/下的
tomcat-native-1.1.29-win32-bin.zip,解壓獲得tcnative-1.dll,將tcnative-1.dll放到JDK/bin下便可。



十,優化:
1,將logging.properties中的  FINE 和  INFO 都該成  ERROR
1catalina.org.apache.juli.FileHandler.level =  ERROR
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.

2localhost.org.apache.juli.FileHandler.level =  ERROR
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.

3manager.org.apache.juli.FileHandler.level =  ERROR
3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.FileHandler.prefix = manager.

4host-manager.org.apache.juli.FileHandler.level =  ERROR
4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.FileHandler.prefix = host-manager.

java.util.logging.ConsoleHandler.level =  ERROR
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter


############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level =  ERROR
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level =  ERROR
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.FileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level =  ERROR
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.FileHandler

# For example, set the org.apache.catalina.util.LifecycleBase logger to log
# each component that extends LifecycleBase changing state:
#org.apache.catalina.util.LifecycleBase.level = FINE

# To see debug messages in TldLocationsCache, uncomment the following line:
#org.apache.jasper.compiler.TldLocationsCache.level = FINE

2,作完第1步以後,logs下還有一個 catalina.out。此時修改 bin下的  catalina.sh,以Tomcat7.0.59爲例,將188行的
if [ -z "$CATALINA_OUT" ] ; then
  CATALINA_OUT= "$CATALINA_BASE"/logs/catalina.out
fi
修改成:
if [ -z "$CATALINA_OUT" ] ; then
  CATALINA_OUT= /dev/null fi
相關文章
相關標籤/搜索