Lua系列文章 第一篇 安裝OpenResty(Nginx+Lua)開發(fā)環(huán)境

Lua安裝

首先我們選擇使用OpenResty,其是由Nginx核心加很多第三方模塊組成,其最大的亮點(diǎn)是默認(rèn)集成了Lua開發(fā)環(huán)境,使得Nginx可以作為一個(gè)Web Server使用。借助于Nginx的事件驅(qū)動(dòng)模型和非阻塞IO,可以實(shí)現(xiàn)高性能的Web應(yīng)用程序。而且OpenResty提供了大量組件如Mysql、Redis、Memcached等等,使在Nginx上開發(fā)Web應(yīng)用更方便更簡(jiǎn)單。目前在大公司的核心功能上都在使用Nginx+Lua架構(gòu),比如淘寶、京東、去哪兒等等。

安裝環(huán)境

安裝步驟可以參考http://openresty.org/#Installation。

1、軟件安裝目錄/usr/local/src,以后我們把所有軟件安裝在此目錄

2、安裝依賴(我的環(huán)境是centos,可以使用如下命令安裝,其他的可以參考o(jì)penresty安裝步驟)

? yum install pcre-devel openssl-devel gcc curl

3、下載ngx_openresty-1.11.2.5.tar.gz并解壓

? wget https://openresty.org/download/openresty-1.11.2.5.tar.gz

? tar xf openresty-1.11.2.5.tar.gz

4、ngx_openresty-1.11.2.5/bundle目錄里存放著nginx核心和很多第三方模塊,比如有我們需要的Lua和LuaJIT。

5、安裝LuaJIT

? cd openresty-1.11.2.5/bundle/LuaJIT-2.1-20170808/?

? make clean && make && make install?

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

6、下載ngx_cache_purge模塊,該模塊用于清理nginx緩存

? cd /usr/local/src/openresty-1.11.2.5/bundle?

? github地址:https://github.com/FRiCKLE/ngx_cache_purge

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

? mv 2.3.tar.gz ngx_cache_purge.2.3.tar.gz

? tar xf ngx_cache_purge.2.3.tar.gz

7、下載nginx_upstream_check_module模塊,該模塊用于ustream健康檢查

? cd /usr/local/src/openresty-1.11.2.5/bundle?

? github地址:https://github.com/yaoweibin/nginx_upstream_check_module

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

? mv v0.3.0.tar.gz nginx_upstream_check_module.v0.3.0.tar.gz

? tar xf nginx_upstream_check_module.v0.3.0.tar.gz

8、安裝ngx_openresty

? 先檢測(cè)nginx是否安裝 如果安裝后

? 請(qǐng)通過(guò) nginx -V 檢查已經(jīng)安裝過(guò)的模塊等等

? nginx version: nginx/1.10.2

? built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)

? built with OpenSSL 1.0.1e-fips 11 Feb 2013

? TLS SNI support enabled

? configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre


? cd /usr/local/src/openresty-1.11.2.5

? 進(jìn)行編譯安裝

? ./configure --prefix=/usr/local --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-http_realip_module? --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2?

? make && make install

mkdir -p /usr/local/site/lualib /usr/local/site/pod /usr/local/site/manifest

ln -sf /usr/local/nginx/sbin/nginx /usr/local/bin/openresty

? --with***? ? ? ? ? ? ? ? ? 安裝一些內(nèi)置/集成的模塊

? --with-http_realip_module? 取用戶真實(shí)ip模塊

? --with-pcre? ? ? ? ? ? ? ? Perl兼容的達(dá)式模塊

? --with-luajit? ? ? ? ? ? ? 集成luajit模塊

? --add-module? ? ? ? ? ? ? 添加自定義的第三方模塊,如此次的ngx_che_purge

9、到/usr/local/src目錄下

? cd /usr/local/src && ll?

total 340

drwxr-xr-x.? 2 root? root? ? 4096 Feb 26 07:57 bin

