ngx_lua_waf.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. Nginx_lua_waf() {
  11. pushd ${oneinstack_dir}/src > /dev/null
  12. [ ! -e "${nginx_install_dir}/sbin/nginx" ] && echo "${CWARNING}Nginx is not installed on your system! ${CEND}" && exit 1
  13. if [ ! -e "/usr/local/lib/libluajit-5.1.so.2.1.0" ]; then
  14. [ -e "/usr/local/lib/libluajit-5.1.so.2.0.5" ] && find /usr/local -name *luajit* | xargs rm -rf
  15. src_url=${mirror_link}/oneinstack/src/luajit2-${luajit2_ver}.tar.gz && Download_src
  16. tar xzf luajit2-${luajit2_ver}.tar.gz
  17. pushd luajit2-${luajit2_ver}
  18. make && make install
  19. [ ! -e "/usr/local/lib/libluajit-5.1.so.2.1.0" ] && { echo "${CFAILURE}LuaJIT install failed! ${CEND}"; kill -9 $$; exit 1; }
  20. popd > /dev/null
  21. rm -rf luajit2-${luajit2_ver}
  22. fi
  23. src_url=${mirror_link}/oneinstack/src/lua-resty-core-${lua_resty_core_ver}.tar.gz && Download_src
  24. tar xzf lua-resty-core-${lua_resty_core_ver}.tar.gz
  25. pushd lua-resty-core-${lua_resty_core_ver}
  26. make install
  27. popd > /dev/null
  28. rm -rf lua-resty-core-${lua_resty_core_ver}
  29. src_url=${mirror_link}/oneinstack/src/lua-resty-lrucache-${lua_resty_lrucache_ver}.tar.gz && Download_src
  30. tar xzf lua-resty-lrucache-${lua_resty_lrucache_ver}.tar.gz
  31. pushd lua-resty-lrucache-${lua_resty_lrucache_ver}
  32. make install
  33. popd > /dev/null
  34. rm -rf lua-resty-lrucache-${lua_resty_lrucache_ver}
  35. [ ! -h "/usr/local/share/lua/5.1" ] && { rm -rf /usr/local/share/lua/5.1 ; ln -s /usr/local/lib/lua /usr/local/share/lua/5.1; }
  36. if [ ! -e "/usr/local/lib/lua/5.1/cjson.so" ]; then
  37. src_url=${mirror_link}/oneinstack/src/lua-cjson-${lua_cjson_ver}.tar.gz && Download_src
  38. tar xzf lua-cjson-${lua_cjson_ver}.tar.gz
  39. pushd lua-cjson-${lua_cjson_ver}
  40. sed -i 's@^LUA_INCLUDE_DIR.*@&/luajit-2.1@' Makefile
  41. make && make install
  42. [ ! -e "/usr/local/lib/lua/5.1/cjson.so" ] && { echo "${CFAILURE}lua-cjson install failed! ${CEND}"; kill -9 $$; exit 1; }
  43. popd > /dev/null
  44. fi
  45. ${nginx_install_dir}/sbin/nginx -V &> $$
  46. nginx_configure_args_tmp=`cat $$ | grep 'configure arguments:' | awk -F: '{print $2}'`
  47. rm -rf $$
  48. nginx_configure_args=`echo ${nginx_configure_args_tmp} | sed "s@--with-openssl=../openssl-\w.\w.\w\+ @--with-openssl=../openssl-${openssl11_ver} @" | sed "s@--with-pcre=../pcre-\w.\w\+ @--with-pcre=../pcre-${pcre_ver} @"`
  49. if [ -z "`echo ${nginx_configure_args} | grep lua-nginx-module`" ]; then
  50. src_url=http://nginx.org/download/nginx-${nginx_ver}.tar.gz && Download_src
  51. src_url=https://www.openssl.org/source/openssl-${openssl11_ver}.tar.gz && Download_src
  52. src_url=${mirror_link}/oneinstack/src/pcre-${pcre_ver}.tar.gz && Download_src
  53. src_url=${mirror_link}/oneinstack/src/ngx_devel_kit.tar.gz && Download_src
  54. src_url=${mirror_link}/oneinstack/src/lua-nginx-module-${lua_nginx_module_ver}.tar.gz && Download_src
  55. tar xzf nginx-${nginx_ver}.tar.gz
  56. tar xzf openssl-${openssl11_ver}.tar.gz
  57. tar xzf pcre-${pcre_ver}.tar.gz
  58. tar xzf ngx_devel_kit.tar.gz
  59. tar xzf lua-nginx-module-${lua_nginx_module_ver}.tar.gz
  60. pushd nginx-${nginx_ver}
  61. make clean
  62. sed -i 's@CFLAGS="$CFLAGS -g"@#CFLAGS="$CFLAGS -g"@' auto/cc/gcc # close debug
  63. export LUAJIT_LIB=/usr/local/lib
  64. export LUAJIT_INC=/usr/local/include/luajit-2.1
  65. ./configure ${nginx_configure_args} --with-ld-opt="-Wl,-rpath,/usr/local/lib" --add-module=../lua-nginx-module-${lua_nginx_module_ver} --add-module=../ngx_devel_kit
  66. make -j ${THREAD}
  67. if [ -f "objs/nginx" ]; then
  68. /bin/mv ${nginx_install_dir}/sbin/nginx{,`date +%m%d`}
  69. /bin/cp objs/nginx ${nginx_install_dir}/sbin/nginx
  70. kill -USR2 `cat /var/run/nginx.pid`
  71. sleep 1
  72. kill -QUIT `cat /var/run/nginx.pid.oldbin`
  73. popd > /dev/null
  74. echo "${CSUCCESS}lua-nginx-module installed successfully! ${CEND}"
  75. sed -i "s@^nginx_modules_options='\(.*\)'@nginx_modules_options=\'\1 --with-ld-opt=\"-Wl,-rpath,/usr/local/lib\" --add-module=../lua-nginx-module-${lua_nginx_module_ver} --add-module=../ngx_devel_kit\'@" ../options.conf
  76. rm -rf nginx-${nginx_ver}
  77. else
  78. echo "${CFAILURE}lua-nginx-module install failed! ${CEND}"
  79. kill -9 $$; exit 1;
  80. fi
  81. fi
  82. popd > /dev/null
  83. }
  84. Tengine_lua_waf() {
  85. pushd ${oneinstack_dir}/src > /dev/null
  86. [ ! -e "${tengine_install_dir}/sbin/nginx" ] && echo "${CWARNING}Tengine is not installed on your system! ${CEND}" && exit 1
  87. if [ ! -e "/usr/local/lib/libluajit-5.1.so.2.1.0" ]; then
  88. [ -e "/usr/local/lib/libluajit-5.1.so.2.0.5" ] && find /usr/local -name *luajit* | xargs rm -rf
  89. src_url=${mirror_link}/oneinstack/src/luajit2-${luajit2_ver}.tar.gz && Download_src
  90. tar xzf luajit2-${luajit2_ver}.tar.gz
  91. pushd luajit2-${luajit2_ver}
  92. make && make install
  93. [ ! -e "/usr/local/lib/libluajit-5.1.so.2.1.0" ] && { echo "${CFAILURE}LuaJIT install failed! ${CEND}"; kill -9 $$; exit 1; }
  94. popd > /dev/null
  95. fi
  96. if [ ! -e "/usr/local/lib/lua/5.1/cjson.so" ]; then
  97. src_url=${mirror_link}/oneinstack/src/lua-cjson-${lua_cjson_ver}.tar.gz && Download_src
  98. tar xzf lua-cjson-${lua_cjson_ver}.tar.gz
  99. pushd lua-cjson-${lua_cjson_ver}
  100. sed -i 's@^LUA_INCLUDE_DIR.*@&/luajit-2.1@' Makefile
  101. make && make install
  102. [ ! -e "/usr/local/lib/lua/5.1/cjson.so" ] && { echo "${CFAILURE}lua-cjson install failed! ${CEND}"; kill -9 $$; exit 1; }
  103. popd > /dev/null
  104. fi
  105. ${tengine_install_dir}/sbin/nginx -V &> $$
  106. tengine_configure_args_tmp=`cat $$ | grep 'configure arguments:' | awk -F: '{print $2}'`
  107. rm -rf $$
  108. tengine_configure_args=`echo ${tengine_configure_args_tmp} | sed "s@--with-openssl=../openssl-\w.\w.\w\+ @--with-openssl=../openssl-${openssl11_ver} @" | sed "s@--with-pcre=../pcre-\w.\w\+ @--with-pcre=../pcre-${pcre_ver} @"`
  109. if [ -z "`echo ${tengine_configure_args} | grep lua`" ]; then
  110. src_url=http://tengine.taobao.org/download/tengine-${tengine_ver}.tar.gz && Download_src
  111. src_url=https://www.openssl.org/source/openssl-${openssl11_ver}.tar.gz && Download_src
  112. src_url=${mirror_link}/oneinstack/src/pcre-${pcre_ver}.tar.gz && Download_src
  113. src_url=${mirror_link}/oneinstack/src/ngx_devel_kit.tar.gz && Download_src
  114. src_url=${mirror_link}/oneinstack/src/lua-nginx-module.tar.gz && Download_src
  115. tar xzf tengine-${tengine_ver}.tar.gz
  116. tar xzf openssl-${openssl11_ver}.tar.gz
  117. tar xzf pcre-${pcre_ver}.tar.gz
  118. tar xzf ngx_devel_kit.tar.gz
  119. tar xzf lua-nginx-module.tar.gz
  120. pushd tengine-${tengine_ver}
  121. make clean
  122. export LUAJIT_LIB=/usr/local/lib
  123. export LUAJIT_INC=/usr/local/include/luajit-2.1
  124. ./configure ${tengine_configure_args} --with-ld-opt="-Wl,-rpath,/usr/local/lib" --add-module=../lua-nginx-module --add-module=../ngx_devel_kit
  125. make -j ${THREAD}
  126. if [ -f "objs/nginx" ]; then
  127. /bin/mv ${tengine_install_dir}/sbin/nginx{,`date +%m%d`}
  128. /bin/mv ${tengine_install_dir}/modules{,`date +%m%d`}
  129. /bin/cp objs/nginx ${tengine_install_dir}/sbin/nginx
  130. chmod +x ${tengine_install_dir}/sbin/*
  131. make install
  132. kill -USR2 `cat /var/run/nginx.pid`
  133. sleep 1
  134. kill -QUIT `cat /var/run/nginx.pid.oldbin`
  135. popd > /dev/null
  136. sed -i "s@^nginx_modules_options='\(.*\)'@nginx_modules_options=\'\1 --with-ld-opt=\"-Wl,-rpath,/usr/local/lib\" --add-module=../lua-nginx-module --add-module=../ngx_devel_kit\'@" ../options.conf
  137. echo "${CSUCCESS}lua_module installed successfully! ${CEND}"
  138. rm -rf tengine-${tengine_ver}
  139. else
  140. echo "${CFAILURE}lua_module install failed! ${CEND}"
  141. kill -9 $$; exit 1;
  142. fi
  143. fi
  144. popd > /dev/null
  145. }
  146. enable_lua_waf() {
  147. pushd ${oneinstack_dir}/src > /dev/null
  148. . ../include/check_dir.sh
  149. rm -f ngx_lua_waf.tar.gz
  150. src_url=${mirror_link}/oneinstack/src/ngx_lua_waf.tar.gz && Download_src
  151. tar xzf ngx_lua_waf.tar.gz -C ${web_install_dir}/conf
  152. [ -e "${web_install_dir}/conf/resty" ] && /bin/mv ${web_install_dir}/conf/resty{,_bak}
  153. sed -i "s@/usr/local/nginx@${web_install_dir}@g" ${web_install_dir}/conf/waf.conf
  154. sed -i "s@/usr/local/nginx@${web_install_dir}@" ${web_install_dir}/conf/waf/config.lua
  155. sed -i "s@/data/wwwlogs@${wwwlogs_dir}@" ${web_install_dir}/conf/waf/config.lua
  156. [ -z "`grep 'include waf.conf;' ${web_install_dir}/conf/nginx.conf`" ] && sed -i "s@ vhost/\*.conf;@&\n include waf.conf;@" ${web_install_dir}/conf/nginx.conf
  157. ${web_install_dir}/sbin/nginx -t
  158. if [ $? -eq 0 ]; then
  159. service nginx reload
  160. echo "${CSUCCESS}ngx_lua_waf enabled successfully! ${CEND}"
  161. chown ${run_user}:${run_group} ${wwwlogs_dir}
  162. else
  163. echo "${CFAILURE}ngx_lua_waf enable failed! ${CEND}"
  164. fi
  165. popd > /dev/null
  166. }
  167. disable_lua_waf() {
  168. pushd ${oneinstack_dir}/src > /dev/null
  169. . ../include/check_dir.sh
  170. sed -i '/include waf.conf;/d' ${web_install_dir}/conf/nginx.conf
  171. ${web_install_dir}/sbin/nginx -t
  172. if [ $? -eq 0 ]; then
  173. rm -rf ${web_install_dir}/conf/{waf,waf.conf}
  174. service nginx reload
  175. echo "${CSUCCESS}ngx_lua_waf disabled successfully! ${CEND}"
  176. else
  177. echo "${CFAILURE}ngx_lua_waf disable failed! ${CEND}"
  178. fi
  179. popd > /dev/null
  180. }