1
0

apache-2.2.sh 5.7 KB

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