openresty.sh 4.8 KB

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