Tomcat配置

1、Tomcat内存参数调整。

[root@localhost ~]# vim /usr/local/tomcat/bin/catalina.sh 
JAVA_OPTS="$JAVA_OPTS -server -Xmn1024M -Xms2096M -Xmx2096M \
-Djava.net.preferIPv4Stack=true -Djava.awt.headless=true \
-Dorg.apache.catalina.connector.RECYCLE_FACADES=false -XX:PermSize=512M \
-XX:MaxPermSize=512M  -XX:+UseConcMarkSweepGC -XX:+UseParNewGC \
-XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection \
-XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled \
-XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly  \
-XX:SurvivorRatio=65536 -XX:MaxTenuringThreshold=0 -XX:CMSInitiatingOccupancyFraction=81 \
-XX:SoftRefLRUPolicyMSPerMB=0 -Xloggc:gc.log -XX:+PrintGCDetails

2、安装tomcat-native以提升tomcat性能

        apr下载地址:http://apache.org/

[root@localhost~]#tar -zxvf apr-1.5.1.tar.gz
[root@localhost~]#cd apr-1.5.1
[[email protected]]#./configure --prefix=/usr/local/apr   #安装apr可移植运行库
[[email protected]]# make
[[email protected]]# make install
[root@localhost~]# cd /usr/local/tomcat/bin/
[root@localhostbin]# tar xf tomcat-native.tar.gz
[root@localhostbin]# cd tomcat-native-1.1.29-src/jni/native/
[root@localhostnative]# ./configure --with-apr=/usr/local/apr --with-java-home=/usr/local/java
[root@localhostnative]# make && make install
[root@localhost ~]# vim /etc/profile  #声明环境变量
export LD_LIBRARY_PATH=/usr/local/apr/lib


3、配置多实例Tomcat。

    Tomcat下载地址:http://tomcat.apache.org/

[root@localhost~]# tar xf apache-tomcat-7.0.47.tar.gz -C /usr/local   #解压tomcat到/usr/local目录
[root@localhost~]# cp -r /usr/local/apache-tomcat-7.0.47/ /usr/local/apache-tomcat-7.0.47-2  #配置Tomcat多实例
[root@localhost~]# mv /usr/local/apache-tomcat-7.0.47/ /usr/local/apache-tomcat-7.0.47-1
[root@localhost~]# ln -sv /usr/local/apache-tomcat-7.0.47-1/ /usr/local/tomcat1  #创建软连接,方便管理
[root@localhost~]# ln -sv /usr/local/apache-tomcat-7.0.47-2/ /usr/local/tomcat2
[root@localhost ~]# vim /etc/profile.d/custom.sh   #声明环境变量
CATALINA_HOME_1=/usr/local/tomcat1
CATALINA_HOME_2=/usr/local/tomcat2
JAVA_HOME=/usr/local/java
LD_LIBRARY_PATH=/usr/local/apr/lib
export CATALINA_HOME_1  CATALINA_HOME_2  JAVA_HOME  LD_LIBRARY_PATH
export PATH=$PATH:$JAVA_HOME/bin
[root@localhost ~]# . /etc/profile.d/custom.sh  #执行自定义变量脚本,使之生效
[root@localhost~]# vim /usr/local/tomcat1/bin/startup.sh #修改tomcat的启动和关闭脚本,分别指定tomcat实例家目录
export CATALINA_HOME=$CATALINA_HOME_1
[root@localhost ~]# vim/usr/local/tomcat1/bin/shutdown.sh
exportCATALINA_HOME=$CATALINA_HOME_1
[root@localhost ~]# vim/usr/local/tomcat2/bin/startup.sh
exportCATALINA_HOME=$CATALINA_HOME_2
[root@localhost ~]# vim /usr/local/tomcat2/bin/shutdown.sh
exportCATALINA_HOME=$CATALINA_HOME_2
[root@localhost ~]# vim /etc/init.d/tomcat1
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
JAVA_HOME=/usr/local/java
CATALINA_HOME=/usr/local/tomcat1
LD_LIBRARY_PATH=/usr/local/apr/lib
export JAVA_HOME CATALINA_HOME LD_LIBRARY_PATH
exec $CATALINA_HOME/bin/catalina.sh $*
[root@localhost ~]# vim /etc/init.d/tomcat2
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
JAVA_HOME=/usr/local/java
CATALINA_HOME=/usr/local/tomcat2
LD_LIBRARY_PATH=/usr/local/apr/lib
export JAVA_HOME CATALINA_HOME LD_LIBRARY_PATH
exec $CATALINA_HOME/bin/catalina.sh $*
[root@localhost~]# vim /usr/local/tomcat2/conf/server.xml   #修改tomcat2默认端口号
              …………
              <Server port="8006"shutdown="SHUTDOWN">  
                     …………
                     <Connectorport="8081" protocol="HTTP/1.1"
                            …………
                            <Connectorport="8010" protocol="AJP/1.3" redirectPort="8443"/>

4、配置redis session共享。

配置redis session共享,把以下3个jar包放到2个tomcat实例的库$CATALINA_HOME_1/lib/   $CATALINA_HOME_2/lib/
        
commons-pool-1.6.jar
jedis-2.1.0.jar
tomcat-redis-session-manager-1.1.jar
 
然后修改$CATALINA_HOME/conf/context.xml指定redis服务器路径
 
<ValveclassName="com.radiadesign.catalina.session.RedisSessionHandlerValve"/>
      <ManagerclassName="com.radiadesign.catalina.session.RedisSessionManager"database="0" host="192.168.1.1"maxInactiveInterval="1800" port="1234"/>

5、日志分割。

[root@localhost ~]# tar xf cronolog-1.6.2.tar.gz   #安装cron日志分割工具
[root@localhost ~]# cd cronolog-1.6.2
[root@localhost cronolog-1.6.2]# ./configure
[root@localhost cronolog-1.6.2]# make && make install
[root@localhost ~]# vim /usr/local/tomcat/bin/catalina.sh
#touch "$CATALINA_OUT"  #注释该行
org.apache.catalina.startup.Bootstrap "$@" start \
 |/usr/local/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out >> /dev/null &
 #>> "$CATALINA_OUT" 2>&1 "&"  #注释该行,添加如上一行
org.apache.catalina.startup.Bootstrap "$@" start \
 |/usr/local/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out >> /dev/null &
 #>> "$CATALINA_OUT" 2>&1 "&"  #注释该行,添加如上一行

6、主配置文件优化调整。

[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml 
    <Connector port="8080"
               executor="tomcatThreadPool" 
               protocol="HTTP/1.1" 
               acceptCount="3000"   #达到最大线程数后的排队数
               maxThreads="2000"    #最大线程数
               compression="on"
               enableLookups="false" 
               compressionMinSize="2048" 
               noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" 
               connectionTimeout="20000"
               redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="UTF-8"/>

7、添加Tomcat管理用户。

[root@localhost ~]# vim /usr/local/tomcat/conf/tomcat-users.xml 
<tomcat-users>
        <role rolename="admin-gui"/>
        <role rolename="admin-script"/>
        <role rolename="manager-gui"/>
        <role rolename="manager-script"/>
        <role rolename="manager-jmx"/>
        <role rolename="manager-status"/>
        <user username="admin" password="123456" roles="admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status"/>
</tomcat-users>


你可能感兴趣的:(tomcat)