<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Andrew Vayanis &#187; Lua</title>
	<atom:link href="http://www.vayanis.com/category/lua/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vayanis.com</link>
	<description>Developer, Gamer, Thinker</description>
	<lastBuildDate>Wed, 19 May 2010 15:58:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Setting up LAML (Linux, Apache, MySQL, Lua) with MAMP</title>
		<link>http://www.vayanis.com/2010/02/04/setting-up-laml-linux-apache-mysql-lua-with-mamp/</link>
		<comments>http://www.vayanis.com/2010/02/04/setting-up-laml-linux-apache-mysql-lua-with-mamp/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 05:00:04 +0000</pubDate>
		<dc:creator>Andrew Vayanis</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Guides]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.vayanis.com/?p=48</guid>
		<description><![CDATA[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&#8217;t be too hard to get it working with my MAMP setup.  Getting started, I spent some time reading any articles I could find [...]]]></description>
			<content:encoded><![CDATA[<p>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&#8217;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 <a href="http://www.keplerproject.org/">Kepler Project</a> which as their website points out:</p>
<blockquote><p>
is an open source platform that brings the power of Lua to web development
</p></blockquote>
<p>While this was my goal, I wanted to start from scratch since I haven&#8217;t had any experience with the language and didn&#8217;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.<br />
<span id="more-48"></span></p>
<h3>Step 1: Install Xcode</h3>
<p>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 <a href="http://developer.apple.com/technology/xcode.html">Apple Developer Center</a>.</p>
<h3>Step 2: Install Lua</h3>
<p>Installing Lua is also straight forward.  Download the source from <a href="http://www.lua.org/ftp/">Lua&#8217;s FTP site</a>.  I chose the current version (5.1.4).  After extracting the source run the following commands:<br />
<code><br />
cd /path/to/source/<br />
make macosx<br />
sudo make install<br />
</code><br />
This should install the Lua interpreter into your /usr/local/bin/</p>
<h3>Step 3: Install LuaRocks</h3>
<p>To install LuaRocks, you first need to download the <a href="http://luarocks.org/releases/">source</a>.  The current release is 2.0.1.  After downloading the source run the following commands:<br />
<code><br />
cd /path/to/source/<br />
make<br />
sudo make install<br />
</code><br />
This should install luarocks in /usr/local/bin/ .  You can test your installation with:<br />
<code>luarocks help</code><br />
You should be presented with a list of commands to use luarocks.</p>
<h3>Step 4: Install Lua MySQL support</h3>
<p>This part is a little tricky as you need to make sure you have the source for your MAMP installation.  You can download the <a href="http://sourceforge.net/projects/mamp/files/mamp/1.8.2/MAMP_components_1.8.2.dmg.zip/download">MAMP source</a> from the <a href="http://www.mamp.info/en/index.html">MAMP website</a>.  Running the .dmg should extract the source.  After extracting the source run the following:<br />
<code><br />
mkdir /Applications/MAMP/Library/Include/<br />
mv /path/to/extracted/mamp/source/ /Applications/MAMP/Library/Include/<br />
</code><br />
Once you have moved your source over, extract the MySQL package then run the following commands:<br />
<code><br />
cd /Applications/MAMP/Library/include/mysql-5.1.37/<br />
./configure<br />
make<br />
</code><br />
Once you have finished building MySQL, you will want to move the mysqlclient into the current MySQL location:<br />
<code><br />
cp /Applications/MAMP/Library/include/mysql-5.1.37/libmysql/.libs/*.dylib /Applications/MAMP/Library/lib/mysql/<br />
</code><br />
You will now be able to install the luasql-mysql package from luarocks with the following command:<br />
<code><br />
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<br />
</code></p>
<h3>Step 5: Install Lua fcgid wrapper</h3>
<p>This is an easy one:<br />
<code><br />
luarocks install lua wsapi-fcgi 1.1<br />
</code></p>
<h3>Step 6: Install Apache mod_fcgid</h3>
<p>This part was a bit tricky for me since I don&#8217;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:<br />
<code><br />
./configure --prefix=/path/to/target/Apache<br />
make<br />
make install<br />
</code><br />
While running the make command, you may get the following error:</p>
<p><code><br />
sendrecv.c:965:2: error: #error APR has detected sendfile on your system, but nobody has written a<br />
sendrecv.c:966:2: error: #error version of it for APR yet. To get past this, either write apr_sendfile<br />
sendrecv.c:967:2: error: #error or change APR_HAS_SENDFILE in apr.h to 0.<br />
</code></p>
<p>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:<br />
<code><br />
-#define APR_HAS_SENDFILE  1<br />
+#define APR_HAS_SENDFILE  0<br />
</code></p>
<p>I know its taken quite some time now, but you are almost done!  All that&#8217;s left is to build mod_fcgid and configure Apache.  To build mod_fcgid run the following commands:<br />
<code><br />
APXS=/path/to/target/Apache/bin/apxs ./configure.apxs<br />
make<br />
make install<br />
cp /path/to/target/Apache/modules/mod_fcgid.so /Applications/MAMP/Library/modules<br />
</code></p>
<h3>Step 7: Configure Apache for mod_fcgid and Lua</h3>
<p>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!</p>
<p>Open /Applications/MAMP/conf/apache2/httpd.conf with your favorite text editor and add the following to the load modules section:<br />
<code><br />
LoadModule fcgid_module modules/mod_fcgid.so<br />
</code><br />
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).<br />
<code><br />
#LoadModule cgi_module modules/mod_cgi.so<br />
#LoadModule cgid_module modules/mod_cgid.so<br />
</code><br />
Next, find the IfModule mod_fcgid directive, and add the following under it:<br />
<code><br />
<IfModule mod_fcgid.c><br />
	AddHandler fcgid-script .lua<br />
	FcgidWrapper /Users/avayanis/.luarocks/bin/wsapi.fcgi .lua<br />
	Options +ExecCGI<br />
</IfModule><br />
</code><br />
Finally, restart Apache through the MAMP application and test it out!  Here is a sample lua script to test out your Lua environment:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">module<span style="color: #66cc66;">&#40;</span>..., package.seeall<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #b1b100;">function</span> run<span style="color: #66cc66;">&#40;</span>wsapi_env<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">local</span> headers <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Content-type&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;text/html&quot;</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">function</span> hello_text<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">coroutine.yield</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Hello World!&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">200</span>, headers, <span style="color: #b1b100;">coroutine.wrap</span><span style="color: #66cc66;">&#40;</span>hello_text<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.vayanis.com/2010/02/04/setting-up-laml-linux-apache-mysql-lua-with-mamp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
