1
0

nginx.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_Nginx() {
  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 pcre-${pcre_ver}.tar.gz
  17. tar xzf nginx-${nginx_ver}.tar.gz
  18. tar xzf openssl-${openssl11_ver}.tar.gz
  19. pushd nginx-${nginx_ver} > /dev/null
  20. # Modify Nginx version
  21. #sed -i 's@#define NGINX_VERSION.*$@#define NGINX_VERSION "1.2"@' src/core/nginx.h
  22. #sed -i 's@#define NGINX_VER.*NGINX_VERSION$@#define NGINX_VER "Linuxeye/" NGINX_VERSION@' src/core/nginx.h
  23. #sed -i 's@Server: nginx@Server: linuxeye@' src/http/ngx_http_header_filter_module.c
  24. # close debug
  25. sed -i 's@CFLAGS="$CFLAGS -g"@#CFLAGS="$CFLAGS -g"@' auto/cc/gcc
  26. [ ! -d "${nginx_install_dir}" ] && mkdir -p ${nginx_install_dir}
  27. ./configure --prefix=${nginx_install_dir} --user=${run_user} --group=${run_group} --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-${openssl11_ver} --with-pcre=../pcre-${pcre_ver} --with-pcre-jit --with-ld-opt='-ljemalloc' ${nginx_modules_options}
  28. make -j ${THREAD} && make install
  29. if [ -e "${nginx_install_dir}/conf/nginx.conf" ]; then
  30. popd > /dev/null
  31. rm -rf pcre-${pcre_ver} openssl-${openssl11_ver} nginx-${nginx_ver}
  32. echo "${CSUCCESS}Nginx installed successfully! ${CEND}"
  33. else
  34. rm -rf ${nginx_install_dir}
  35. echo "${CFAILURE}Nginx install failed, Please Contact the author! ${CEND}"
  36. kill -9 $$
  37. fi
  38. [ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=${nginx_install_dir}/sbin:\$PATH" >> /etc/profile
  39. [ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep ${nginx_install_dir} /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=${nginx_install_dir}/sbin:\1@" /etc/profile
  40. . /etc/profile
  41. if [ -e /bin/systemctl ]; then
  42. /bin/cp ../init.d/nginx.service /lib/systemd/system/
  43. sed -i "s@/usr/local/nginx@${nginx_install_dir}@g" /lib/systemd/system/nginx.service
  44. systemctl enable nginx
  45. else
  46. [ "${PM}" == 'yum' ] && { /bin/cp ../init.d/Nginx-init-CentOS /etc/init.d/nginx; sed -i "s@/usr/local/nginx@${nginx_install_dir}@g" /etc/init.d/nginx; chkconfig --add nginx; chkconfig nginx on; }
  47. [ "${PM}" == 'apt-get' ] && { /bin/cp ../init.d/Nginx-init-Ubuntu /etc/init.d/nginx; sed -i "s@/usr/local/nginx@${nginx_install_dir}@g" /etc/init.d/nginx; update-rc.d nginx defaults; }
  48. fi
  49. mv ${nginx_install_dir}/conf/nginx.conf{,_bk}
  50. if [[ ${apache_option} =~ ^[1-2]$ ]] || [ -e "${apache_install_dir}/bin/httpd" ]; then
  51. /bin/cp ../config/nginx_apache.conf ${nginx_install_dir}/conf/nginx.conf
  52. elif { [[ ${tomcat_option} =~ ^[1-4]$ ]] || [ -e "${tomcat_install_dir}/conf/server.xml" ]; } && { [[ ! ${php_option} =~ ^[1-9]$|^10$ ]] && [ ! -e "${php_install_dir}/bin/php" ]; }; then
  53. /bin/cp ../config/nginx_tomcat.conf ${nginx_install_dir}/conf/nginx.conf
  54. else
  55. /bin/cp ../config/nginx.conf ${nginx_install_dir}/conf/nginx.conf
  56. [[ "${php_option}" =~ ^[1-9]$|^10$ ]] && [ -z "`grep '/php-fpm_status' ${nginx_install_dir}/conf/nginx.conf`" ] && sed -i "s@index index.html index.php;@index index.html index.php;\n location ~ /php-fpm_status {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n allow 127.0.0.1;\n deny all;\n }@" ${nginx_install_dir}/conf/nginx.conf
  57. fi
  58. cat > ${nginx_install_dir}/conf/proxy.conf << EOF
  59. proxy_connect_timeout 300s;
  60. proxy_send_timeout 900;
  61. proxy_read_timeout 900;
  62. proxy_buffer_size 32k;
  63. proxy_buffers 4 64k;
  64. proxy_busy_buffers_size 128k;
  65. proxy_redirect off;
  66. proxy_hide_header Vary;
  67. proxy_set_header Accept-Encoding '';
  68. proxy_set_header Referer \$http_referer;
  69. proxy_set_header Cookie \$http_cookie;
  70. proxy_set_header Host \$host;
  71. proxy_set_header X-Real-IP \$remote_addr;
  72. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  73. proxy_set_header X-Forwarded-Proto \$scheme;
  74. EOF
  75. sed -i "s@/data/wwwroot/default@${wwwroot_dir}/default@" ${nginx_install_dir}/conf/nginx.conf
  76. sed -i "s@/data/wwwlogs@${wwwlogs_dir}@g" ${nginx_install_dir}/conf/nginx.conf
  77. sed -i "s@^user www www@user ${run_user} ${run_group}@" ${nginx_install_dir}/conf/nginx.conf
  78. # logrotate nginx log
  79. cat > /etc/logrotate.d/nginx << EOF
  80. ${wwwlogs_dir}/*nginx.log {
  81. daily
  82. rotate 5
  83. missingok
  84. dateext
  85. compress
  86. notifempty
  87. sharedscripts
  88. postrotate
  89. [ -e /var/run/nginx.pid ] && kill -USR1 \`cat /var/run/nginx.pid\`
  90. endscript
  91. }
  92. EOF
  93. popd > /dev/null
  94. ldconfig
  95. service nginx start
  96. }