OpenResty 安裝 和 hello world

(1)部署openresty

mkdir -p /usr/servers

cd /usr/servers/

yum install readline-devel pcre pcre-devel openssl openssl-devel gcc curl GeoIP-devel

wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz

tar -xzvf ngx_openresty-1.7.7.2.tar.gz

cd /usr/servers/ngx_openresty-1.7.7.2/

cd bundle/LuaJIT-2.1-20150120/

make clean && make && make install

ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit

cd bundle

wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz

tar -xvf 2.3.tar.gz

cd bundle

wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz

tar -xvf v0.3.0.tar.gz

cd /usr/servers/ngx_openresty-1.7.7.2

./configure --prefix=/usr/servers --with-http_realip_module? --with-pcre? --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2

[

./configure --with-luajit --with-pcre --with-http_gzip_static_module --with-http_realip_module --with-http_geoip_module --with-http_ssl_module --with-http_stub_status_module

]

make && make install

cd /usr/servers/

ll

/usr/servers/luajit

/usr/servers/lualib

/usr/servers/nginx

/usr/servers/nginx/sbin/nginx -V

啟動(dòng)nginx: /usr/servers/nginx/sbin/nginx


(2)nginx+lua開(kāi)發(fā)的hello world

vi /usr/servers/nginx/conf/nginx.conf

在http部分添加:

lua_package_path "/usr/servers/lualib/?.lua;;";

lua_package_cpath "/usr/servers/lualib/?.so;;";

/usr/servers/nginx/conf下,創(chuàng)建一個(gè)lua.conf

server {

listen? ? ? 80;

server_name? _;

}

在nginx.conf的http部分添加:

include lua.conf;

驗(yàn)證配置是否正確:

/usr/servers/nginx/sbin/nginx -t

在lua.conf的server部分添加:

location /lua {

default_type 'text/html';

content_by_lua 'ngx.say("hello world")';

}

/usr/servers/nginx/sbin/nginx -t

重新nginx加載配置

/usr/servers/nginx/sbin/nginx -s reload

訪問(wèn)http: http://192.168.31.187/lua

vi /usr/servers/nginx/conf/lua/test.lua

ngx.say("hello world");

修改lua.conf

location /lua {

default_type 'text/html';

content_by_lua_file conf/lua/test.lua;

}

查看異常日志

tail -f /usr/servers/nginx/logs/error.log

(3)工程化的nginx+lua項(xiàng)目結(jié)構(gòu)

項(xiàng)目工程結(jié)構(gòu)

hello

hello.conf

lua

hello.lua

lualib

*.lua

*.so

放在/usr/hello目錄下

/usr/servers/nginx/conf/nginx.conf

worker_processes? 2;

error_log? logs/error.log;

events {

worker_connections? 1024;

}

http {

include? ? ? mime.types;

default_type? text/html;

lua_package_path "/usr/hello/lualib/?.lua;;";

lua_package_cpath "/usr/hello/lualib/?.so;;";

include /usr/hello/hello.conf;

}

/usr/hello/hello.conf

server {

listen? ? ? 80;

server_name? _;

location /lua {

default_type 'text/html';

lua_code_cache off;

content_by_lua_file /usr/hello/lua/hello.lua;

}

}


/usr/hello/lua/hello.lua

ngx.say("hello world");

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容