openresty.sh 4.8 KB

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