apache-2.4.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_Apache24() {
  11. pushd ${oneinstack_dir}/src
  12. tar xzf pcre-${pcre_version}.tar.gz
  13. pushd pcre-${pcre_version}
  14. ./configure
  15. make -j ${THREAD} && make install
  16. popd
  17. id -u ${run_user} >/dev/null 2>&1
  18. [ $? -ne 0 ] && useradd -M -s /sbin/nologin $run_user
  19. tar xzf httpd-${apache24_version}.tar.gz
  20. tar xzf nghttp2-${nghttp2_version}.tar.gz
  21. tar xzf apr-${apr_version}.tar.gz
  22. tar xzf apr-util-${apr_util_version}.tar.gz
  23. # install nghttp2
  24. if [ ! -e "/usr/local/lib/libnghttp2.so" ]; then
  25. pushd nghttp2-${nghttp2_version}
  26. ./configure
  27. make -j ${THREAD} && make install
  28. popd
  29. echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf; ldconfig
  30. rm -rf nghttp2-${nghttp2_version}
  31. fi
  32. pushd httpd-${apache24_version}
  33. [ ! -d "${apache_install_dir}" ] && mkdir -p ${apache_install_dir}
  34. /bin/cp -R ../apr-${apr_version} ./srclib/apr
  35. /bin/cp -R ../apr-util-${apr_util_version} ./srclib/apr-util
  36. LDFLAGS=-ldl LD_LIBRARY_PATH=${openssl_install_dir}/lib ./configure --prefix=${apache_install_dir} --with-mpm=prefork --with-included-apr --enable-headers --enable-deflate --enable-so --enable-dav --enable-rewrite --enable-ssl --with-ssl=${openssl_install_dir} --enable-http2 --with-nghttp2=/usr/local --enable-expires --enable-static-support --enable-suexec --enable-modules=all --enable-mods-shared=all
  37. make -j ${THREAD} && make install
  38. unset LDFLAGS
  39. if [ -e "${apache_install_dir}/conf/httpd.conf" ]; then
  40. echo "${CSUCCESS}Apache installed successfully! ${CEND}"
  41. popd
  42. rm -rf httpd-${apache24_version}
  43. else
  44. rm -rf ${apache_install_dir}
  45. echo "${CFAILURE}Apache install failed, Please contact the author! ${CEND}"
  46. kill -9 $$
  47. fi
  48. [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=${apache_install_dir}/bin:\$PATH" >> /etc/profile
  49. [ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep ${apache_install_dir} /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=${apache_install_dir}/bin:\1@" /etc/profile
  50. . /etc/profile
  51. /bin/cp ${apache_install_dir}/bin/apachectl /etc/init.d/httpd
  52. sed -i '2a # chkconfig: - 85 15' /etc/init.d/httpd
  53. sed -i '3a # description: Apache is a World Wide Web server. It is used to serve' /etc/init.d/httpd
  54. chmod +x /etc/init.d/httpd
  55. [ "$OS" == 'CentOS' ] && { chkconfig --add httpd; chkconfig httpd on; }
  56. [[ $OS =~ ^Ubuntu$|^Debian$ ]] && update-rc.d httpd defaults
  57. sed -i "s@^User daemon@User $run_user@" ${apache_install_dir}/conf/httpd.conf
  58. sed -i "s@^Group daemon@Group $run_user@" ${apache_install_dir}/conf/httpd.conf
  59. if [ "${Nginx_version}" == '4' -a ! -e "${web_install_dir}/sbin/nginx" ]; then
  60. sed -i 's/^#ServerName www.example.com:80/ServerName 0.0.0.0:80/' ${apache_install_dir}/conf/httpd.conf
  61. TMP_PORT=80
  62. elif [[ ${Nginx_version} =~ ^[1-3]$ ]] || [ -e "${web_install_dir}/sbin/nginx" ]; then
  63. sed -i 's/^#ServerName www.example.com:80/ServerName 127.0.0.1:88/' ${apache_install_dir}/conf/httpd.conf
  64. sed -i 's@^Listen.*@Listen 127.0.0.1:88@' ${apache_install_dir}/conf/httpd.conf
  65. TMP_PORT=88
  66. fi
  67. sed -i "s@AddType\(.*\)Z@AddType\1Z\n AddType application/x-httpd-php .php .phtml\n AddType application/x-httpd-php-source .phps@" ${apache_install_dir}/conf/httpd.conf
  68. sed -i "s@#AddHandler cgi-script .cgi@AddHandler cgi-script .cgi .pl@" ${apache_install_dir}/conf/httpd.conf
  69. sed -ri 's@^#(.*mod_suexec.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  70. sed -ri 's@^#(.*mod_vhost_alias.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  71. sed -ri 's@^#(.*mod_rewrite.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  72. sed -ri 's@^#(.*mod_deflate.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  73. sed -ri 's@^#(.*mod_expires.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  74. sed -ri 's@^#(.*mod_ssl.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  75. sed -ri 's@^#(.*mod_http2.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  76. sed -i 's@DirectoryIndex index.html@DirectoryIndex index.html index.php@' ${apache_install_dir}/conf/httpd.conf
  77. sed -i "s@^DocumentRoot.*@DocumentRoot \"$wwwroot_dir/default\"@" ${apache_install_dir}/conf/httpd.conf
  78. sed -i "s@^<Directory \"${apache_install_dir}/htdocs\">@<Directory \"$wwwroot_dir/default\">@" ${apache_install_dir}/conf/httpd.conf
  79. sed -i "s@^#Include conf/extra/httpd-mpm.conf@Include conf/extra/httpd-mpm.conf@" ${apache_install_dir}/conf/httpd.conf
  80. #logrotate apache log
  81. cat > /etc/logrotate.d/apache << EOF
  82. $wwwlogs_dir/*apache.log {
  83. daily
  84. rotate 5
  85. missingok
  86. dateext
  87. compress
  88. notifempty
  89. sharedscripts
  90. postrotate
  91. [ -e /var/run/httpd.pid ] && kill -USR1 \`cat /var/run/httpd.pid\`
  92. endscript
  93. }
  94. EOF
  95. mkdir ${apache_install_dir}/conf/vhost
  96. cat > ${apache_install_dir}/conf/vhost/0.conf << EOF
  97. <VirtualHost *:$TMP_PORT>
  98. ServerAdmin admin@linuxeye.com
  99. DocumentRoot "$wwwroot_dir/default"
  100. ServerName 127.0.0.1
  101. ErrorLog "$wwwlogs_dir/error_apache.log"
  102. CustomLog "$wwwlogs_dir/access_apache.log" common
  103. <Directory "$wwwroot_dir/default">
  104. SetOutputFilter DEFLATE
  105. Options FollowSymLinks ExecCGI
  106. Require all granted
  107. AllowOverride All
  108. Order allow,deny
  109. Allow from all
  110. DirectoryIndex index.html index.php
  111. </Directory>
  112. <Location /server-status>
  113. SetHandler server-status
  114. Order Deny,Allow
  115. Deny from all
  116. Allow from 127.0.0.1
  117. </Location>
  118. </VirtualHost>
  119. EOF
  120. cat >> ${apache_install_dir}/conf/httpd.conf <<EOF
  121. <IfModule mod_headers.c>
  122. AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
  123. <FilesMatch "\.(js|css|html|htm|png|jpg|swf|pdf|shtml|xml|flv|gif|ico|jpeg)\$">
  124. RequestHeader edit "If-None-Match" "^(.*)-gzip(.*)\$" "\$1\$2"
  125. Header edit "ETag" "^(.*)-gzip(.*)\$" "\$1\$2"
  126. </FilesMatch>
  127. DeflateCompressionLevel 6
  128. SetOutputFilter DEFLATE
  129. </IfModule>
  130. ProtocolsHonorOrder On
  131. PidFile /var/run/httpd.pid
  132. ServerTokens ProductOnly
  133. ServerSignature Off
  134. Include conf/vhost/*.conf
  135. EOF
  136. [ "${Nginx_version}" == '4' -a ! -e "${web_install_dir}/sbin/nginx" ] && echo 'Protocols h2 http/1.1' >> ${apache_install_dir}/conf/httpd.conf
  137. if [ "${Nginx_version}" != '4' -o -e "${web_install_dir}/sbin/nginx" ]; then
  138. cat > ${apache_install_dir}/conf/extra/httpd-remoteip.conf << EOF
  139. LoadModule remoteip_module modules/mod_remoteip.so
  140. RemoteIPHeader X-Forwarded-For
  141. RemoteIPInternalProxy 127.0.0.1
  142. EOF
  143. sed -i "s@Include conf/extra/httpd-mpm.conf@Include conf/extra/httpd-mpm.conf\nInclude conf/extra/httpd-remoteip.conf@" ${apache_install_dir}/conf/httpd.conf
  144. sed -i "s@LogFormat \"%h %l@LogFormat \"%h %a %l@g" ${apache_install_dir}/conf/httpd.conf
  145. fi
  146. ldconfig
  147. service httpd start
  148. popd
  149. }