源码安装nginx配置Lua模块
1 下载nginx源码
$wget ‘http://sysoev.ru/nginx/nginx-1.2.6.tar.gz’
依赖关系
1. gzip module requires zlib library
2. rewrite module requires pcre library
3. ssl support requires openssl library
2.安装PCRE库
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
tar -zxvf pcre-8.21.tar.gz
cd pcre-8.21
./configure
make
make install
3.安装zlib库
http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:
cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
4.安装ssl(某些vps默认没装ssl)
cd /usr/local/src
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
5.下载lua-nginx-module源码
$git clone https://github.com/chaoslawful/lua-nginx-module.git
$tar zcvf lua-nginx-module
6.编译nginx
$tar -zxvf nginx-1.2.6.tar.gz
$cd nginx-1.2.6.tar.gz
$sudo ./configure –with-debug –with-pcre=/home/onejian/local/pcre-8.21 –with-zlib=/home/onejian/local/zlib-1.2.8 –with-openssl=/home/onejian/local/openssl-1.0.1c
–with-pcre=/home/onejian/local/pcre-8.21 指的是pcre-8.21 的源码路径。
–with-zlib=/home/onejian/local/zlib-1.2.8 指的是zlib-1.2.7 的源码路径。
–with-openssl=/home/onejian/local/openssl-1.0.1c指的是opensll的源码路径
确保系统的 80 端口没被其他程序占用,运行/usr/local/nginx/nginx 命令来启动 Nginx,
netstat -ano|grep 80
如果查不到结果后执行,有结果则忽略此步骤(ubuntu下必须用sudo启动,不然只能在前台运行)
sudo /usr/local/nginx/nginx
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
添加Lua库及echo等其他库
安装Lua
下载lua源码
在lua.org官方主页下载源代码,一开始我直接下的最新版,最后才发现:
ngx_lua does not support Lua 5.2.Only Lua 5.1.x and LuaJIT 2.0 are supported. Seehttp://wiki.nginx.org/HttpLuaModule#Installation
后悔没先看wiki,http://wiki.nginx.org/HttpLuaModule#Installation
$wget http://www.lua.org/ftp/lua-5.1.5.tar.gz
$tar -zxvf lua-5.1.5.tar.gz
$make linux
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lua.o lua.c
在包含自 lua.h:16 的文件中,
从 lua.c:15:
luaconf.h:275:31: 错误:readline/readline.h:没有那个文件或目录
luaconf.h:276:30: 错误:readline/history.h:没有那个文件或目录
安装readline
$wget ftp://ftp.cwru.edu/pub/bash/readline-6.2.tar.gz
$tar -zxvf readline-6.2.tar.gz
$./configure
$make install
再重新编译lua, 提示-lncurses失败,也就是没有ncurses库
/usr/bin/ld: cannot find -lncurses
collect2: ld 返回 1
make[2]: *** [lua] 错误 1
make[2]: Leaving directory `/root/lua-5.1.5/src’
make[1]: *** [linux] 错误 2
make[1]: Leaving directory `/root/lua-5.1.5/src’
make: *** [linux] 错误 2
安装ncurses
$wget ftp://invisible-island.net/ncurses/ncurses-5.9.tar.gz
$tar -zxvf ncurses-5.9.tar.gz
$./configure
$make & make install
查看lua是否安装成功
$lua -v
下载nginx中lua相关module
下载LuaJIT
wget http://luajit.org/download/LuaJIT-2.0.0-beta10.tar.gz
4. 安装LuaJIT
tar -xzvf LuaJIT-2.0.0-beta10.tar.gz
cd LuaJIT-2.0.0-beta10
make && sudo make install
5.下载 ngx_devel_kit(在这个页面找:https://github.com/simpl/ngx_devel_kit/tags)
wget https://github.com/simpl/ngx_devel_kit/tarball/master -O simpl-ngx_devel_kit.tar.gz
6.下载最新的 lua-nginx-module(在这个页面找:https://github.com/chaoslawful/lua-nginx-module/tags)
wget https://github.com/chaoslawful/lua-nginx-module/tarball/master -O lua-nginx-module.tar.gz
重新编译nginx
$tar -zxvf nginx-1.2.6.tar.gz
$cd nginx-1.2.6.tar.gz
$sudo ./configure –with-debug –with-pcre=/home/onejian/local/pcre-8.21 –with-zlib=/home/onejian/local/zlib-1.2.8 –with-openssl=/home/onejian/local/openssl-1.0.1c –add-module=../echo-nginx-module –add-module=../lua-nginx-module-0.9.0 –add-module=../ngx_devel_kit-0.2.19
$make -j2
$make install
提示
/usr/local/lib/liblua.a(loadlib.o): In function `ll_loadfunc’:
loadlib.c:(.text+0x7d4): undefined reference to `dlsym’
loadlib.c:(.text+0x7e2): undefined reference to `dlerror’
loadlib.c:(.text+0x8ad): undefined reference to `dlopen’
loadlib.c:(.text+0x8c3): undefined reference to `dlerror’
/usr/local/lib/liblua.a(loadlib.o): In function `gctm’:
loadlib.c:(.text+0xdbc): undefined reference to `dlclose’
collect2: ld 返回 1
make[1]: *** [objs/nginx] 错误 1
make[1]: Leaving directory `/root/nginx-1.2.7′
make: *** [install] 错误 2
根据http://www.linuxquestions.org/questions/programming-9/linker-problems-cannot-find-dlsym-dlopen-dlerror-dlclose-720199/的提示,在obj/Makefile的对应位置(搜索llua)添加-ldl选项
测试是否成功:
nginxconfig中加入
location /hello {
default_type ‘text/plain’;
content_by_lua ‘ngx.say(“hello, lua”)’;
}
使用/to/nginx/sbin/nginx –t 检查nginx配置,此时应该没有报错
8 /to/nginx/sbin/nginx #启动nginx
或者/to/nginx/sbin/nginx –s reload #重启nginx
访问192.168.100.1/hello
会出现“hello,lua”