Lua作为一门领域语言确实很有特色,利用openresty项目我们可以方便地用lua脚本控制nginx服务器的行为,我不禁想看看在apache服务器中是否也有类似的项目,然后就发现了下面这个mod_lwt模块,那么来试试吧。首先下载mod_lwt项目的安装包,安装之前需要保证
Lua作为一门领域语言确实很有特色,利用openresty项目我们可以方便地用lua脚本控制
nginx服务器的行为,我不禁想看看在apache服务器中是否也有类似的项目,然后就发现了下面这个mod_lwt模块,那么来试试吧。
首先下载
mod_lwt项目的安装包,安装之前需要保证系统环境中已经安装了apache,lua以及liblua,后者可以在lua官网上下载
liblua.so文件,这里使用的是5.1版本(其实只需要去掉lua.c文件编译也可得到liblua.so,为了省事我就直接下载别人编译好的了)。
安装mod_lwt,命令如下
cd mod_lwt
make
make install
安装过程会在/usr/local/share/lua/5.1/这个目录下生成httpd.lua以及httpd/wsapi.lua两个文件
修改apache配置文件,在相应位置添加如下内容 AddHandler lwt .lua
AddHandler lwt-wsapi .ws
LoadModule lwt_module /usr/lib64/apache2/mod_lwt.so
然后调用如下命令启动模块,重启服务器 a2enmod lwt
apache2ctl restart
安装模块就已经完成了,接下来我们用官网的测试脚本test.lua试试,放在网站根目录
require "httpd"
local request_fields = { "uri", "protocol", "hostname", "path", "path_info", "args",
"method", "filename", "filedir", "user", "auth_type",
"local_ip", "remote_ip" }
request, args = ...
httpd.set_content_type("text/plain; charset=utf8")
httpd.write("Hello Lua World\r\n")
for _, key in ipairs(request_fields) do
httpd.write(key .." -> " .. (request[key] or "(not set)") .. "\r\n")
end
然后就可以用浏览器访问test.lua脚本,可以看到如下效果,是不是很类似php脚本动态生成网页啊,呵呵
可见用lua已经可以方便地操作apache服务器的行为,获取数据,产生输出。项目主页上提供了一些基本的http的api供开发者使用
httpd.pairs (apr_table)
httpd.set_status (status)
httpd.set_content_type (content_type)
httpd.add_header (name, value [, err_header])
httpd.add_COOKIE (name [, value [, expires [, path [, domain [, secure [, httponly]]]]]])
httpd.write_template (filename [, flags [, file]])
httpd.escape_uri (string)
httpd.escape_xml (string)
httpd.escape_js (string)
httpd.input
httpd.output
httpd.read (...)
httpd.write (...)
httpd.debug (message)
httpd.notice (message)
httpd.err (message)
httpd.redirect (request, uri, status)
httpd.dump (value)
httpd.match (path_info, ...)
下面还是创建个脚本感受一下Hello World的效果。
require "httpd" -- Import the httpd module
request, args = ... -- Get the request and args values passed to the script
name = args.name or "Unknown" -- Process arguments
httpd.set_content_type("text/plain") -- Set content type
httpd.write("Hello, " .. name .. "!\r\n")
httpd.write("Welcome to " .. request.hostname .. "!\r\n")
用浏览器访问脚本hello.lua?name=ciaos,会输出如下结果 Hello, ciaos!
Welcome to 10.6.2.245!
LWT将业务逻辑独立到lua脚本中来,我们就可以使用lua来实现自己的需求了,同时LWT提供可选的Lua模块用来访问数据库和缓存,真是不错的语言啊。(网上有人测试LWT比python脚本的性能提升120%左右,未查实)
功能特性(官方说明):
Apache module. LWT provides an Apache HTTP server module that
handles requests by invoking Lua scripts. The module provides the
core functionality for creating web applications.
Template engine. LWT provides a template engine that blends
Lua with HTML/XML. The engine supports substitutions and logic in a
way that is natural to both Lua and HTML/XML. This makes it easy to
develop web applications with a clean separation of UI logic in Lua
and layout in HTML.
Full request control. LWT provides full control over web
requests, including request status, content type, input and output.
This faciliates the creation of JSON web services, and other uses
of HTTP.
File uploads. LWT supports HTTP file uploads from a web
browser.
WSAPI. LWT supports the WSAPI standard, and can be used to
run WSAPI applications directly in the Apache HTTP server.
Databases. The IS module supports the access to information
system in an SQL injection safe way. Currently, the module supports
MySQL, Sybase, SQL Server and SQLite.
Caching. The cache module supports the use of caches,
currently
memcached.
除了LWT以外,还有一个模块
mod_plua也是将lua作为脚本嵌入到html代码中,大家可以自行试试。