Nginx的nb之處就不說了,lua也是一個小巧的腳本語言,由標准C編寫而成,幾乎可以運行在所有的平台上,也非常強大,其他特性請自行度娘。
nginx_lua_module是由淘寶的工程師清無(王曉哲)和春來(章亦春)所開發的nginx第三方模塊,它能將lua語言嵌入到nginx配置中,從而使用lua就極大增強了nginx的能力 http://wiki.nginx.org/HttpLuaModule
下面說說mac下Nginx如何編譯集成nginx_lua_module模塊
1. 下載nginx需要的模塊源碼
lua-nginx-module-0.10.5,LuaJIT,ngx_devel_kit-0.3.0,openssl,zlib
還有主要的nginx源碼,具體下載地址就搜索一下吧,版本隨時也會變化
2. 編譯安裝
2.1. 下載安裝PCRE庫
PCRE是為了重寫URL的rewrite
如果本機安裝了brew就比較方便了,直接 brew install PCRE 即可自動下載安裝。否則請下載源碼
./configure make make install
2.2. 下載安裝zlib庫
zlib是為了gzip壓縮使用。Brew上好像沒有,所以還是源碼安裝。
cd /Users/hecom/Downloads/zlib-1.2.8 ./configure make make install
2.3. 下載安裝ssl庫
下載源碼,解壓
cd /Users/hecom/Downloads/openssl-1.0.1t ./configure make make install
2.4. 下載安裝luajit庫
使用源碼安裝,http://luajit.org/download.html
cd /Users/hecom/Downloads/ LuaJIT-2.0.4t ./configure make make install
lib和include的默認安裝位置在/usr/local/lib和usr/local/include文件夾下面,這個很重要,需要導入環境變量使用。
2.5. 下載lua-nginx-module並解壓
我的本地路徑:/Users/hecom/Downloads/lua-nginx-module-0.10.5
注意版本號
2.6. 下載ngx_devel_kit並解壓
我的本地路徑:/Users/hecom/Downloads/ngx_devel_kit-0.3.0
注意版本號
2.7. 下載nginx並解壓
我的本地路徑:/Users/hecom/Downloads/nginx-1.11.1
注意版本號
2.8. 導入環境變量(很重要)
export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.0/
如果nginx無法啟動,報下面錯誤,請檢測是否執行該步驟。
[error] failed to initialize Lua VM in /usr/local/nginx/conf/nginx.conf:44
2.9. 編譯安裝nginx
cd /Users/hecom/Downloads/nginx-1.11.1
./configure --prefix=/usr/local/nginx \ #nginx安裝位置 #--with-openssl=/Users/hecom/Downloads/openssl-1.0.1t \ #openssl的源碼路徑,如果不是自己編譯,則不需要這個參數。
--with-http_ssl_module \ #開啟ssl模塊 --with-http_stub_status_module \ --with-zlib=/Users/hecom/Downloads/zlib-1.2.8 \ #zlib源碼路徑 --add-module=/Users/hecom/Downloads/lua-nginx-module-0.10.5 \ #源碼路徑 --add-module=/Users/hecom/Downloads/ngx_devel_kit-0.3.0 #ngx_devel源碼路徑
make make install
上面命令請在root命令下執行,即在前面加上sudo, 各個源碼路徑請根據自己的位置修改。
自己編譯openssl上走了很多彎路,一直報錯:ld: symbol(s) not found for architecture x86_64,編譯openssl時加 ./Configure darwin64-x86_64-cc 也不行,最后沒有搞定,使用系統自帶的版本通過了
3. 測試nginx
如果幸運不出錯的話,nginx變安裝成功了。輸入下面命令啟動nginx並測試,sudo /usr/local/nginx/sbin/nginx,然后在瀏覽器中輸入:http://localhost/,如果看到下面畫面恭喜你,成功了!
4. 測試lua
打開/usr/local/nginx/conf/nginx.conf文件,添加lua腳本。
location /hello { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; }
然后執行 sudo /usr/local/nginx/sbin/nginx –s reload
在瀏覽器中輸入 http://localhost/hello,看到輸出 “hello,lua”
否則根據錯誤日志排查原因