实战:Nginx集成Lua脚本并调用memcached
之前在阿里云的centos系統(tǒng)上已經(jīng)安裝過nginx,但是沒有安裝lua所需的模塊,因此重新編譯nginx來擴(kuò)展lua模塊。
ngx_lua_module 是一個nginx http模塊,它把 lua 解析器內(nèi)嵌到 nginx,用來解析并執(zhí)行l(wèi)ua 語言編寫的網(wǎng)頁后臺腳本。這里主要是示范一下,如何在Nginx下安裝lua-nginx-module模塊
?
首先先安裝好:
yum -y install openssl openssl-devel? ? ? ?注意:ubuntu系統(tǒng)安裝openssl-devel 命令:sudo apt-get install libssl-dev
yum install lua-devel
1)首先下載安裝Lua腳本解釋器 LuaJIT
wget?http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar -zxvf??LuaJIT-2.0.2.tar.gz
cd?LuaJIT-2.0.2
make install PREFIX=/usr/local/LuaJIT
==>在/etc/profile?文件中加入環(huán)境變量
#luajit
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
2) 下載ngx_devel_kit(NDK)模塊 :https://github.com/simpl/ngx_devel_kit/tags,不需要安裝
cd /usr/local/src
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -xzvf v0.2.19.tar.gz
3)下載最新的lua-nginx-module 模塊 :https://github.com/openresty/lua-nginx-module/tags,不需要安裝?
cd /usr/local/src
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
tar -xzvf v0.10.2.tar.gz
4)通過nginx -V 獲得之前編譯的模塊信息,拷貝下來。
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'?
5)下載nginx的源碼包
wget?http://nginx.org/download/nginx-1.16.0.tar.gz
tar -zxvf?nginx-1.16.0.tar.gz
cd?nginx-1.16.0
?
6)加入lua模塊,重新編譯安裝nginx
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'??--add-module=/usr/local/src/ngx_devel_kit-0.3.0 --add-module=/usr/local/src/lua-nginx-module-0.10.9rc7
?
7)make & make install
8)? 加載lua庫,加入到ld.so.conf文件
9)? echo "/usr/local/lib" >> /etc/ld.so.conf
10) ldconfig
11)? nginx -V查看
至此nginx加載lua完成;測試一下:
配置如下
訪問:
接下來就是lua調(diào)用memcached數(shù)據(jù)庫;
首先,安裝memcached:yum install memcached
然后,在memchched設(shè)置一個key為 “183.6.159.136”,value為1的鍵值對,方便后面測試。
下載lua-resty-memchached,此模塊是lua鏈接memcached數(shù)據(jù)庫的開發(fā)包。下圖中復(fù)制的路徑自己決定;
在nginx中配置:
上圖紅色框框是添加庫文件路徑,參考?ttps://blog.csdn.net/langeldep/article/details/8289199
當(dāng)我們訪問host/luatest時,nginx會去執(zhí)行dep.lua腳本文件
對應(yīng)的dep.lua文件:
在lua腳本中,我們鏈接memcached,獲取鍵值,打印輸出給客戶端;
當(dāng)我們在瀏覽器訪問:
可以正確顯示,至此nginx運(yùn)行l(wèi)ua腳本,lua腳本連接memcached的實戰(zhàn)完成
?
?
參考:
當(dāng)然,如果你之前沒有安裝過Nginx,而且嫌安裝麻煩,可直接下載openresty安裝簡單快捷,http://openresty.org/cn/installation.html(阿里的大牛章亦春的作品,膜拜~~~)
https://blog.csdn.net/qq_25551295/article/details/51744815
http://www.imooc.com/article/19597
https://www.imooc.com/article/67583
總結(jié)
以上是生活随笔為你收集整理的实战:Nginx集成Lua脚本并调用memcached的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nginx作为静态资源web服务
- 下一篇: 统计操作系统的整个系统负载占用CPU的情