tomcat-9.sh 6.8 KB

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