tomcat-7.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # http://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. Install_tomcat-7()
  11. {
  12. cd $oneinstack_dir/src
  13. . /etc/profile
  14. src_url=http://mirrors.linuxeye.com/apache/tomcat/v$tomcat_7_version/apache-tomcat-$tomcat_7_version.tar.gz && Download_src
  15. src_url=http://mirrors.linuxeye.com/apache/tomcat/v$tomcat_7_version/catalina-jmx-remote.jar && Download_src
  16. id -u $run_user >/dev/null 2>&1
  17. [ $? -ne 0 ] && useradd -M -s /bin/bash $run_user || { [ -z "`grep ^$run_user /etc/passwd | grep '/bin/bash'`" ] && usermod -s /bin/bash $run_user; }
  18. tar xzf apache-tomcat-$tomcat_7_version.tar.gz
  19. [ ! -d "$tomcat_install_dir" ] && mkdir -p $tomcat_install_dir
  20. /bin/cp -R apache-tomcat-$tomcat_7_version/* $tomcat_install_dir
  21. if [ -e "$tomcat_install_dir/conf/server.xml" ];then
  22. /bin/cp catalina-jmx-remote.jar $tomcat_install_dir/lib
  23. cd $tomcat_install_dir/lib
  24. [ ! -d "$tomcat_install_dir/lib/catalina" ] && mkdir $tomcat_install_dir/lib/catalina
  25. cd $tomcat_install_dir/lib/catalina
  26. jar xf ../catalina.jar
  27. sed -i 's@^server.info=.*@server.info=Tomcat@' org/apache/catalina/util/ServerInfo.properties
  28. sed -i 's@^server.number=.*@server.number=7@' org/apache/catalina/util/ServerInfo.properties
  29. sed -i "s@^server.built=.*@server.built=`date`@" org/apache/catalina/util/ServerInfo.properties
  30. jar cf ../catalina.jar ./*
  31. cd ../../bin
  32. rm -rf $tomcat_install_dir/lib/catalina
  33. OS_CentOS='yum -y install apr apr-devel'
  34. OS_Debian_Ubuntu='apt-get -y install libapr1-dev libaprutil1-dev'
  35. OS_command
  36. tar xzf tomcat-native.tar.gz
  37. cd tomcat-native-*-src/jni/native/
  38. rm -rf /usr/local/apr
  39. ./configure --with-apr=/usr/bin/apr-1-config
  40. make && make install
  41. if [ -d "/usr/local/apr/lib" ];then
  42. [ $Mem -le 768 ] && Xms_Mem=`expr $Mem / 3` || Xms_Mem=256
  43. cat > $tomcat_install_dir/bin/setenv.sh << EOF
  44. JAVA_OPTS='-server -Xms${Xms_Mem}m -Xmx`expr $Mem / 2`m'
  45. CATALINA_OPTS="-Djava.library.path=/usr/local/apr/lib"
  46. # -Djava.rmi.server.hostname=$IPADDR
  47. # -Dcom.sun.management.jmxremote.password.file=\$CATALINA_BASE/conf/jmxremote.password
  48. # -Dcom.sun.management.jmxremote.access.file=\$CATALINA_BASE/conf/jmxremote.access
  49. # -Dcom.sun.management.jmxremote.ssl=false"
  50. EOF
  51. cd ../../../;rm -rf tomcat-native-*
  52. chmod +x $tomcat_install_dir/bin/*.sh
  53. /bin/mv $tomcat_install_dir/conf/server.xml{,_bk}
  54. cd $oneinstack_dir/src
  55. /bin/cp ../config/server.xml $tomcat_install_dir/conf
  56. sed -i "s@/usr/local/tomcat@$tomcat_install_dir@g" $tomcat_install_dir/conf/server.xml
  57. [ ! -d "$tomcat_install_dir/conf/vhost" ] && mkdir $tomcat_install_dir/conf/vhost
  58. cat > $tomcat_install_dir/conf/vhost/localhost.xml << EOF
  59. <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
  60. <Context path="" docBase="$wwwroot_dir/default" debug="0" reloadable="false" crossContext="true"/>
  61. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  62. prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  63. </Host>
  64. EOF
  65. # logrotate tomcat catalina.out
  66. cat > /etc/logrotate.d/tomcat << EOF
  67. $tomcat_install_dir/logs/catalina.out {
  68. daily
  69. rotate 5
  70. missingok
  71. dateext
  72. compress
  73. notifempty
  74. copytruncate
  75. }
  76. EOF
  77. [ -z "`grep '<user username="admin" password=' $tomcat_install_dir/conf/tomcat-users.xml`" ] && sed -i "s@^</tomcat-users>@<role rolename=\"admin-gui\"/>\n<role rolename=\"admin-script\"/>\n<role rolename=\"manager-gui\"/>\n<role rolename=\"manager-script\"/>\n<user username=\"admin\" password=\"`cat /dev/urandom | head -1 | md5sum | head -c 10`\" roles=\"admin-gui,admin-script,manager-gui,manager-script\"/>\n</tomcat-users>@" $tomcat_install_dir/conf/tomcat-users.xml
  78. cat > $tomcat_install_dir/conf/jmxremote.access << EOF
  79. monitorRole readonly
  80. controlRole readwrite \
  81. create javax.management.monitor.*,javax.management.timer.* \
  82. unregister
  83. EOF
  84. cat > $tomcat_install_dir/conf/jmxremote.password << EOF
  85. monitorRole `cat /dev/urandom | head -1 | md5sum | head -c 8`
  86. # controlRole R&D
  87. EOF
  88. chown -R $run_user.$run_user $tomcat_install_dir
  89. /bin/cp ../init.d/Tomcat-init /etc/init.d/tomcat
  90. sed -i "s@JAVA_HOME=.*@JAVA_HOME=$JAVA_HOME@" /etc/init.d/tomcat
  91. sed -i "s@^CATALINA_HOME=.*@CATALINA_HOME=$tomcat_install_dir@" /etc/init.d/tomcat
  92. sed -i "s@^TOMCAT_USER=.*@TOMCAT_USER=$run_user@" /etc/init.d/tomcat
  93. OS_CentOS='chkconfig --add tomcat \n
  94. chkconfig tomcat on'
  95. OS_Debian_Ubuntu='update-rc.d tomcat defaults'
  96. OS_command
  97. echo "${CSUCCESS}Tomcat install successfully! ${CEND}"
  98. fi
  99. else
  100. rm -rf $tomcat_install_dir
  101. echo "${CFAILURE}Tomcat install failed, Please contact the author! ${CEND}"
  102. kill -9 $$
  103. fi
  104. cd ..
  105. }