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

你可能感兴趣的:(Linux CentOS 6.x 开发配置文档 3 - Tomcat)