So far my dynamic web pages have been written using PHP, see:
However recently I have become interested in achieving much the same results using Lua.
There is already a Lua add-on called luacgi, however I found it a bit confusing and poorly documented, so this posting is an attempt to get you started at doing web pages in Lua from first principles.
Install Lua at your web host site
The first step would be to install Lua at your web hosting site, if it is not already installed. If necessary, obtain a copy of the source from:
I used Lua 5.0.2, however Lua version 5.1 has now been released.
In my case, after compiling and installing, my copy of Lua ended up in:
This is important to know, as the path to the Lua executable has to be mentioned in the first line of your web script.
If it is already installed you could find its path by typing:
The basic components of a web page
Web pages sent from a web server to a web browser consist of two major parts, separated by a blank line:
The header contains things like the type of body (text, html, etc.), cookies, date, server type and so on. This is an example header:
The body of the page is the actual data the user sees (generally, the HTML code).
First example, straight text
Let's start off with a simple example dynamic page. The nice thing about using Apache is that it appears to supply most of the headers itself, so for a simple page the only header we need is:
This tells the brower to interpret the body of the web page as pure text (not HTML). Thus, a minimal dynamic web page written in Lua could be this:
Here we are printing (sending to the browser) 3 lines:
The first line of the file indicates the path to the Lua executable.
I saved this file into my "cgi-bin" directory, specifically to:
An important step is to make the file executable, like this:
Having done that, I can now enter this URL into my web browser:
My internal web server is at the local address of 10.0.0.2 - you would replace that with the address of wherever your server is located.
This may not look very exciting, but a small modification, and we can do some maths on our page. Let's make a 2 times table:
If I run this I see a page appear in my browser with this in it:
http://www.php.net/
However recently I have become interested in achieving much the same results using Lua.
There is already a Lua add-on called luacgi, however I found it a bit confusing and poorly documented, so this posting is an attempt to get you started at doing web pages in Lua from first principles.
Install Lua at your web host site
The first step would be to install Lua at your web hosting site, if it is not already installed. If necessary, obtain a copy of the source from:
http://www.lua.org/download.html
I used Lua 5.0.2, however Lua version 5.1 has now been released.
In my case, after compiling and installing, my copy of Lua ended up in:
/usr/local/bin/lua
This is important to know, as the path to the Lua executable has to be mentioned in the first line of your web script.
If it is already installed you could find its path by typing:
whereis lua
The basic components of a web page
Web pages sent from a web server to a web browser consist of two major parts, separated by a blank line:
- The HTTP header
- The body of the page
The header contains things like the type of body (text, html, etc.), cookies, date, server type and so on. This is an example header:
HTTP/1.1 200 OK
Date: Fri, 28 Apr 2006 03:14:58 GMT
Server: Apache/2.0.46 (Unix) PHP/4.3.2
Set-Cookie: foo=bar
Set-Cookie: food=apples
Connection: close
Content-Type: text/html; charset=iso-8859-1
The body of the page is the actual data the user sees (generally, the HTML code).
First example, straight text
Let's start off with a simple example dynamic page. The nice thing about using Apache is that it appears to supply most of the headers itself, so for a simple page the only header we need is:
Content-Type: text/plain
This tells the brower to interpret the body of the web page as pure text (not HTML). Thus, a minimal dynamic web page written in Lua could be this:
#! /usr/local/bin/lua
print [[
Content-Type: text/plain
Hello, world
]]
Here we are printing (sending to the browser) 3 lines:
- The header (content-type)
- The blank line which separates the header from the body)
- The body of the page
The first line of the file indicates the path to the Lua executable.
I saved this file into my "cgi-bin" directory, specifically to:
/usr/local/httpd/cgi-bin/test1.lua
An important step is to make the file executable, like this:
chmod a+x test1.lua
Having done that, I can now enter this URL into my web browser:
http://10.0.0.2/cgi-bin/test1.lua
My internal web server is at the local address of 10.0.0.2 - you would replace that with the address of wherever your server is located.
This may not look very exciting, but a small modification, and we can do some maths on our page. Let's make a 2 times table:
#! /usr/local/bin/lua
-- HTTP header
print [[
Content-Type: text/plain
]]
-- body of page
for i = 1, 10 do
print (i, i * 2)
end -- for
If I run this I see a page appear in my browser with this in it:
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
10 20