As a weekend project I decided to set up a LAML, (Linux, Apache, MySQL, Lua) stack on my computer. I never worked with Lua before, but I figured it wouldn’t be too hard to get it working with my MAMP setup. Getting started, I spent some time reading any articles I could find on the web that talked about setting up or using using Lua for web development. I quickly ran into the Kepler Project which as their website points out:

is an open source platform that brings the power of Lua to web development

While this was my goal, I wanted to start from scratch since I haven’t had any experience with the language and didn’t want to learn it on a specific platform just yet. So, I went on to figure out how to install and configure Lua with MAMP. To accomplish this yourself, you will need: Xcode, MAMP, Lua, LuaRocks, MAMP source, and mod_fcgid.

Step 1: Install Xcode

This one is pretty straight forward. You can easily install Xcode if you have an OS X install DVD. Otherwise you can download it from the Apple Developer Center.

Step 2: Install Lua

Installing Lua is also straight forward. Download the source from Lua’s FTP site. I chose the current version (5.1.4). After extracting the source run the following commands:

1
2
3
cd /path/to/source/
make macosx
sudo make install

This should install the Lua interpreter into your /usr/local/bin/

Step 3: Install LuaRocks

To install LuaRocks, you first need to download the source. The current release is 2.0.1. After downloading the source run the following commands:

1
2
3
cd /path/to/source/
make
sudo make install

This should install luarocks in /usr/local/bin/ . You can test your installation with:

1
luarocks help

You should be presented with a list of commands to use luarocks.

Step 4: Install Lua MySQL support

This part is a little tricky as you need to make sure you have the source for your MAMP installation. You can download the MAMP source from the MAMP website. Running the .dmg should extract the source. After extracting the source run the following:

1
2
mkdir /Applications/MAMP/Library/Include/
mv /path/to/extracted/mamp/source/ /Applications/MAMP/Library/Include/

Once you have moved your source over, extract the MySQL package then run the following commands:

1
2
3
cd /Applications/MAMP/Library/include/mysql-5.1.37/
./configure
make

Once you have finished building MySQL, you will want to move the mysqlclient into the current MySQL location:

1
cp /Applications/MAMP/Library/include/mysql-5.1.37/libmysql/.libs/*.dylib /Applications/MAMP/Library/lib/mysql/

You will now be able to install the luasql-mysql package from luarocks with the following command:

1
sudo luarocks install luasql-mysql 2.2.0-1 MYSQL_INCDIR=/Applications/MAMP/Library/include/mysql-5.1.37/include MYSQL_LIBDIR=/Applications/MAMP/Library/lib/mysql

Step 5: Install Lua fcgid wrapper

This is an easy one:

1
luarocks install lua wsapi-fcgi 1.1

Step 6: Install Apache mod_fcgid

This part was a bit tricky for me since I don’t have much experience compiling Apache modules. First, you will need to extract the httpd source, for me this was httpd-2.0.63.tar.gz. Next, you will want to build Apache into a separate directory. Use the following to build Apache:

1
2
3
./configure --prefix=/path/to/target/Apache
make
make install

While running the make command, you may get the following error:

1
2
3
sendrecv.c:965:2: error: #error APR has detected sendfile on your system, but nobody has written a
sendrecv.c:966:2: error: #error version of it for APR yet. To get past this, either write apr_sendfile
sendrecv.c:967:2: error: #error or change APR_HAS_SENDFILE in apr.h to 0.

Fixing this is simple, all you need to do is edit /Applications/MAMP/Library/include/httpd-2.0.63/srclib/apr/include/apr.h and make the following modifications:

1
2
-#define APR_HAS_SENDFILE 1
+#define APR_HAS_SENDFILE 0

I know its taken quite some time now, but you are almost done! All that’s left is to build mod_fcgid and configure Apache. To build mod_fcgid run the following commands:

1
2
3
4
APXS=/path/to/target/Apache/bin/apxs ./configure.apxs
make
make install
cp /path/to/target/Apache/modules/mod_fcgid.so /Applications/MAMP/Library/modules

Step 7: Configure Apache for mod_fcgid and Lua

Now that you have everything built and in place, you just need to configure Apache and then you will be ready to start building Lua web apps!

Open /Applications/MAMP/conf/apache2/httpd.conf with your favorite text editor and add the following to the load modules section:

1
LoadModule fcgid_module modules/mod_fcgid.so

While you are at it, you can comment out the following lines if you like (I am not sure if it is possonlble to run mod_cgid with mod_fcgid, so I just took them out).

1
2
#LoadModule cgi_module modules/mod_cgi.so
#LoadModule cgid_module modules/mod_cgid.so

Next, find the IfModule mod_fcgid directive, and add the following under it:

1
2
3
4
5
<ifModule mod_fcgid.c>
AddHandler fcgid-script .lua
FcgidWrapper /Users/avayanis/.luarocks/bin/wsapi.fcgi .lua
Options +ExecCGI
</ifModule>

Finally, restart Apache through the MAMP application and test it out! Here is a sample lua script to test out your Lua environment:

1
2
3
4
5
6
7
8
9
10
module(..., package.seeall)
function run(wsapi_env)
local headers = { ["Content-type"] = "text/html" }
local function hello_text()
coroutine.yield("Hello World!")
end
return 200, headers, coroutine.wrap(hello_text)
end