tomcat-8.sh 6.1 KB

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