tomcat-7.sh 6.1 KB

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