-rw-r--r--? 1 root? root? 22924 Feb 26 07:57 COPYRIGHT

drwxr-xr-x.? 2 root? root? ? 4096 Sep 23? 2011 etc

drwxr-xr-x.? 2 root? root? ? 4096 Sep 23? 2011 games

drwxr-xr-x? 11 root? root? ? 4096 May 19? 2017 go

drwxr-xr-x? 6 root? root? ? 4096 Mar? 3? 2017 graphviz

drwxr-xr-x.? 4 root? root? ? 4096 Nov 28 02:57 include

drwxr-xr-x.? 5 root? root? ? 4096 Feb 26 07:27 lib

drwxr-xr-x.? 4 root? root? ? 4096 Nov 22 03:23 lib64

drwxr-xr-x? 6 root? root? ? 4096 Oct 25 04:41 libdatrie

drwxr-xr-x.? 2 root? root? ? 4096 Sep 23? 2011 libexec

drwxr-xr-x? 7 root? root? ? 4096 Nov 26? 2016 libmcrypt

drwxr-xr-x? 6 root? root? ? 4096 Feb 26 07:57 luajit

drwxr-xr-x? 6 root? root? ? 4096 Feb 26 07:57 lualib

drwxr-xr-x? 4 root? root? ? 4096 Nov 28 02:57 man

drwxr-xr-x? 4 root? root? ? 4096 Nov 26? 2016 mcrypt

drwxr-xr-x? 5 root? root? ? 4096 Nov 26? 2016 mhash

drwxr-xr-x? 13 mysql mysql? 4096 Jan 11? 2017 mysql

drwxr-xr-x? 11 root? root? ? 4096 Oct 16 08:19 nginx

drwxr-xr-x? 8 root? root? ? 4096 May? 8? 2017 openresty

drwxr-xr-x? 9 root? root? ? 4096 Nov 26? 2016 php

drwxr-xr-x? 43 root? root? ? 4096 Feb 26 07:57 pod

drwxr-xr-x? 6 redis redis? 4096 Nov 27? 2016 redis

-rw-r--r--? 1 root? root? 216208 Feb 26 07:57 resty.index

drwxr-xr-x.? 2 root? root? ? 4096 Sep 23? 2011 sbin

drwxr-xr-x. 11 root? root? ? 4096 Feb 26 07:27 share

drwxr-xr-x? 5 root? root? ? 4096 Feb 26 07:57 site

drwxr-xr-x. 14 root? root? ? 4096 Feb 26 07:24 src

會(huì)發(fā)現(xiàn)多出來(lái)了如下目錄,說(shuō)明安裝成功

/usr/local/luajit :luajit環(huán)境,luajit類似于java的jit,即即時(shí)編譯,lua是一種解釋語(yǔ)言,通過(guò)luajit可以即時(shí)編譯lua代碼到機(jī)器代碼,得到很好的性能;

/usr/local/lualib:要使用的lua庫(kù),里邊提供了一些默認(rèn)的lua庫(kù),如redis,json庫(kù)等,也可以把一些自己開發(fā)的或第三方的放在這;

/usr/local/nginx :安裝的nginx;

通過(guò)/usr/local/nginx/sbin/nginx? -V 查看nginx版本和安裝的模塊

? ? ? ? nginx version: openresty/1.11.2.5

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis2-nginx-module-0.14 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-http_realip_module --add-module=/usr/local/src/openresty-1.11.2.5/bundle/ngx_cache_purge-2.3 --add-module=/usr/local/src/openresty-1.11.2.5/bundle/nginx_upstream_check_module-0.3.0

10、啟動(dòng)nginx

? 執(zhí)行/usr/local/nginx/sbin/nginx 或者 service nginx restart

? nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

? nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

? Stopping nginx:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

? Starting nginx:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

11、接下來(lái)該配置nginx+lua開發(fā)環(huán)境了

? ? 配置環(huán)境

