apache-2.2.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 6+ Debian 7+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. Install_Apache22() {
  11. pushd ${oneinstack_dir}/src > /dev/null
  12. id -u ${run_user} >/dev/null 2>&1
  13. [ $? -ne 0 ] && useradd -M -s /sbin/nologin ${run_user}
  14. tar xzf httpd-${apache22_ver}.tar.gz
  15. pushd httpd-${apache22_ver}
  16. [ ! -d "${apache_install_dir}" ] && mkdir -p ${apache_install_dir}
  17. [ "${Ubuntu_ver}" == "12" ] && sed -i '@SSL_PROTOCOL_SSLV2@d' modules/ssl/ssl_engine_io.c
  18. LDFLAGS=-ldl ./configure --prefix=${apache_install_dir} --with-mpm=prefork --enable-mpms-shared=all --with-included-apr --enable-headers --enable-mime-magic --enable-deflate --enable-proxy --enable-so --enable-dav --enable-rewrite --enable-expires --enable-static-support --enable-suexec --with-expat=builtin --enable-mods-shared=most --enable-ssl --with-ssl=${openssl_install_dir}
  19. make -j ${THREAD} && make install
  20. unset LDFLAGS
  21. if [ -e "${apache_install_dir}/conf/httpd.conf" ]; then
  22. echo "${CSUCCESS}Apache installed successfully! ${CEND}"
  23. popd
  24. rm -rf httpd-${apache22_ver}
  25. else
  26. rm -rf ${apache_install_dir}
  27. echo "${CFAILURE}Apache install failed, Please contact the author! ${CEND}"
  28. kill -9 $$
  29. fi
  30. [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=${apache_install_dir}/bin:\$PATH" >> /etc/profile
  31. [ -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
  32. . /etc/profile
  33. if [ -e /bin/systemctl ]; then
  34. /bin/cp ../init.d/httpd.service /lib/systemd/system/
  35. sed -i "s@/usr/local/apache@${apache_install_dir}@g" /lib/systemd/system/httpd.service
  36. systemctl enable httpd
  37. else
  38. /bin/cp ${apache_install_dir}/bin/apachectl /etc/init.d/httpd
  39. sed -i '2a # chkconfig: - 85 15' /etc/init.d/httpd
  40. sed -i '3a # description: Apache is a World Wide Web server. It is used to serve' /etc/init.d/httpd
  41. chmod +x /etc/init.d/httpd
  42. [ "${PM}" == 'yum' ] && { chkconfig --add httpd; chkconfig httpd on; }
  43. [ "${PM}" == 'apt-get' ] && update-rc.d httpd defaults
  44. fi
  45. sed -i "s@^User daemon@User ${run_user}@" ${apache_install_dir}/conf/httpd.conf
  46. sed -i "s@^Group daemon@Group ${run_user}@" ${apache_install_dir}/conf/httpd.conf
  47. if [[ ! ${nginx_option} =~ ^[1-3]$ ]] && [ ! -e "${web_install_dir}/sbin/nginx" ]; then
  48. sed -i 's/^#ServerName www.example.com:80/ServerName 0.0.0.0:80/' ${apache_install_dir}/conf/httpd.conf
  49. TMP_PORT=80
  50. elif [[ ${nginx_option} =~ ^[1-3]$ ]] || [ -e "${web_install_dir}/sbin/nginx" ]; then
  51. sed -i 's/^#ServerName www.example.com:80/ServerName 127.0.0.1:88/' ${apache_install_dir}/conf/httpd.conf
  52. sed -i 's@^Listen.*@Listen 127.0.0.1:88@' ${apache_install_dir}/conf/httpd.conf
  53. TMP_PORT=88
  54. fi
  55. 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
  56. sed -i "s@#AddHandler cgi-script .cgi@AddHandler cgi-script .cgi .pl@" ${apache_install_dir}/conf/httpd.conf
  57. sed -i 's@DirectoryIndex index.html@DirectoryIndex index.html index.php@' ${apache_install_dir}/conf/httpd.conf
  58. sed -i "s@^DocumentRoot.*@DocumentRoot \"${wwwroot_dir}/default\"@" ${apache_install_dir}/conf/httpd.conf
  59. sed -i "s@^<Directory \"${apache_install_dir}/htdocs\">@<Directory \"${wwwroot_dir}/default\">@" ${apache_install_dir}/conf/httpd.conf
  60. sed -i "s@^#Include conf/extra/httpd-mpm.conf@Include conf/extra/httpd-mpm.conf@" ${apache_install_dir}/conf/httpd.conf
  61. #logrotate apache log
  62. cat > /etc/logrotate.d/apache << EOF
  63. ${wwwlogs_dir}/*apache.log {
  64. daily
  65. rotate 5
  66. missingok
  67. dateext
  68. compress
  69. notifempty
  70. sharedscripts
  71. postrotate
  72. [ -e /var/run/httpd.pid ] && kill -USR1 \`cat /var/run/httpd.pid\`
  73. endscript
  74. }
  75. EOF
  76. mkdir ${apache_install_dir}/conf/vhost
  77. cat > ${apache_install_dir}/conf/vhost/0.conf << EOF
  78. NameVirtualHost *:$TMP_PORT
  79. <VirtualHost *:$TMP_PORT>
  80. ServerAdmin admin@example.com
  81. DocumentRoot "${wwwroot_dir}/default"
  82. ServerName 127.0.0.1
  83. ErrorLog "${wwwlogs_dir}/error_apache.log"
  84. CustomLog "${wwwlogs_dir}/access_apache.log" common
  85. <Directory "${wwwroot_dir}/default">
  86. SetOutputFilter DEFLATE
  87. Options FollowSymLinks ExecCGI
  88. AllowOverride All
  89. Order allow,deny
  90. Allow from all
  91. DirectoryIndex index.html index.php
  92. </Directory>
  93. <Location /server-status>
  94. SetHandler server-status
  95. Order Deny,Allow
  96. Deny from all
  97. Allow from 127.0.0.1
  98. </Location>
  99. </VirtualHost>
  100. EOF
  101. cat >> ${apache_install_dir}/conf/httpd.conf <<EOF
  102. <IfModule mod_headers.c>
  103. AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
  104. <FilesMatch "\.(js|css|html|htm|png|jpg|swf|pdf|shtml|xml|flv|gif|ico|jpeg)\$">
  105. RequestHeader edit "If-None-Match" "^(.*)-gzip(.*)\$" "\$1\$2"
  106. Header edit "ETag" "^(.*)-gzip(.*)\$" "\$1\$2"
  107. </FilesMatch>
  108. DeflateCompressionLevel 6
  109. SetOutputFilter DEFLATE
  110. </IfModule>
  111. PidFile /var/run/httpd.pid
  112. ServerTokens ProductOnly
  113. ServerSignature Off
  114. Include conf/vhost/*.conf
  115. EOF
  116. if [ "${nginx_option}" != '4' -o -e "${web_install_dir}/sbin/nginx" ]; then
  117. ${apache_install_dir}/bin/apxs -i -c -n mod_remoteip.so mod_remoteip.c
  118. cat > ${apache_install_dir}/conf/extra/httpd-remoteip.conf << EOF
  119. LoadModule remoteip_module modules/mod_remoteip.so
  120. RemoteIPHeader X-Forwarded-For
  121. RemoteIPInternalProxy 127.0.0.1
  122. EOF
  123. 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
  124. fi
  125. ldconfig
  126. service httpd start
  127. popd
  128. }