前些日子手痒写了个一步安装JDK+TOMCAT的脚本,感觉不是很完善,没有充分发挥TOMCAT的性能,
这次花了点时间完善下了脚本:
1. 给TOMCAT自动加上APR支持和参数调优(我的脚本中给Xmx设置了一半的物理内存,因为我的机器不全跑WEB应用,童鞋们可以在脚本中自行修改),
2. 为了脚本的通用性,垃圾收集器新生代采用并行收集器-XX:+UseParallelGC,并设置了最大期望响应时间(单位毫秒)-XX:MaxGCPauseMillis=100,打开了-XX:+UseAdaptiveSizePolicy,
JVM会根据负载自动调整新生代中Eden区和survivor区的大小,永生代Perm区貌似也会自动调整,没有去改动它,
3. 旧生代也是并行收集器-XX:+UseParallelOldGC,默认新生代占最大堆-Xmx的3/8,旧生代占5/8
4. 脚本修改了hosts文件,并打开了tomcat的jmxremote,端口是1234,你可以在客户机用jconsole远程连接tomcat服务器机监控它的运行情况,并且打开了垃圾回收日志,放在tomcat的logs目录下,名为gc.log
5. 增大tomcat上传war包的文件大小至500MB,开机会自启动tomcat。
6.增加了一个启动tomcat或强行杀tomcat进程重启的脚本
本脚本允许重复执行多次,不影响执行结果,
--------------------------------------------------------------
脚本所需环境:
Linux开发环境,基本上带gcc,make,openssh,zlib,openssl,openssl-devel就行了(测试采用 CentOS 6.4 32位)
--------------------------------------------------------------
脚本所需软件包:
ORACLE JDK7 不限版本(测试采用 jdk-7u45-linux-i586.tar.gz)
TOMCAT7 不限版本(测试采用apache-tomcat-7.0.47.tar.gz)
apr 不限版本(测试采用apr-1.4.8.tar.gz)
apr-util 不限版本(测试采用apr-util-1.5.2.tar.gz)
tips:执行脚本时,这四个软件包和脚本需放在同一目录下
--------------------------------------------------------------
本脚本完成的一些功能:
备份原/etc/profile
备份原/etc/rc.local
备份原/etc/hosts
安装JDK
把JDK环境变量写入/etc/profile
安装tomcat
安装apr
安装apr-util
安装tomcat-native.tar.gz
把tomcat环境变量写入/etc/profile
把APR LD_LIBRARY_PATH环境变量写入/etc/profile
把tomcat自启动命令写入/etc/rc.local
给tomcat添加一个管理员,账号:tomcat;密码:tomcat
在tomcat的/bin/catalina.sh加上启动参数”CATALINA_OPTS······”
增大tomcat上传war包的文件大小至500MB(这大小总该够了)
执行完脚本后,手工执行一遍source /etc/profile让新环境变量生效
--------------------------------------------------------------
完整的脚本:
install_jdk_and_tomcat_with_apr.sh
#!/bin/sh
###### record your original path ######
echo "record your original path ......"
sleep 2
ORIGINALPATH=`pwd`
echo "your original path: ${ORIGINALPATH}"
sleep 2
###### set /etc/hosts for tomcat remote jmx ######
IPADDR=`ifconfig|awk 'NR==2{print $2}'|sed 's/addr://'`
HOSTNAME=`hostname`
echo "ipaddress: ${IPADDR}, hostname: ${HOSTNAME}"
sleep 2
if [ -e /etc/hosts.bak ];then
echo "restore original /etc/hosts.bak ......"
sleep 2
cp /etc/hosts.bak /etc/hosts
else
echo "backup original /etc/hosts.bak ......"
sleep 2
cp /etc/hosts /etc/hosts.bak
fi
sed -i "s/127.0.0.1/${IPADDR}/g" /etc/hosts
echo "${IPADDR} ${HOSTNAME}">>/etc/hosts
###### backup original /etc/profile ######
if [ -e /etc/profile.bak ];then
echo "restore original /etc/profile ......"
sleep 2
cp /etc/profile.bak /etc/profile
else
echo "backup original /etc/profile ......"
sleep 2
cp /etc/profile /etc/profile.bak
fi
###### backup original /etc/rc.local ######
if [ -e /etc/rc.local.bak ];then
echo "restore original /etc/rc.local ......"
sleep 2
cp /etc/rc.local.bak /etc/rc.local
else
echo "backup original /etc/rc.local ......"
sleep 2
cp /etc/rc.local /etc/rc.local.bak
fi
###### install jdk ######
if [ -e /usr/java ];then
echo "deleting old or existed jdk ......"
sleep 2
rm -rf /usr/java
echo "installing jdk ......"
mkdir /usr/java
sleep 2
tar zxvf jdk* -C /usr/java
else
echo "installing jdk ......"
sleep 2
mkdir /usr/java
tar zxvf jdk* -C /usr/java
fi
###### write JDK environment into /etc/profile ######
JDKPATH=`ls /usr/java`
echo ${JDKPATH}
echo "write JDK environment into /etc/profile ......"
sleep 2
echo " ">>/etc/profile
echo "# jdk environment">>/etc/profile
echo "JAVA_HOME=/usr/java/${JDKPATH}">>/etc/profile
echo 'PATH=$JAVA_HOME/bin:$PATH'>>/etc/profile
echo 'CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar'>>/etc/profile
echo 'export JAVA_HOME'>>/etc/profile
echo 'export PATH'>>/etc/profile
echo 'export CLASSPATH'>>/etc/profile
###### install tomcat ######
if [ -e /usr/local/apache-tomcat* ];then
echo "deleting old or existed tomcat ......"
sleep 2
rm -rf /usr/local/apache-tomcat*
echo "installing tomcat ......"
sleep 2
tar zxvf apache-tomcat* -C /usr/local
else
echo "installing tomcat ......"
sleep 2
tar zxvf apache-tomcat* -C /usr/local
fi
###### install apr ######
if [ -e /usr/local/apr ];then
echo "deleting old or existed apr ......"
sleep 2
rm -rf /usr/local/apr
echo "installing apr ......"
sleep 2
tar zxvf apr-1* -C /tmp
cd /tmp/apr-1*
./configure
make
make install
else
echo "installing apr ......"
sleep 2
tar zxvf apr-1* -C /tmp
cd /tmp/apr-1*
./configure
make
make install
fi
###### install apr-util ######
cd ${ORIGINALPATH}
echo "installing apr-util ......"
sleep 2
tar zxvf apr-util* -C /tmp
cd /tmp/apr-util*
./configure --with-apr=/usr/local/apr
make
make install
echo "deleting /tmp/apr* ......"
sleep 2
rm -rf /tmp/apr*
###### install tomcat-native.tar.gz ######
cd /usr/local/apache-tomcat*/bin
echo "installing tomcat-native ......"
sleep 2
tar zxvf tomcat-native.tar.gz
cd tomcat-native-*/jni/native
./configure --with-apr=/usr/local/apr --with-java-home=/usr/java/${JDKPATH} --with-ssl=yes
make
make install
###### write tomcat environment into /etc/profile ######
echo "write tomcat environment into /etc/profile ......"
sleep 2
cd /usr/local/apache-tomcat*
TOMCATPATH=`pwd`
echo "tomcat home path: ${TOMCATPATH}"
echo " ">>/etc/profile
echo "# tomcat environment">>/etc/profile
echo "TOMCAT_HOME=${TOMCATPATH}">>/etc/profile
echo 'PATH=$TOMCAT_HOME/bin:$PATH'>>/etc/profile
###### write apr environment into /etc/profile ######
echo "write apr environment into /etc/profile ......"
sleep 2
echo " ">>/etc/profile
echo "# apr environment">>/etc/profile
echo 'LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib'>>/etc/profile
echo 'export LD_LIBRARY_PATH'>>/etc/profile
###### write tomcat anto startup.sh into /etc/rc.local ######
echo "write tomcat anto startup.sh into /etc/rc.local ......"
sleep 2
echo "source /etc/profile">>/etc/rc.local
echo "${TOMCATPATH}/bin/startup.sh">>/etc/rc.local
###### add username : tomcat password : tomcat into tomcat-users.xml ######
echo "backup original tomcat-users.xml ......"
sleep 2
cp ${TOMCATPATH}/conf/tomcat-users.xml ${TOMCATPATH}/conf/tomcat-users.xml.bak
echo "add username : tomcat password : tomcat into tomcat-users.xml ......"
sleep 2
echo '<tomcat-users>'>${TOMCATPATH}/conf/tomcat-users.xml
echo '<role rolename="admin-gui"/>'>>${TOMCATPATH}/conf/tomcat-users.xml
echo '<role rolename="manager-gui"/>'>>${TOMCATPATH}/conf/tomcat-users.xml
echo '<user username="tomcat" password="tomcat" roles="admin-gui,manager-gui"/>'>>${TOMCATPATH}/conf/tomcat-users.xml
echo '</tomcat-users>'>>${TOMCATPATH}/conf/tomcat-users.xml
###### set tomcat JAVA_OPTS ######
echo "backup original catalina.sh ......"
sleep 2
cp ${TOMCATPATH}/bin/catalina.sh ${TOMCATPATH}/bin/catalina.sh.bak
MEMORY=`free -m | awk 'NR==2 {print $2}'`
HALFMEM=$(expr ${MEMORY} / 2)
QUARTERMEM=$(expr ${MEMORY} / 4)
PERMMEM=$(expr ${MEMORY} / 8)
echo "physical memory : ${MEMORY} MB"
sleep 2
echo "set tomcat CATALINA_OPTS ......"
sleep 2
sed -i "2i\ " ${TOMCATPATH}/bin/catalina.sh
sed -i "3i\CATALINA_OPTS=\"-Djava.awt.headless=true -Dfile.encoding=UTF-8 " ${TOMCATPATH}/bin/catalina.sh
sed -i "4i\ -server -Xms${HALFMEM}m -Xmx${HALFMEM}m -Xss256k" ${TOMCATPATH}/bin/catalina.sh
sed -i "5i\ -XX:+UseParallelGC -XX:MaxGCPauseMillis=100 -XX:+UseAdaptiveSizePolicy -XX:+UseParallelOldGC" ${TOMCATPATH}/bin/catalina.sh
#sed -i "5i\ -XX:PermSize=${PERMMEM}m -XX:MaxPermSize=${PERMMEM}m " ${TOMCATPATH}/bin/catalina.sh
#sed -i "6i\ -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled " ${TOMCATPATH}/bin/catalina.sh
#sed -i "7i\ -XX:+UseCMSCompactAtFullCollection -XX:+UseFastAccessorMethods" ${TOMCATPATH}/bin/catalina.sh
###### pring gc log ######
sed -i "6i\ -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:${TOMCATPATH}/logs/gc.log " ${TOMCATPATH}/bin/catalina.sh
sed -i "7i\ -XX:+DisableExplicitGC " ${TOMCATPATH}/bin/catalina.sh
###### open tomcat jmx port ######
sed -i "8i\ -Dcom.sun.management.jmxremote.port=1234 " ${TOMCATPATH}/bin/catalina.sh
sed -i "9i\ -Dcom.sun.management.jmxremote.ssl=false " ${TOMCATPATH}/bin/catalina.sh
sed -i "10i\ -Dcom.sun.management.jmxremote.authenticate=false \"" ${TOMCATPATH}/bin/catalina.sh
###### set tomcat max-file-size ######
echo "backup original webapps/manager/WEB-INF/web.xml ......"
sleep 2
cp ${TOMCATPATH}/webapps/manager/WEB-INF/web.xml ${TOMCATPATH}/webapps/manager/WEB-INF/web.xml.bak
echo "set tomcat max-file-size ......"
sleep 2
sed -i 's/50MB/500MB/g' ${TOMCATPATH}/webapps/manager/WEB-INF/web.xml
sed -i 's/52428800/524288000/g' ${TOMCATPATH}/webapps/manager/WEB-INF/web.xml
###### cp reboot_tomcat_force.sh into ${TOMCAT_HOME}/bin ######
cd ${ORIGINALPATH}
if [ -e reboot_tomcat_violently.sh ];then
echo 'file reboot_tomcat_violently.sh existed and copy it into ${TOMCAT_HOME}/bin/ ......'
sleep 2
cp reboot_tomcat_violently.sh ${TOMCATPATH}/bin/
cd ${TOMCATPATH}/bin
echo "creat soft link rbt.sh to reboot_tomcat_violently.sh ......"
sleep 2
ln -s reboot_tomcat_violently.sh rbt.sh
else
echo "file reboot_tomcat_violently.sh is not found ......"
sleep 2
fi
###### echo tips ######
echo ""
echo "tips: if you want the new environment variable to take effect ,don't forget execute"
echo "source /etc/profile"
echo "manually!!!"
#source /etc/profile
reboot_tomcat_violently.sh
#!/bin/sh
source /etc/profile
if [ `ps -ef|grep java|awk '$3==1 {print $2}'`>1 ]; then
echo "tomcat pid: `ps -ef|grep java|awk '$3==1 {print $2}'` "
echo "kill tomcat ......"
sleep 2
kill -9 `ps -ef|grep java|awk '$3==1 {print $2}'`
echo "start tomcat ......"
sleep 2
startup.sh
tail -f ${TOMCAT_HOME}/logs/catalina.out
else
echo "tomcat is not running yet, start tomcat ......"
startup.sh
tail -f ${TOMCAT_HOME}/logs/catalina.out
fi
来源:oschina
链接:https://my.oschina.net/u/919487/blog/175963