<?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>Tupil Code Blog &#187; Apache</title>
	<atom:link href="http://blog.tupil.com/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tupil.com</link>
	<description>(Get up early, code often)</description>
	<lastBuildDate>Fri, 27 Aug 2010 10:50:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Running Happstack applications with FastCGI</title>
		<link>http://blog.tupil.com/running-happstack-applications-with-fastcgi/</link>
		<comments>http://blog.tupil.com/running-happstack-applications-with-fastcgi/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 14:20:46 +0000</pubDate>
		<dc:creator>Eelco Lempsink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[FastCGI]]></category>
		<category><![CDATA[hackathon]]></category>
		<category><![CDATA[Happstack]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[library]]></category>

		<guid isPermaLink="false">http://blog.tupil.com/?p=57</guid>
		<description><![CDATA[This weekend  the 5th Haskell Hackathon happened.  It was hosted by Utrecht University and organized by people from the university and us. Together with Galois and Microsoft Research we sponsored the event. Between running around, listening to interesting presentations and talking to cool people we were able to code up a module enabling us to [...]]]></description>
			<content:encoded><![CDATA[<p>This weekend  the <a href="http://haskell.org/haskellwiki/Hac5">5th Haskell Hackathon</a> happened.  It was hosted by <a href="http://www.uu.nl">Utrecht University</a> and organized by people from the university and us. Together with <a href="http://www.galois.com">Galois</a> and <a href="http://research.microsoft.com">Microsoft Research</a> we sponsored the event.</p>
<p>Between running around, listening to interesting presentations and talking to cool people we were able to code up a module enabling us to use <a href="http://www.happstack.com">Happstack</a> through <a href="http://www.fastcgi.com">FastCGI</a>. This scratches an itch we&#8217;ve had for some time: we&#8217;re running a couple of HAppS/Happstack servers on our server but they&#8217;re configured to work through mod_proxy, which means having to choose an unique port for each application and doing (re)starting the servers by hand.</p>
<p>We just released <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/happstack-fastcgi">happstack-fastcgi on Hackage</a>. This post gives an example of how to use it with Apache and mod_fastcgi.  We assume you configured the extension &#8216;.fcgi&#8217; for FastCGI scripts.</p>
<h2>Converting your Happstack code</h2>
<p>Let&#8217;s look at a &#8216;normal&#8217; Happstack application. Usually, it has a <code>main</code> function that looks something like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Happstack<span style="color: #339933; font-weight: bold;">.</span>Server
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> simpleHTTP nullConf server
&nbsp;
<span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>Where the &#8220;server&#8221; part is something of the type <code>ServerPart a</code>.  To make this work with FastCGI, we need to convert the server parts so that they talk FastCGI.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Happstack<span style="color: #339933; font-weight: bold;">.</span>Server
<span style="color: #06c; font-weight: bold;">import</span> Happstack<span style="color: #339933; font-weight: bold;">.</span>Server<span style="color: #339933; font-weight: bold;">.</span>FastCGI
&nbsp;
simpleCGI <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>ToMessage a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> ServerPartT <span style="color: #cccc00; font-weight: bold;">IO</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
simpleCGI <span style="color: #339933; font-weight: bold;">=</span> runFastCGIConcurrent <span style="color: red;">10</span> <span style="color: #339933; font-weight: bold;">.</span> serverPartToCGI
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> simpleCGI server
&nbsp;
<span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>Assuming the above code is placed in a script called Main.hs, you can compile it with</p>
<pre>
ghc -threaded --make Main.hs -o main.fcgi
</pre>
<p>Notice the &#8216;-threaded&#8217;, because else we won&#8217;t be able to serve request concurrently (other than having FastCGI start an extra instance of our program, which we don&#8217;t want when we&#8217;re working with happstack-state, for example).</p>
<p>So, let&#8217;s fill something in for <code>server</code>, for example</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">server <span style="color: #339933; font-weight: bold;">=</span> withRequest <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">return</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">show</span></pre></div></div>

<p>This will simple dump the request as it has been translated from a CGI request to a Happstack request.</p>
<h2>Running it with Apache</h2>
<p>Just for reference, the <a href='http://www.fastcgi.com/drupal/node/25'>FastCGI documentation</a> is your friend, but he might be a bit long-winded, so I will do a short recap of the basic things.</p>
<p>Let&#8217;s first do the easy thing. If you set up FastCGI correctly and made it the handler for .fcgi scripts, you can just throw the .fcgi file (like we just compiled) anywhere within your document root (provided that the ExecCGI option is enabled for that directory) and go to the correct URL with your browser.</p>
<p>Using scripts in this way is called &#8216;dynamic&#8217; in FastCGI-speak. With a bit more configuration (unfortunately on server/virtual host config level) we can run the script &#8216;statically&#8217;, which means it will be started when Apache is started and (by default) gets one process.  This is particularly important when using happstack-state, things might very well go wrong otherwise. To set up a &#8216;static&#8217; FastCGI script, you must use FastCGI&#8217;s <code>FastCgiServer</code> configuration directive.</p>
<p>Make sure you restart Apache when you change the script (or use FastCGI&#8217;s autoreload setting, which introduces a bit of overhead but is much more convenient).</p>
<h2>Nice URLs and serving static files</h2>
<p>Now you have the FastCGI file running, you might want to do something about the URLs, because doing requests to http://example.com/main.fcgi/some/url doesn&#8217;t really look that nice. As an added advantage, you can now use Apache to serve your static files, reducing the work your Happstack server needs to do.</p>
<p>The traditional .htaccess trickery looks something like this:</p>
<pre>
Options ExecCGI
DirectoryIndex main.fcgi

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) main.fcgi/$1 [PT,L]
</pre>
<p>(If your application is running in a subdirectory, you also need the <code>RewriteBase</code> configuration directive.)</p>
<h2>Closing words</h2>
<p>We did some really simple benchmarks that show that using Happstack through FastCGI, either dynamic or static, is about as fast as using Happstack directly or even mod_proxy (which is pretty fast, but a hassle).  That means the overhead is (as expected) quite small.</p>
<p>At first, we wanted to demo it with Gitit, but unfortunately Gitit or some library that uses Gitit tries to get the PATH environment variable and gives an error when it doesn&#8217;t succeed&#8230; So, if your Happstack application doesn&#8217;t (seem to) work with FastCGI, keep an eye on your logs. </p>
<p>Patches and bugreports are very welcome. You can clone <a href="http://github.com/chriseidhof/happstack-fastcgi">the repository</a> from Github, enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tupil.com/running-happstack-applications-with-fastcgi/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
