前言
对于Apdche这个东西,绝大多数人都是非常熟悉的。很多人都会诟病这个Apache,说它效率不高而且非常消耗资源,然后会建议用Nginx。这些不能否认,但是我还是很喜欢Apache,因为它比较稳定。
Apache关于Lua我不知道是哪一个版本编译进去了的,但是最新版的是有的。在Apache的bin目录下有一个lua51.dll很明显,这个是Lua5.1版本的,目前Lua已经到了5.3版本了,如果你想追求新的版本的话,你可以自己把apache编译一次。然后还有,在Apache的modules目录下有一个mod_lua.so是开启Apache和Lua“通信桥梁”的文件。修改配置文件
- 找到含有mod_lua.so的一行,去掉前边的#即可。
- 找到含有mod_rewrite.so的一行,去掉前边的#。
- 可能你需要修改Apache的默认文档,在DirectoryIndex的位置按要求添加即可。
- 将AllowOverride后边的None写为All,表示在整台服务器上都开启了URL重写。
写Demo文件
- 首先我们写一个.htaccess的文件,作用就是会把我们的Lua后缀修改为php。,内容如下:
RewriteEngine onRewriteCond %{REQUEST_FILENAME} infoRewriteRule (.*).php $1.lua [NC]
- 新建一个info.lua,写入内容如下:
-- Extend tostring to report function type (C or Lua)do local type, tostr = type, tostring function tostring(obj) local type, val = type(obj), tostr(obj) if type == "function" then type = pcall(coroutine.create, obj) and "Lua " or "C " -- coroutines cannot start at a C function return type .. val else return val end endendlocal safe_replacements = { ["<"] = "<", [">"] = ">", ["&"] ="&",}local function safestring(...) return tostring(...):gsub("[<>&]", safe_replacements):gsub("\n", "\n")endlocal function emstring(...) return """.. safestring(...) .."""endlocal function print_info(info) print [[mod_lua info ]]endlocal function compile_info(req) local info = {} do -- Lua compile options local dump = string.dump(function() end) local gc_pause = collectgarbage("setpause", 1); collectgarbage("setpause", gc_pause) local gc_stepmul = collectgarbage("setstepmul", 2); collectgarbage("setstepmul", gc_stepmul) info["Lua configuration"] = { -- Bytecode header is undocumented, see luaU_header in lundump.c Version = ("%i.%i"):format(math.floor(dump:byte(5) / 16), dump:byte(5) % 16), Endianness = dump:byte(7) == 1 and "little" or "big", int = dump:byte(8)*8 .. " bit integer", size_t = dump:byte(9)*8 .. " bit integer", ["VM instruction"] = dump:byte(10)*8 .. " bit integer", Number = dump:byte(11)*8 .. " bit " .. (dump:byte(12) == 1 and "integer" or "float"), -- package.config is undocumented, see luaopen_package in loadlib.c ["Path seperator"] = safestring(package.config:sub(1,1)), ["Lua package path"] = safestring(package.path:gsub(package.config:sub(3,3), "\n")), ["C package path"] = safestring(package.cpath:gsub(package.config:sub(3,3), "\n")), -- Garbage collection values _are_ documented :) ["GC count"] = ("%.0f bytes"):format(collectgarbage"count" * 1024), ["GC pause"] = ("%.0f%%"):format(gc_pause), ["GC step multiplier"] = ("%.0f%%"):format(gc_stepmul), } end do -- Globals local g = {} for key, value in pairs(getfenv(0)) do local typev = type(value) local str if typev == "table" then str = safestring(value) if value ~= getfenv(0) then -- don't recursively follow _G str = str .. "]] for group, settings in pairs(info) do print('
mod_lua
'.. group .. "
") print [[]] for key, value in pairs(settings) do print("
\n" end print [[\n") end print " ".. key .." ".. value .."
- " for field, v in pairs(value) do str = str .. "
- " .. safestring(field) .. " (" if type(v) == "string" then str = str .. emstring(v) else str = str .. safestring(v) end str = str .. ") " end str = str .. "
- " for key, value in apr_table.pairs(value) do list = list .. "
- " .. safestring(key) .. " (" .. emstring(value) .. ") " end rinfo[field] = tostring(req[field]) .. list .. "
访问Demo
打开Apache,访问 就能看到