To get started with writing web apps (think PHP, ASP etc), you'll need to tell httpd to regard
your scripts as Lua scripts. If we assume that your scripts all end with
.lua, then you'll need
to add the following to your appropriate VirtualHost or global section:
<Files *.lua> SetHandler lua-script </Files>
This will tell httpd, that all of your .lua files will be treated as Lua scripts, and run through mod_lua.
The standard procedure for creating a new Lua script is to make a function called
gets passed a request_rec structure with all of mod_lua's internal variables and
built-in functions, and expects one of several return codes.
A boilerplate web app, which sets a content type and prints out something, could look like this:
function handle(r) r.content_type = "text/html" -- set the output to text/html r:puts("Hello world!") return apache2.OK end
The return code,
apache2.OK basically means "everything went okay, carry on". If you wish to return a specific
HTTP status, you may do so by returning the integer value of that code, such as 404 for
Not found or a 500 for
Internal Server Error.
The entire Lua script will be parsed before the function is run, and as such, it is recommended that you include any external libraries in the global scope:
local foo = require "foo" function handle(r) r.content_type = "text/html" -- set the output to text/html local form = r:parseargs() -- get some GET data local name = form['name'] or "Anonymous" r:puts("Hello ", name, "!") return apache2.OK end
Some other topics you may find interesting: