---
title: "联手lighttpd+lua"
author: "helpbs"
date: "2007-12-31T12:47:00Z"
tags: []
slug: "lighttpd-lua"
draft: false
lastmod: "2012-04-20T18:30:27Z"
id: 1492115
url: "https://helpbs.iteye.com/blog/1492115"
---

系统：rhelg

1）stage1 安装lua

cd /usr/local/src

wget [http://www.lua.org/ftp/lua-5.1.2.tar.gz](http://www.lua.org/ftp/lua-5.1.2.tar.gz)

tar xzvf lua-5.1.2.tar.gz

cd lua-5.1.2

make linux

make install

安装完毕

lua -v

Lua 5.1.2Copyright (C) 1994-2007 Lua.org, PUC-Rio

看来是装好了

2)安装 lighttpd

cd /usr/local/src

wget [http://www.lighttpd.net/download/lighttpd-1.4.18.tar.gz](http://www.lighttpd.net/download/lighttpd-1.4.18.tar.gz)

tar xzvf lighttpd-1.4.18.tar.gz

cd lighttpd-1.4.18

./configure --with-lua=lua5.1

问题1）

出错

checking for LUA... configure: error: Package requirements (lua5.1 &gt;= 5.1) were not met:

No package 'lua5.1' found

Consider adjusting the PKG\_CONFIG\_PATH environment variable if you

installed software in a non-standard prefix.

Alternatively, you may set the environment variables LUA\_CFLAGS

and LUA\_LIBS to avoid the need to call pkg-config.

See the pkg-config man page for more details

看来是不找不到默认路径下lua, 应该是源代码安装的lua路径并不是lighttpd默认的

如果你用rpm装应该不会出问题

于是按照出错信息

export LUA\_CFLAGS= " /usr/local/bin/lua "

export LUA\_LIBS="/usr/local/lib/lua"

这次

./configure --with-lua=lua5.1通过

继续。。。。。。

make && make install

cp doc/rc.lighttpd.redhat /etc/init.d/lightpd

将文件中lighttpd="/usr/sbin/lighttpd"

改为lighttpd="/usr/local/sbin/lighttpd"

cp doc/sysconfig.lighttpd /etc/sysconfig/lighttpd

mkdir /etc/lighttpd

cp -p doc/lighttpd.conf /etc/lighttpd/lighttpd.conf

配置如下：

server.modules = ( ..., "mod\_magnet", ... )

$HTTP\["host"] == "d3.xxxx.com" {

server.document-root = "/ root/"

magnet.attract-physical-path-to = (server.document-root + "/flv-streaming.lua")

}

flv-streaming.lua如下：

header=""

start=0  
if (lighty.env\["uri.query"]) then  
\-- split the query-string  
get = {}  
for k, v in string.gmatch(lighty.env\["uri.query"], "(%w+)=(%w+)") do  
get\[k] = v  
end

if (get and get\["start"]) then  
start = tonumber(get\["start"])  
else  
start=0  
end

-- send te FLV header only when seeking + a seek into the file  
if (start ~= nil and start &gt; 0) then  
header="FLV/1/1/0/0/0/9/0/0/0/9"  
end

end  
lighty.content = { header ,  
{ filename = lighty.env\["physical.path"], offset = start } }  
lighty.header\["Content-Type"] = "video/x-flv"

return 200

启动lighttpd

/etc/init.d/lighttpd start

问题2）

启动 lighttpd：can't handle '$HTTP\[url] =~ ...' as you compiled without pcre support.

(perhaps just a missing pcre-devel package ?)

于是

wget [ftp://rpmfind.net/linux/fedora/releases/7/Everything/i386/os/Fedora/pcre-devel-7.0-2.i386.rpm](ftp://rpmfind.net/linux/fedora/releases/7/Everything/i386/os/Fedora/pcre-devel-7.0-2.i386.rpm)

(根据你机器上的pcre package下载相应pcre-devel)

rpm -ivh pcre-devel-6.6-1.1.i386.rpm

make clean

./configure --with-lua=lua5.1

（察看结果， 这次发现激活的插件含有mod\_rewrite，应该说明有了pcre支持）

Make && make install

/etc/init.d/lighttpd start

问题3）

plugin.c.165) dlopen() failed for: /usr/local/lib/mod\_magnet.so  
/usr/local/lib/mod\_magnet.so: undefined symbol: lua\_getfield  
(server.c.621) loading plugins finally failed

很奇怪的问题，找不到原因，看到网上有人也遇到这个问题，重新编译安装了一把就好了

于是再次重新编译安装lighttpd

/etc/init.d/lighttpd start

这次仍然遇到问题，但和问题3不同

问题4）

启动 lighttpd：2007-12-21 16:29:26: (plugin.c.213) mod\_magnet plugin init failed

2007-12-21 16:29:26: (server.c.621) loading plugins finally failed

于是：

export LUA\_CFLAGS= "-I/usr/local/include"

export LUA\_LIBS= "-L/usr/local/lib -llua"

cd /usr/local/src/lighttpd-1.4.18

make uninstall

./configure --with-lua=lua5.1

Make &&　make install

/etc/init.d/lighttpd start

历经重重困难，终于启动成功！

浏览器输入

ｈｔｔｐ：／／ｄ３．ｘｘｘ．ｃｏｍ／ｔｅｓｔ．ｆｌｖ

似乎还比较块

总结：利用lighttpd快速特性，和能利用lua脚本的灵活性，可构造一个纯web服务器的方案，

无需任何掺和任何应用服务器的东西，可以进行敏捷的部署，对于控制网站的静态

资源有很好的效果。本文的lua脚本你看不出有任何控制意义的代码，但你的确可以通过

lua写控制代码。

参考：

[http://trac.lighttpd.net/trac/wiki/Docs%3AModMagnet](http://trac.lighttpd.net/trac/wiki/Docs%3AModMagnet)