6

I'm attempting to set up mod_lua module for Apache, but have encountered difficulty regarding accessing third party Lua modules. Say I have a hello_world.lua in Apache's htdocs folder that has something like this:

require "apache2"

function handle(r)
  r.content_type = "text/html"
  r:write "Hello World from <strong>mod_lua</strong>."
  return apache2.OK
end

And I go to "http://localhost/hello_world.lua", that will function as expected. But if I try to add a line such as:

require "socket"

Or

require "cgilua"

I get the following output:

Error!

attempt to call a nil value

However, some modules do work, such as:

require "base"

That functions as expected.

If I navigate to base.lua in the filesystem (c:\program files\lua\5.1\lua\base.lua) and remove this file, then attempt to run my script I get the same error as stated above. So this must be the directory that mod_lua is checking for modules. Modules dlls are not in this folder, instead they are in c:\program files\lua\5.1\clibs\, which I set up the environment variable LUA_CPATH to point to.

Luasocket and cgilua are both present in this folder, yet they cause an error when I try to require them in my script.

From what I can gather, it works fine with any pure lua modules, but anything that has cmodules as well (socket, etc) causes problems.

Additional info:

What needs to be done to be able to require modules in scripts run by mod_lua?

gastrop0d
  • 61
  • 4

1 Answers1

3

It looks like you need to add LuaPackageCPath and/or LuaPackagePath directives to your site configuration (in the global configuration file, or .htaccess, ...).

In your case, I'd assume that

LuaPackagePath c:\program files\lua\5.1\lua\
LuaPackageCPath c:\program files\lua\5.1\clibs\

should do the trick.

kartben
  • 2,485
  • 21
  • 24
  • It doesn't work for me. I've compiled lfs, then I got a `lfs.so` file. If I create a lua file with `require "lfs"` in the same folder as `lfs.so` and launch interactive lua, or `lua myscript.lua` it works. But if I try through `mod_apache` it doesn't work. The error in the log file is `lua: Unable to find function handle in /web/folder/i.lua` – Olivier Pons May 11 '14 at 13:18
  • after add LuaPackagePath C:\Program Files (x86)\Lua\5.1\lib LuaPackageCPath C:\Program Files (x86)\Lua\5.1\clibs – Jeevanantham Feb 01 '16 at 06:46