apache.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. Install_Apache() {
  11. pushd ${oneinstack_dir}/src > /dev/null
  12. tar xzf pcre-${pcre_ver}.tar.gz
  13. pushd pcre-${pcre_ver} > /dev/null
  14. ./configure
  15. make -j ${THREAD} && make install
  16. popd > /dev/null
  17. id -g ${run_group} >/dev/null 2>&1
  18. [ $? -ne 0 ] && groupadd ${run_group}
  19. id -u ${run_user} >/dev/null 2>&1
  20. [ $? -ne 0 ] && useradd -g ${run_group} -M -s /sbin/nologin ${run_user}
  21. tar xzf httpd-${apache_ver}.tar.gz
  22. # install apr
  23. if [ ! -e "${apr_install_dir}/bin/apr-1-config" ]; then
  24. tar xzf apr-${apr_ver}.tar.gz
  25. pushd apr-${apr_ver} > /dev/null
  26. ./configure --prefix=${apr_install_dir}
  27. make -j ${THREAD} && make install
  28. popd > /dev/null
  29. rm -rf apr-${apr_ver}
  30. fi
  31. # install apr-util
  32. if [ ! -e "${apr_install_dir}/bin/apu-1-config" ]; then
  33. tar xzf apr-util-${apr_util_ver}.tar.gz
  34. pushd apr-util-${apr_util_ver} > /dev/null
  35. ./configure --prefix=${apr_install_dir} --with-apr=${apr_install_dir}
  36. make -j ${THREAD} && make install
  37. popd > /dev/null
  38. rm -rf apr-util-${apr_util_ver}
  39. fi
  40. # install nghttp2
  41. if [ ! -e "/usr/local/lib/libnghttp2.so" ]; then
  42. tar xzf nghttp2-${nghttp2_ver}.tar.gz
  43. pushd nghttp2-${nghttp2_ver} > /dev/null
  44. ./configure
  45. make -j ${THREAD} && make install
  46. popd > /dev/null
  47. [ -z "`grep /usr/local/lib /etc/ld.so.conf.d/*.conf`" ] && echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
  48. ldconfig
  49. rm -rf nghttp2-${nghttp2_ver}
  50. fi
  51. pushd httpd-${apache_ver} > /dev/null
  52. LDFLAGS=-ldl ./configure --prefix=${apache_install_dir} --enable-mpms-shared=all --with-pcre --with-apr=${apr_install_dir} --with-apr-util=${apr_install_dir} --enable-headers --enable-mime-magic --enable-deflate --enable-proxy --enable-so --enable-dav --enable-rewrite --enable-remoteip --enable-expires --enable-static-support --enable-suexec --enable-mods-shared=most --enable-nonportable-atomics=yes --enable-ssl --with-ssl --enable-http2 --with-nghttp2=/usr/local
  53. make -j ${THREAD} && make install
  54. popd > /dev/null
  55. unset LDFLAGS
  56. if [ -e "${apache_install_dir}/bin/httpd" ]; then
  57. echo "${CSUCCESS}Apache installed successfully! ${CEND}"
  58. rm -rf httpd-${apache_ver} pcre-${pcre_ver}
  59. else
  60. rm -rf ${apache_install_dir}
  61. echo "${CFAILURE}Apache install failed, Please contact the author! ${CEND}" && grep -Ew 'NAME|ID|ID_LIKE|VERSION_ID|PRETTY_NAME' /etc/os-release
  62. kill -9 $$; exit 1;
  63. fi
  64. [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=${apache_install_dir}/bin:\$PATH" >> /etc/profile
  65. [ -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
  66. . /etc/profile
  67. /bin/cp ../init.d/httpd.service /lib/systemd/system/
  68. sed -i "s@/usr/local/apache@${apache_install_dir}@g" /lib/systemd/system/httpd.service
  69. systemctl enable httpd
  70. sed -i "s@^User daemon@User ${run_user}@" ${apache_install_dir}/conf/httpd.conf
  71. sed -i "s@^Group daemon@Group ${run_group}@" ${apache_install_dir}/conf/httpd.conf
  72. if [[ ! ${nginx_option} =~ ^[1-3]$ ]] && [ ! -e "${web_install_dir}/sbin/nginx" ]; then
  73. sed -i 's/^#ServerName www.example.com:80/ServerName 0.0.0.0:80/' ${apache_install_dir}/conf/httpd.conf
  74. TMP_PORT=80
  75. elif [[ ${nginx_option} =~ ^[1-3]$ ]] || [ -e "${web_install_dir}/sbin/nginx" ]; then
  76. sed -i 's/^#ServerName www.example.com:80/ServerName 127.0.0.1:88/' ${apache_install_dir}/conf/httpd.conf
  77. sed -i 's@^Listen.*@Listen 127.0.0.1:88@' ${apache_install_dir}/conf/httpd.conf
  78. TMP_PORT=88
  79. fi
  80. 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
  81. sed -i "s@#AddHandler cgi-script .cgi@AddHandler cgi-script .cgi .pl@" ${apache_install_dir}/conf/httpd.conf
  82. sed -ri 's@^#(LoadModule.*mod_proxy.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  83. sed -ri 's@^#(LoadModule.*mod_proxy_fcgi.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  84. sed -ri 's@^#(LoadModule.*mod_suexec.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  85. sed -ri 's@^#(LoadModule.*mod_vhost_alias.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  86. sed -ri 's@^#(LoadModule.*mod_rewrite.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  87. sed -ri 's@^#(LoadModule.*mod_deflate.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  88. sed -ri 's@^#(LoadModule.*mod_expires.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  89. sed -ri 's@^#(LoadModule.*mod_ssl.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  90. sed -ri 's@^#(LoadModule.*mod_http2.so)@\1@' ${apache_install_dir}/conf/httpd.conf
  91. sed -i 's@DirectoryIndex index.html@DirectoryIndex index.html index.php@' ${apache_install_dir}/conf/httpd.conf
  92. sed -i "s@^DocumentRoot.*@DocumentRoot \"${wwwroot_dir}/default\"@" ${apache_install_dir}/conf/httpd.conf
  93. sed -i "s@^<Directory \"${apache_install_dir}/htdocs\">@<Directory \"${wwwroot_dir}/default\">@" ${apache_install_dir}/conf/httpd.conf
  94. sed -i "s@^#Include conf/extra/httpd-mpm.conf@Include conf/extra/httpd-mpm.conf@" ${apache_install_dir}/conf/httpd.conf
  95. if [ "${apache_mpm_option}" == '2' ]; then
  96. sed -ri 's@^(LoadModule.*mod_mpm_event.so)@#\1@' ${apache_install_dir}/conf/httpd.conf
  97. sed -i 's@^#LoadModule mpm_prefork_module@LoadModule mpm_prefork_module@' ${apache_install_dir}/conf/httpd.conf
  98. elif [ "${apache_mpm_option}" == '3' ]; then
  99. sed -ri 's@^(LoadModule.*mod_mpm_event.so)@#\1@' ${apache_install_dir}/conf/httpd.conf
  100. sed -i 's@^#LoadModule mpm_worker_module@LoadModule mpm_worker_module@' ${apache_install_dir}/conf/httpd.conf
  101. fi
  102. #logrotate apache log
  103. cat > /etc/logrotate.d/apache << EOF
  104. ${wwwlogs_dir}/*apache.log {
  105. daily
  106. rotate 5
  107. missingok
  108. dateext
  109. compress
  110. notifempty
  111. sharedscripts
  112. postrotate
  113. [ -e /var/run/httpd.pid ] && kill -USR1 \`cat /var/run/httpd.pid\`
  114. endscript
  115. }
  116. EOF
  117. mkdir ${apache_install_dir}/conf/vhost
  118. [ "${apache_mode_option}" != '2' ] && Apache_fcgi=$(echo -e "<Files ~ (\\.user.ini|\\.htaccess|\\.git|\\.svn|\\.project|LICENSE|README.md)\$>\n Order allow,deny\n Deny from all\n </Files>\n <FilesMatch \\.php\$>\n SetHandler \"proxy:unix:/dev/shm/php-cgi.sock|fcgi://localhost\"\n </FilesMatch>")
  119. cat > ${apache_install_dir}/conf/vhost/0.conf << EOF
  120. <VirtualHost *:$TMP_PORT>
  121. ServerAdmin admin@example.com
  122. DocumentRoot "${wwwroot_dir}/default"
  123. ServerName 127.0.0.1
  124. ErrorLog "${wwwlogs_dir}/error_apache.log"
  125. CustomLog "${wwwlogs_dir}/access_apache.log" common
  126. <Files ~ (\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)\$>
  127. Order allow,deny
  128. Deny from all
  129. </Files>
  130. ${Apache_fcgi}
  131. <Directory "${wwwroot_dir}/default">
  132. SetOutputFilter DEFLATE
  133. Options FollowSymLinks ExecCGI
  134. Require all granted
  135. AllowOverride All
  136. Order allow,deny
  137. Allow from all
  138. DirectoryIndex index.html index.php
  139. </Directory>
  140. <Location /server-status>
  141. SetHandler server-status
  142. Order Deny,Allow
  143. Deny from all
  144. Allow from 127.0.0.1
  145. </Location>
  146. </VirtualHost>
  147. EOF
  148. cat >> ${apache_install_dir}/conf/httpd.conf <<EOF
  149. <IfModule mod_headers.c>
  150. AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
  151. <FilesMatch "\.(js|css|html|htm|png|jpg|swf|pdf|shtml|xml|flv|gif|ico|jpeg)\$">
  152. RequestHeader edit "If-None-Match" "^(.*)-gzip(.*)\$" "\$1\$2"
  153. Header edit "ETag" "^(.*)-gzip(.*)\$" "\$1\$2"
  154. </FilesMatch>
  155. DeflateCompressionLevel 6
  156. SetOutputFilter DEFLATE
  157. </IfModule>
  158. ProtocolsHonorOrder On
  159. PidFile /var/run/httpd.pid
  160. ServerTokens ProductOnly
  161. ServerSignature Off
  162. Include conf/vhost/*.conf
  163. EOF
  164. [ "${nginx_option}" == '4' -a ! -e "${web_install_dir}/sbin/nginx" ] && echo 'Protocols h2 http/1.1' >> ${apache_install_dir}/conf/httpd.conf
  165. if [ "${nginx_option}" != '4' -o -e "${web_install_dir}/sbin/nginx" ]; then
  166. cat > ${apache_install_dir}/conf/extra/httpd-remoteip.conf << EOF
  167. LoadModule remoteip_module modules/mod_remoteip.so
  168. RemoteIPHeader X-Forwarded-For
  169. RemoteIPInternalProxy 127.0.0.1
  170. EOF
  171. 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
  172. sed -i "s@LogFormat \"%h %l@LogFormat \"%h %a %l@g" ${apache_install_dir}/conf/httpd.conf
  173. fi
  174. ldconfig
  175. [ "${with_old_openssl_flag}" == 'y' ] && sed -i "s@^export LD_LIBRARY_PATH.*@export LD_LIBRARY_PATH=${openssl_install_dir}/lib:\$LD_LIBRARY_PATH@" ${apache_install_dir}/bin/envvars
  176. systemctl start httpd
  177. popd > /dev/null
  178. }