资讯专栏INFORMATION COLUMN

Apache下的Lua的配置

AlienZHOU / 3457人阅读

摘要:找到含有的一行,去掉前边的。可能你需要修改的默认文档,在的位置按要求添加即可。写文件首先我们写一个的文件,作用就是会把我们的后缀修改为。

前言

对于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。,内容如下:

</>复制代码

  1. RewriteEngine on
  2. RewriteCond %{REQUEST_FILENAME} info
  3. RewriteRule (.*).php $1.lua [NC]

新建一个info.lua,写入内容如下:

</>复制代码

  1. -- Extend tostring to report function type (C or Lua)
  2. do
  3. local type, tostr = type, tostring
  4. function tostring(obj)
  5. local type, val = type(obj), tostr(obj)
  6. if type == "function" then
  7. type = pcall(coroutine.create, obj) and "Lua " or "C " -- coroutines cannot start at a C function
  8. return type .. val
  9. else
  10. return val
  11. end
  12. end
  13. end
  14. local safe_replacements = {
  15. ["<"] = "<",
  16. [">"] = ">",
  17. ["&"] ="&",
  18. }
  19. local function safestring(...)
  20. return tostring(...):gsub("[<>&]", safe_replacements):gsub("
  21. ", "
  22. ")
  23. end
  24. local function emstring(...)
  25. return """.. safestring(...) .."""
  26. end
  27. local function print_info(info)
  28. print [[
  29. <span class="hljs-attribute">mod_lua</span> <span class="hljs-literal">info</span>
  30. mod_lua

  31. ]]
  32. for group, settings in pairs(info) do
  33. print("
  34. ".. group .. "

  35. ")
  36. print [[
  37. ]]
  38. for key, value in pairs(settings) do
  39. print("
  40. ")
  41. end
  42. print "
    ".. key .."".. value .."
  43. "
  44. end
  45. print [[
  46. ]]
  47. end
  48. local function compile_info(req)
  49. local info = {}
  50. do -- Lua compile options
  51. local dump = string.dump(function() end)
  52. local gc_pause = collectgarbage("setpause", 1); collectgarbage("setpause", gc_pause)
  53. local gc_stepmul = collectgarbage("setstepmul", 2); collectgarbage("setstepmul", gc_stepmul)
  54. info["Lua configuration"] = {
  55. -- Bytecode header is undocumented, see luaU_header in lundump.c
  56. Version = ("%i.%i"):format(math.floor(dump:byte(5) / 16), dump:byte(5) % 16),
  57. Endianness = dump:byte(7) == 1 and "little" or "big",
  58. int = dump:byte(8)*8 .. " bit integer",
  59. size_t = dump:byte(9)*8 .. " bit integer",
  60. ["VM instruction"] = dump:byte(10)*8 .. " bit integer",
  61. Number = dump:byte(11)*8 .. " bit " .. (dump:byte(12) == 1 and "integer" or "float"),
  62. -- package.config is undocumented, see luaopen_package in loadlib.c
  63. ["Path seperator"] = safestring(package.config:sub(1,1)),
  64. ["Lua package path"] = safestring(package.path:gsub(package.config:sub(3,3), "
  65. ")),
  66. ["C package path"] = safestring(package.cpath:gsub(package.config:sub(3,3), "
  67. ")),
  68. -- Garbage collection values _are_ documented :)
  69. ["GC count"] = ("%.0f bytes"):format(collectgarbage"count" * 1024),
  70. ["GC pause"] = ("%.0f%%"):format(gc_pause),
  71. ["GC step multiplier"] = ("%.0f%%"):format(gc_stepmul),
  72. }
  73. end
  74. do -- Globals
  75. local g = {}
  76. for key, value in pairs(getfenv(0)) do
  77. local typev = type(value)
  78. local str
  79. if typev == "table" then
  80. str = safestring(value)
  81. if value ~= getfenv(0) then -- don"t recursively follow _G
  82. str = str .. "
    • "
    • 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 .. "
  83. "
  84. end
  85. elseif typev == "string" then
  86. str = emstring(value)
  87. else
  88. str = safestring(value)
  89. end
  90. g[safestring(key)] = str
  91. end
  92. info.Globals = g
  93. end
  94. do -- Request object
  95. local rinfo = {}
  96. for _, field in pairs{"puts", "write", "document_root", "parseargs", "parsebody", "debug", "info", "notice",
  97. "warn", "err", "crit", "alert", "emerg", "add_output_filter", "assbackwards", "status", "protocol", "range",
  98. "content_type", "content_encoding", "ap_auth_type", "unparsed_uri", "user", "filename", "canonical_filename",
  99. "path_info", "args", "hostname", "uri", "the_request", "method", "headers_in", "headers_out"} do
  100. local value = req[field]
  101. if type(value) == "userdata" and apr_table and apr_table.pairs then
  102. local list = "
    • "
    • for key, value in apr_table.pairs(value) do
    • list = list .. "
    • " .. safestring(key) .. " (" .. emstring(value) .. ")
    • "
    • end
    • rinfo[field] = tostring(req[field]) .. list .. "
  103. "
  104. elseif type(value) == "string" then
  105. rinfo[field] = emstring(req[field])
  106. else
  107. rinfo[field] = safestring(req[field])
  108. end
  109. end
  110. info.Request = rinfo
  111. end
  112. do -- Arguments (query string)
  113. local args = req:parseargs()
  114. local args_clean = {}
  115. for key, value in pairs(args) do
  116. args_clean[safestring(key)] = emstring(value)
  117. end
  118. if next(args_clean) then
  119. info["Query string"] = args_clean
  120. end
  121. end
  122. return info
  123. end
  124. function handle(r)
  125. -- setup the environment
  126. r.content_type = "text/html"
  127. r.headers_out["X-Powered-By"] = "mod_lua; " .. _VERSION
  128. print = function(s) return r:write(tostring(s)) end
  129. -- run the main script
  130. local info = compile_info(r)
  131. print_info(info)
  132. -- finish
  133. return apache2.OK
  134. end
访问Demo

打开Apache,访问http://127.0.0.1/info.php 就能看到

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/35732.html

相关文章

  • 基于 Nginx 动态代理

    摘要:目前最常用的软件反向代理服务器有和。基于实现动态代理为了实现动态代理方案,需要在反向代理服务器中增加定制的功能。同时,由于反向代理服务器需要处理大量的代理请求,因此会频繁的读取反向代理配置数据。 基于 Nginx 的动态代理 作者:赵波日期:2016 年 8 月 4 日 在实际应用中,遇到了这样一个场景: 已有一个手机 APP 客户端,需要在该 APP 客户端中实现通过 Web 的形式...

    wean 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<