本脚本install_tomcat.sh实现一键自动安装配置Tomcat,已在Ubuntu12.04上测试成功,可重复多次运行,脚本参照《安装Tomcat on Ubuntu12.04》(http://phenixikki.blog.51cto.com/blog/7572938/1344842)编写,在其他系统如CentOS不通用,需部分更改

使用注意事项:

<1> 如修改Tomcat访问端口,请修改# Tomcat Access Port下的内容

<2> 如修改管理账号及密码,请修改# Tomcat Admin下的内容(同时需修改判断条件)


#!/bin/bash
#
# Author: ikki
# Date: 2013-12-25
# Description: One-click Auto Install Tomcat ( on Ubuntu12.04)
# Version: 0.1
#
JAVAHOME=/etc/profile.d/java.sh
TOMCATDIR=/usr/local/tomcat
TOMCATSERV=/etc/init.d/tomcat
TOMCATVER=apache-tomcat-6.0.32
#
# Install OpenJDK
if dpkg -l openjdk-6-jre &>/dev/null; then
  echo "=== OpenJDK Installed ==="
else
  apt-get -y install openjdk-6-jre && echo "=== Install OpenJDK Finished ==="
  java -version
fi
#
# Set JAVA_HOME Environment Variable
echo "export JAVA_HOME=/usr/lib/jvm/java-6-openjdk-amd64/" > $JAVAHOME \
&& source $JAVAHOME \
#
# Install Tomcat
if [ ! -f $TOMCATVER.tar.gz ]; then
  wget http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.32/bin/$TOMCATVER.tar.gz
fi
[ ! -d $TOMCATVER ] && tar xf $TOMCATVER.tar.gz && cp -r $TOMCATVER $TOMCATDIR
chown -R root.root $TOMCATDIR && chmod -R 755 $TOMCATDIR
#
cat > $TOMCATSERV </dev/null || apt-get -y install sysv-rc-conf
sysv-rc-conf tomcat on
sysv-rc-conf --list tomcat
#
echo "=== Install Tomcat Finished ==="
#
# Modify Configuration
if [ `grep -o Xms $TOMCATDIR/bin/catalina.sh` == "" ]; then
  sed -i '2 a\JAVA_OPTS="-server -Xms1024m -Xmx1600m -XX:PermSize=256M -XX:MaxNewSize=256m -XX:MaxPermSize=256m -Djava.awt.headless=true"' $TOMCATDIR/bin/catalina.sh
fi
# Tomcat Access Port
sed -i 's/Connector port="8080"/Connector port="8081"/' $TOMCATDIR/conf/server.xml
# Tomcat Admin
if [ `grep -o test123 $TOMCATDIR/conf/tomcat-users.xml` == "" ]; then
 sed -i '$ i\  \n  \n  \n  ' $TOMCATDIR/conf/tomcat-users.xml
else
  echo "Files Modified"
fi
#
echo "=== Configuration Completed ==="
#
# Start Tomcat
/etc/init.d/tomcat start