tomcat-7.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. # https://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. Install_tomcat-7() {
  11. pushd ${oneinstack_dir}/src
  12. . /etc/profile
  13. id -u ${run_user} >/dev/null 2>&1
  14. [ $? -ne 0 ] && useradd -M -s /bin/bash ${run_user} || { [ -z "$(grep ^${run_user} /etc/passwd | grep '/bin/bash')" ] && usermod -s /bin/bash ${run_user}; }
  15. tar xzf apache-tomcat-${tomcat_7_version}.tar.gz
  16. [ ! -d "${tomcat_install_dir}" ] && mkdir -p ${tomcat_install_dir}
  17. /bin/cp -R apache-tomcat-${tomcat_7_version}/* ${tomcat_install_dir}
  18. rm -rf ${tomcat_install_dir}/webapps/{docs,examples,host-manager,manager,ROOT/*}
  19. if [ ! -e "${tomcat_install_dir}/conf/server.xml" ]; then
  20. rm -rf ${tomcat_install_dir}
  21. echo "${CFAILURE}Tomcat install failed, Please contact the author! ${CEND}"
  22. kill -9 $$
  23. fi
  24. /bin/cp catalina-jmx-remote.jar ${tomcat_install_dir}/lib
  25. [ ! -d "${tomcat_install_dir}/lib/catalina" ] && mkdir ${tomcat_install_dir}/lib/catalina
  26. pushd ${tomcat_install_dir}/lib/catalina
  27. jar xf ../catalina.jar
  28. sed -i 's@^server.info=.*@server.info=Tomcat@' org/apache/catalina/util/ServerInfo.properties
  29. sed -i 's@^server.number=.*@server.number=7@' org/apache/catalina/util/ServerInfo.properties
  30. sed -i "s@^server.built=.*@server.built=$(date)@" org/apache/catalina/util/ServerInfo.properties
  31. jar cf ../catalina.jar ./*
  32. popd
  33. rm -rf ${tomcat_install_dir}/lib/catalina
  34. [ "${OS}" == "CentOS" ] && yum -y install apr apr-devel
  35. [[ "${OS}" =~ ^Ubuntu$|^Debian$ ]] && apt-get -y install libapr1-dev libaprutil1-dev
  36. pushd ${tomcat_install_dir}/bin
  37. tar xzf tomcat-native.tar.gz
  38. pushd tomcat-native-*-src/jni/native/
  39. rm -rf /usr/local/apr
  40. ./configure --with-apr=/usr/bin/apr-1-config
  41. make -j ${THREAD} && make install
  42. popd
  43. rm -rf tomcat-native-*
  44. if [ -d "/usr/local/apr/lib" ]; then
  45. [ ${Mem} -le 768 ] && let Xms_Mem="${Mem}/3" || Xms_Mem=256
  46. let XmxMem="${Mem}/2"
  47. cat > ${tomcat_install_dir}/bin/setenv.sh << EOF
  48. JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms${Xms_Mem}m -Xmx${XmxMem}m -Dfile.encoding=UTF-8'
  49. CATALINA_OPTS="-Djava.library.path=/usr/local/apr/lib"
  50. # -Djava.rmi.server.hostname=$IPADDR
  51. # -Dcom.sun.management.jmxremote.password.file=\$CATALINA_BASE/conf/jmxremote.password
  52. # -Dcom.sun.management.jmxremote.access.file=\$CATALINA_BASE/conf/jmxremote.access
  53. # -Dcom.sun.management.jmxremote.ssl=false"
  54. EOF
  55. chmod +x ./*.sh
  56. /bin/mv ${tomcat_install_dir}/conf/server.xml{,_bk}
  57. popd # goto ${oneinstack_dir}/src
  58. /bin/cp ${oneinstack_dir}/config/server.xml ${tomcat_install_dir}/conf
  59. sed -i "s@/usr/local/tomcat@${tomcat_install_dir}@g" ${tomcat_install_dir}/conf/server.xml
  60. if [ ! -e "${nginx_install_dir}/sbin/nginx" -a ! -e "${tengine_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" ]; then
  61. if [ "${OS}" == "CentOS" ]; then
  62. if [ -z "$(grep -w '8080' /etc/sysconfig/iptables)" ]; then
  63. iptables -I INPUT 5 -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
  64. service iptables save
  65. fi
  66. elif [[ "${OS}" =~ ^Ubuntu$|^Debian$ ]]; then
  67. if [ -z "$(grep -w '8080' /etc/iptables.up.rules)" ]; then
  68. iptables -I INPUT 5 -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
  69. iptables-save > /etc/iptables.up.rules
  70. fi
  71. fi
  72. fi
  73. [ ! -d "${tomcat_install_dir}/conf/vhost" ] && mkdir ${tomcat_install_dir}/conf/vhost
  74. cat > ${tomcat_install_dir}/conf/vhost/localhost.xml << EOF
  75. <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
  76. <Context path="" docBase="${wwwroot_dir}/default" debug="0" reloadable="false" crossContext="true"/>
  77. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  78. prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  79. </Host>
  80. EOF
  81. # logrotate tomcat catalina.out
  82. cat > /etc/logrotate.d/tomcat << EOF
  83. ${tomcat_install_dir}/logs/catalina.out {
  84. daily
  85. rotate 5
  86. missingok
  87. dateext
  88. compress
  89. notifempty
  90. copytruncate
  91. }
  92. EOF
  93. [ -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
  94. cat > ${tomcat_install_dir}/conf/jmxremote.access << EOF
  95. monitorRole readonly
  96. controlRole readwrite \
  97. create javax.management.monitor.*,javax.management.timer.* \
  98. unregister
  99. EOF
  100. cat > ${tomcat_install_dir}/conf/jmxremote.password << EOF
  101. monitorRole $(cat /dev/urandom | head -1 | md5sum | head -c 8)
  102. # controlRole R&D
  103. EOF
  104. chown -R ${run_user}.${run_user} ${tomcat_install_dir}
  105. /bin/cp ${oneinstack_dir}/init.d/Tomcat-init /etc/init.d/tomcat
  106. sed -i "s@JAVA_HOME=.*@JAVA_HOME=${JAVA_HOME}@" /etc/init.d/tomcat
  107. sed -i "s@^CATALINA_HOME=.*@CATALINA_HOME=${tomcat_install_dir}@" /etc/init.d/tomcat
  108. sed -i "s@^TOMCAT_USER=.*@TOMCAT_USER=${run_user}@" /etc/init.d/tomcat
  109. [ "${OS}" == "CentOS" ] && { chkconfig --add tomcat; chkconfig tomcat on; }
  110. [[ "${OS}" =~ ^Ubuntu$|^Debian$ ]] && update-rc.d tomcat defaults
  111. echo "${CSUCCESS}Tomcat installed successfully! ${CEND}"
  112. rm -rf apache-tomcat-${tomcat_7_version}
  113. else
  114. popd
  115. echo "${CFAILURE}Tomcat install failed, Please contact the author! ${CEND}"
  116. fi
  117. service tomcat start
  118. popd
  119. }