? ? 配置及Nginx HttpLuaModule文檔在可以查看http://wiki.nginx.org/HttpLuaModule。

? 1)、編輯nginx.conf配置文件

? ? ? vim /usr/local/nginx/conf/nginx.conf?

? 2)、在http部分添加如下配置

? ? ? #lua模塊路徑,多個(gè)之間”;”分隔,其中”;;”表示默認(rèn)搜索路徑,默認(rèn)到/usr/local/nginx下找?

? ? ? lua_package_path "/usr/local/lualib/?.lua;;";? #lua 模塊?

? ? ? lua_package_cpath "/usr/local/lualib/?.so;;";? #c模塊?

? 3)、為了方便開發(fā)我們?cè)?usr/local/nginx/conf目錄下創(chuàng)建一個(gè)lua.conf 內(nèi)容如下:

? ? ? #lua.conf?

? ? ? server {?

? ? ? ? listen? ? ? 81;?

? ? ? ? server_name? _;?

? ? ? }?


? 4)、在nginx.conf中的http部分添加include lua.conf包含此文件片段

? ? ? include lua.conf;?

? 5)、測(cè)試是否正常

? ? ? /usr/local/nginx/sbin/nginx? -t?

? ? ? 如果顯示如下內(nèi)容說(shuō)明配置成功

? ? ? nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

? ? ? nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

? 6)、編寫HelloWorld實(shí)例

? ? ? 在lua.conf中server部分添加如下配置

? ? ? location /lua {?

? ? ? ? ? ? default_type 'text/html';?

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

? ? ? }?


? ? ? 測(cè)試配置是否正確 /usr/local/nginx/sbin/nginx? -t?

? ? ? 重啟nginx /usr/local/nginx/sbin/nginx? -s reload


? ? ? 訪問(wèn)如 http://192.168.1.10:81/lua (自己的機(jī)器根據(jù)實(shí)際情況換ip),可以看到如下內(nèi)容 :? hello world

? 7)、lua代碼文件

? ? 我們把lua代碼放在nginx配置中會(huì)隨著lua的代碼的增加導(dǎo)致配置文件太長(zhǎng)不好維護(hù),因此我們應(yīng)該把lua代碼移到外部文件中存儲(chǔ)、方便管理和維護(hù)


? ? cd /usr/local/nginx/conf? && mkdir lua

? ? vim /usr/local/nginx/conf/lua/test.lua?

? ? #添加如下內(nèi)容?

? ? ngx.say("hello world");

? ? 然后lua.conf修改為?


? ? location /lua {?

? ? default_type 'text/html';?

? ? content_by_lua_file conf/lua/test.lua; #相對(duì)于nginx安裝目錄 此處conf/lua/test.lua也可以使用絕對(duì)路徑/usr/local/nginx/conf/lua/test.lua。

? ? }

? 8)、lua_code_cache 講解

? ? 默認(rèn)情況下lua_code_cache? 是開啟的,即緩存lua代碼,即每次lua代碼變更必須reload nginx才生效,如果在開發(fā)階段可以通過(guò)lua_code_cache? off;

? ? 關(guān)閉緩存,這樣調(diào)試時(shí)每次修改lua代碼不需要reload nginx;但是正式環(huán)境一定記得開啟緩存。

? ? location /lua {?

? ? ? ? default_type 'text/html';?

? ? ? ? lua_code_cache off;?

? ? ? ? content_by_lua_file conf/lua/test.lua;?

? ? }?

? ? 開啟后-t和-s reload nginx會(huì)看到如下報(bào)警

? ? ? ? [root@localhost conf]# nginx -t

nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx/conf/lua.conf:7

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost conf]#

[root@localhost conf]# nginx -s reload

nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx/conf/lua.conf:7

9)、nginx+lua項(xiàng)目構(gòu)建

? 以后我們的nginx lua開發(fā)文件會(huì)越來(lái)越多,我們應(yīng)該把其項(xiàng)目化,已方便開發(fā)。項(xiàng)目目錄結(jié)構(gòu)如下所示:

