tomcat-9.sh 7.1 KB

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