? luaexample

? ? ? example.conf? ? ---該項(xiàng)目的nginx 配置文件

? ? ? lua? ? ? ? ? ? ? ---我們自己的lua代碼

? ? ? ? test.lua

? ? ? lualib? ? ? ? ? ? ---lua依賴庫(kù)/第三方依賴

? ? ? ? *.lua

? ? ? ? *.so

? 其中我們把lualib也放到項(xiàng)目中的好處就是以后部署的時(shí)候可以一起部署,防止有的服務(wù)器忘記復(fù)制依賴而造成缺少依賴的情況。

? 我們將項(xiàng)目放到到/usr/luaexample目錄下。

? /usr/local/nginx/conf/nginx.conf配置文件如下(此處我們最小化了配置文件)

user nginx nginx;

worker_processes? auto;

error_log? /home/wwwlogs/nginx_error.log? crit;

pid? ? ? ? /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 51200;

events {

? ? use epoll;

? ? worker_connections 51200;

? ? multi_accept on;

}

http {

? ? include? ? ? mime.types;

? ? default_type? application/octet-stream;

? ? log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '

? ? ? '$status $body_bytes_sent "$http_referer" '

? ? ? '"$http_user_agent" "$http_x_forwarded_for"';

? ? server_names_hash_bucket_size 128;

? ? client_header_buffer_size 32k;

? ? large_client_header_buffers 4 32k;

? ? client_max_body_size 50m;

? ? sendfile? ? ? ? on;

? ? tcp_nopush? ? on;

? ? keepalive_timeout? 65;

? ? tcp_nodelay on;

? ? fastcgi_connect_timeout 300;

? ? fastcgi_send_timeout 300;

? ? fastcgi_read_timeout 300;

? ? fastcgi_buffer_size 64k;

? ? fastcgi_buffers 4 64k;

? ? fastcgi_busy_buffers_size 128k;

? ? fastcgi_temp_file_write_size 256k;

? ? gzip? on;

? ? gzip_min_length? 1k;

? ? gzip_buffers? ? 4 16k;

? ? gzip_http_version 1.1;

? ? gzip_comp_level 2;

? ? gzip_types? ? text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;

? ? gzip_vary on;

? ? gzip_proxied? expired no-cache no-store private auth;

? ? gzip_disable? "MSIE [1-6]\.";

? ? server_tokens off;

? ? access_log off;


? ? #lua模塊路徑,多個(gè)之間”;”分隔,其中”;;”表示默認(rèn)搜索路徑,默認(rèn)到/usr/local/nginx下找?

? ? lua_package_path "/usr/local/lualib/?.lua;;";? #lua 模塊?

? ? lua_package_cpath "/usr/local/lualib/?.so;;";? #c模塊?

? ? include /usr/luaexample/example.conf;


? ? server {

? ? ? listen? ? ? 80;

? ? ? server_name? localhost;

? ? ? location / {

? root? html;

? index? index.html index.htm;

? ? ? }

? ? ? error_page? 404? ? ? ? ? ? ? /404.html;

? ? }

? include vhost/*.conf;

}


? 通過(guò)絕對(duì)路徑包含我們的lua依賴庫(kù)和nginx項(xiàng)目配置文件。

? /usr/luaexample/example.conf配置文件如下


? server {?

? ? ? ? listen? ? ? 81;?

? ? ? ? server_name? _;?


? ? ? ? location /lua {?

? ? ? ? ? ? default_type 'text/html';?

? ? ? ? ? ? lua_code_cache off;?

? ? ? ? ? ? content_by_lua_file /usr/luaexample/lua/test.lua;?

? ? ? ? }?

? ? }?

lua文件我們使用絕對(duì)路徑/usr/luaexample/lua/test.lua。

到此我們的nginx+lua基本環(huán)境搭建完畢。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容