<?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"
	>

<channel>
	<title>Foliosus &#187; Lighttpd</title>
	<atom:link href="http://www.foliosus.com/category/web-technology/lighttpd/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.foliosus.com</link>
	<description>Plants, food and web design</description>
	<pubDate>Mon, 05 May 2008 23:27:50 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>HOWTO: Lighttpd with SSL, Rails, PHP and MySQL on OSX 10.4 (Tiger)</title>
		<link>http://www.foliosus.com/2006/04/04/howto-lighttpd-with-ssl-rails-php-and-mysql-on-osx-104-tiger/</link>
		<comments>http://www.foliosus.com/2006/04/04/howto-lighttpd-with-ssl-rails-php-and-mysql-on-osx-104-tiger/#comments</comments>
		<pubDate>Tue, 04 Apr 2006 17:48:44 +0000</pubDate>
		<dc:creator>Brent Miller</dc:creator>
		
		<category><![CDATA[Lighttpd]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.foliosus.com/2006/04/04/howto-lighttpd-with-ssl-rails-php-and-mysql-on-osx-104-tiger/</guid>
		<description><![CDATA[I&#8217;ve run Apache on my Mac for as long as I&#8217;ve had OSX, using either the built-in version or the ServerLogistics package, which they don&#8217;t make anymore.  It always worked great, and when I started learning PHP, it was easy to install and make work.  Getting SSL to work was a little bit [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve run Apache on my Mac for as long as I&#8217;ve had OSX, using either the built-in version or the <a href="http://www.serverlogistics.com/" title="Good installer package for MySQL">ServerLogistics</a> package, which they don&#8217;t make anymore.  It always worked great, and when I started learning PHP, it was <a href="http://www.entropy.ch/software/macosx/php/" title="PHP installer package">easy to install</a> and make work.  Getting SSL to work was a little bit more of a challenge, but once I found <a href="http://forums.serverlogistics.com/viewtopic.php?t=494" title="SSL for Apache 2.0">the right instructions</a> it was a breeze.</p>
<p>But now I need to serve Rails as well.  Getting Apache to do RoR with fcgi can be a hassle, and it&#8217;s apparently slow as well, so I decided to switch to <a href="www.lighttpd.net" title="Lighty home">lighttpd</a>, or lighty as it&#8217;s called.</p>
<p>For development purposes, using the standard <span class="code">script/server</span> is a no-brainer; however I want a &#8220;production&#8221; environment as well.  One that can serve Rails, and legacy PHP, and do SSL for authentication, while talking to MySQL.  I discovered that lighty can do all of these things, but how?<span id="more-34"></span></p>
<p>I looked around, and once you get to the deployment phase of RoR, there isn&#8217;t a lot of good information on the web, especially for smaller set-ups like mine; essentially, I&#8217;m almost the only person who hits my web server, as it houses some personal productivity stuff just for me that most other people don&#8217;t find interesting.  That and my photo collection for friends to browse.</p>
<p>So I looked and looked, and found a lot of disjointed bits and bobs, but I didn&#8217;t find a good how-to for non-unix geeks.  But, given the ease of use of lighty, I&#8217;ve put one together.  Enjoy!</p>
<h3>Requirements</h3>
<ol>
<li>Tiger (OSX 10.4)</li>
<li>XCode 2.0 or newer and developer&#8217;s tools</li>
<li>Patience and a nice snack while waiting for compiling to happen</li>
</ol>
<h3 id="rubyetc">Building Ruby, Gems, RoR, FastCGI, PCRE</h3>
<p>Go to <a href="http://hivelogic.com/articles/2005/12/01/ruby_rails_lighttpd_mysql_tiger">Dan Benjamin&#8217;s excellent instructions</a> to do all of these things, and follow them well.  However, stop when you get to the lighttpd instructions, because we&#8217;re going to make a change.  Do this instead of what he says:</p>
<pre class="code" name="lighttpd">curl -O http://lighttpd.net/download/lighttpd-1.4.11.tar.gz
tar xzvf lighttpd-1.4.11.tar.gz
cd lighttpd-1.4.11
./configure --prefix=/usr/local --with-pcre=/usr/local --with-openssl
make
sudo make install
cd ..</pre>
<p>The only difference is the addition of <span class="code">&#8211;with-openssl</span> to the configuration, which will compile lighttpd with SSL (v2 and v3) support.</p>
<h3 id="mysql">Building MySQL</h3>
<p>You can choose to build MySQL according to <a href="http://hivelogic.com/articles/2005/12/01/ruby_rails_lighttpd_mysql_tiger">the HiveLogic instructions</a>, or you can use the <a href="http://dev.mysql.com/downloads/mysql/5.0.html#Mac_OS_X" title="MySQL 5.0 download page">MySQL 5.0 packages</a> from the dolphin&#8217;s mouth, so to speak, and avoid the compile time.  This works for <a href="http://dev.mysql.com/downloads/mysql/4.1.html#Mac_OS_X" title="MySQL 4.1 download page">version 4.1</a> as well.  I&#8217;ve tried both versions with the setup described here, and both work.</p>
<p>Don&#8217;t forget to install the MySQL native bindings (again, from <a href="http://hivelogic.com/articles/2005/12/01/ruby_rails_lighttpd_mysql_tiger">the HiveLogic instructions</a>) as well.</p>
<h3>But, I&#8217;ve already got this stuff working!</h3>
<p>Chances are, you&#8217;ve already got a full setup that works, because you saw <a href="http://hivelogic.com/articles/2005/12/01/ruby_rails_lighttpd_mysql_tiger">the HiveLogic instructions</a> long before I ever wrote this page.  If that&#8217;s the case, then all you need to do is follow <a href="#lighttpd">my lighttpd compile instructions</a>, which will over-write the pre-existing lighttpd version, with no ill effects.</p>
<h3 id="php">Building PHP</h3>
<p>First, <a href="http://www.php.net/downloads.php" title="PHP download page">download the PHP 5.1.2 full source code</a>, and unpack the tarball by double-clicking it in the finder.  I would recommend moving the unpacked &#8220;php-5.1.2&#8243; folder to the same folder where you downloaded all of the source code from <a href="http://hivelogic.com/articles/2005/12/01/ruby_rails_lighttpd_mysql_tiger">the hivelogic instructions</a>.</p>
<p>Second, open a terminal, and execute the following commands:</p>
<pre class="code">./configure --enable-fastcgi --enable-discard-path --enable-force-redirect --with-zlib --with-xml --with-mysql=/usr/local/mysql --prefix=/usr/local/php5-fcgi --disable-cli --enable-memory-limit --with-layout=GNU --with-regex=php
make
sudo make install</pre>
<p>When this is done, you&#8217;ll have a working cgi-fcgi version of PHP.  We&#8217;ll hook this up to lighttpd in a minute, when we get to the lighty configuration files.</p>
<h3 id="ssl">SSL</h3>
<p>Before we start configuring lighty, we&#8217;ve got to get an SSL certificate.  If you&#8217;re creating your own, you can follow the instructions from the lighty web site:</p>
<pre class="code">openssl req -new -x509 -keyout host.pem -out host.pem -days 365 -nodes</pre>
<p>If you already have a certificate that&#8217;s a .crt and a .key file, you have to make them snuggle up into a single .pem file:</p>
<pre class="code">cat host.key host.crt > host.pem</pre>
<p>Both of these methods result in a single .pem file, usually named &#8220;host.pem&#8221; where &#8220;host&#8221; is the name of the server you&#8217;re using the certificate for.  This file can be stored anywhere you want on your system, since the lighty configuration takes an explicit path to it.</p>
<h3 id="lighttpdconfig">Configuring lighttpd</h3>
<p>Now the more difficult part: we get to configure lighty.  I&#8217;ll present the config files one chunk at a time, explaining what the various commands do.  I&#8217;ve chosen to store mine in <span class="code">/etc/lighttpd/</span> to mimic how Apache stores its files (<span class="code">/etc/httpd/</span>).  Any path will do, just change the references to that directory in the coming intsructions.</p>
<p>First, we&#8217;ll set up 2 top-level files, which are both quite simple.  They set up two nearly identical configurations for the lighty daemon, that run side-by-side.  The first scans port 80, and serves http requests.  Let&#8217;s call this file <span class="code">lighttpd.conf</span>.</p>
<pre class="code">include "lighttpd_shared.conf"

server.port = 80

server.errorlog    = "/etc/lighttpd/lighttpd.error.log"
accesslog.filename = "/etc/lighttpd/lighttpd.access.log"</pre>
<p>The first line causes lighty to parse the main configuration file, which we&#8217;ll go through below.  The next line binds lighty to port 80, the default for http, and then we assign error log files.</p>
<p>We do something very similar for the other top-level file, the one that configures the SSL daemon.  I&#8217;ve called mine <span class="code">lighttpd_ssl.conf</span>.</p>
<pre class="code">include "lighttpd_shared.conf"

server.port = 443

server.errorlog    = "/etc/lighttpd/lighttpd_ssl.error.log"
accesslog.filename = "/etc/lighttpd/lighttpd_ssl.access.log"

ssl.engine         = "enable"
ssl.pemfile        = "/etc/lighttpd/host.pem"</pre>
<p>In this case, we&#8217;ve bound the daemon to port 443, the default for secure connections.  We&#8217;ve specified log files, and then enabled the SSL engine and specified the path to the .pem file.</p>
<p>By the way, the log files must exist before you run the daemon, and have the right permissions.  To do this, simply touch the files, assign them to &#8220;www&#8221; and set the permissions:</p>
<pre class="code">cd /etc/lighttpd
touch lighttpd.error.log
touch lighttpd.access.log
touch lighttpd_ssl.error.log
touch lighttpd_ssl.access.log
sudo chown www *.log
sudo chmod 0666 *log</pre>
<p>Now, on to the meat of the matter.  Both of our top-level files parse the <span class="code">lighttpd_shared.conf</span> file, so that both daemons run in parallel and do the same things.  This means that all pages in our web server can be served with a regular or a secure connection.  Here&#8217;s the meat:</p>
<pre class="code">server.modules = ( "mod_rewrite",
                   "mod_access",
                   "mod_fastcgi",
                   "mod_userdir",
                   "mod_accesslog" )

# Main folder containing web documents
server.document-root = "path/to/main/"

# Allow http://www.domain.com/~username/ style requests
userdir.basepath = "/Users/"
userdir.path = "Sites"
userdir.include-user = ("username") # only allow requests for this user (optional)

# If no file is specified, what to look for?
index-file.names = ( "index.html", "index.htm", "index.php" )

# Required event handler for OS X
server.event-handler = "freebsd-kqueue"

# Run the server under the user-name "www" for security purposes
# To bind to port 80, the server must be called by root user, but we don't want
# the server to have free run of the box, so it runs as "www"
server.username = "www"
server.groupname = "www"

# Set up the appropriate MIME type mappings
mimetype.assign             = (
  ".pdf"          =>      "application/pdf",
  ".sig"          =>      "application/pgp-signature",
  ".spl"          =>      "application/futuresplash",
  ".class"        =>      "application/octet-stream",
  ".ps"           =>      "application/postscript",
  ".torrent"      =>      "application/x-bittorrent",
  ".dvi"          =>      "application/x-dvi",
  ".gz"           =>      "application/x-gzip",
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
  ".swf"          =>      "application/x-shockwave-flash",
  ".tar.gz"       =>      "application/x-tgz",
  ".tgz"          =>      "application/x-tgz",
  ".tar"          =>      "application/x-tar",
  ".zip"          =>      "application/zip",
  ".mp3"          =>      "audio/mpeg",
  ".m3u"          =>      "audio/x-mpegurl",
  ".wma"          =>      "audio/x-ms-wma",
  ".wax"          =>      "audio/x-ms-wax",
  ".ogg"          =>      "application/ogg",
  ".wav"          =>      "audio/x-wav",
  ".gif"          =>      "image/gif",
  ".jpg"          =>      "image/jpeg",
  ".jpeg"         =>      "image/jpeg",
  ".png"          =>      "image/png",
  ".xbm"          =>      "image/x-xbitmap",
  ".xpm"          =>      "image/x-xpixmap",
  ".xwd"          =>      "image/x-xwindowdump",
  ".css"          =>      "text/css",
  ".html"         =>      "text/html",
  ".htm"          =>      "text/html",
  ".js"           =>      "text/javascript",
  ".asc"          =>      "text/plain",
  ".c"            =>      "text/plain",
  ".cpp"          =>      "text/plain",
  ".log"          =>      "text/plain",
  ".conf"         =>      "text/plain",
  ".text"         =>      "text/plain",
  ".txt"          =>      "text/plain",
  ".dtd"          =>      "text/xml",
  ".xml"          =>      "text/xml",
  ".mpeg"         =>      "video/mpeg",
  ".mpg"          =>      "video/mpeg",
  ".mov"          =>      "video/quicktime",
  ".qt"           =>      "video/quicktime",
  ".avi"          =>      "video/x-msvideo",
  ".asf"          =>      "video/x-ms-asf",
  ".asx"          =>      "video/x-ms-asf",
  ".wmv"          =>      "video/x-ms-wmv",
  ".bz2"          =>      "application/x-bzip",
  ".tbz"          =>      "application/x-bzip-compressed-tar",
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
 )

# Don't server these files statically, for security
static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc", ".pl", ".yml" )

# Use mod_access to deny direct access to files ending in ~ or .inc; these are usually
# code or backup files
url.access-deny             = ( "~", ".inc" )

# Disable auto-generated directory listings, for security
dir-listing.activate        = "disable"

# Disable range requests for PDF files
$HTTP["url"] =~ &#8220;\.pdf$&#8221; {
  server.range-requests = &#8220;disable&#8221;
}

# Set up a fastcgi server for PHP
fastcgi.server = ( &#8220;.php&#8221; => ((
                     &#8220;bin-path&#8221; => &#8220;/usr/local/php5-fcgi/bin/php&#8221;, # path to php binary
                     &#8220;socket&#8221; => &#8220;/tmp/php.socket&#8221;,
                 )))

# Now create virtual subdomains for each rails app we want; myrailsapp.domain.com
# Repeat the following block for each rails app, and for each one
# replace &#8220;myrailsapp&#8221; with the name of your application
# This sets up separate fcgi processes for each application

$HTTP["host"] =~ &#8220;^myrailsapp\.&#8221; {
     var.myrailsapp = &#8220;/path/to/rails/app&#8221;
     server.document-root = var. myrailsapp + &#8220;/public&#8221;
     server.error-handler-404 = &#8220;/dispatch.fcgi&#8221;
     fastcgi.server = ( &#8220;.fcgi&#8221; =>
                     ( &#8220;localhost&#8221; =>
                         ( &#8220;bin-environment&#8221; => (&#8221;RAILS_ENV&#8221; => &#8220;production&#8221;),
                           &#8220;bin-path&#8221; => var. myrailsapp + &#8220;/public/dispatch.fcgi&#8221;,
                            &#8220;socket&#8221; => &#8220;/tmp/myrailsapp.fcgi.socket&#8221;
                         )
                      )
                 )
}</pre>
<p>You can tweak the fastcgi performance by using <a href="http://www.lighttpd.net/documentation/fastcgi.html" title="Lighttpd fastcgi configuration options">the lighttpd instructions</a> for directives such as &#8220;min-procs&#8221; and &#8220;max-procs&#8221; but that&#8217;s up to you.</p>
<h3 id="launchd">Lighty at startup</h3>
<p>Wouldn&#8217;t it be nice if lighty launched at startup, and we didn&#8217;t have to worry about it?  How about if it relaunched whenever it crashed?  That would be even better.  Save the following to <span class="code">/Library/LaunchDaemons/net.lighttpd.plist</span> and watch the magic happen, thanks to <a href="http://textsnippets.com/posts/show/124" title="Original launchd instructions">froehle</a>:</p>
<pre class="code">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
	&lt;key&gt;Label&lt;/key&gt;
	&lt;string&gt;net.lighttpd&lt;/string&gt;
	&lt;key&gt;OnDemand&lt;/key&gt;
	&lt;false /&gt;
	&lt;key&gt;Program&lt;/key&gt;
	&lt;string&gt;/usr/local/sbin/lighttpd&lt;/string&gt;
	&lt;key&gt;ProgramArguments&lt;/key&gt;
	&lt;array&gt;
		&lt;string&gt;/usr/local/sbin/lighttpd&lt;/string&gt;
		&lt;string&gt;-f/path/to/lighttpd.conf&lt;/string&gt;
		&lt;string&gt;-D&lt;/string&gt;
	&lt;/array&gt;
&lt;/dict&gt;
&lt;/plist&gt;</pre>
<p>Oh, wait, before the magic, there&#8217;s one more (<span class="code">/Library/LaunchDaemons/net.lighttpd_ssl.plist</span>):</p>
<pre class="code">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
	&lt;key&gt;Label&lt;/key&gt;
	&lt;string&gt;net.lighttpd_ssl&lt;/string&gt;
	&lt;key&gt;OnDemand&lt;/key&gt;
	&lt;false /&gt;
	&lt;key&gt;Program&lt;/key&gt;
	&lt;string&gt;/usr/local/sbin/lighttpd&lt;/string&gt;
	&lt;key&gt;ProgramArguments&lt;/key&gt;
	&lt;array&gt;
		&lt;string&gt;/usr/local/sbin/lighttpd&lt;/string&gt;
		&lt;string&gt;-f/path/to/lighttpd_ssl.conf&lt;/string&gt;
		&lt;string&gt;-D&lt;/string&gt;
	&lt;/array&gt;
&lt;/dict&gt;
&lt;/plist&gt;</pre>
<p>Of course, if you&#8217;ve installed the lighttpd binary someplace other than <span class="code">/usr/local/sbin</span> then you should change those 2 lines in each .plist file, and you should specify the path to your .conf files properly.</p>
<h3 id="graceful">Graceful restarts</h3>
<p>Wouldn&#8217;t it be super-nice if we could issue graceful restart commands?  It&#8217;s actually very easy in Tiger (10.4) since launchd monitors apps it has started, and re-opens them if they&#8217;ve quit or crashed.  That means that all we have to do is cause a graceful shutdown, and launchd will take care of the restart for us.  Put the following in <span class="code">/usr/local/sbin/</span> in a file called &#8220;lighttpdctl&#8221; (named after apachectl) with the appropriate path to your config files:</p>
<pre class="code">#!/bin/sh
sudo killall -9 lighttpd</pre>
<p>Make it executable by doing this:</p>
<pre class="code">chmod u+x /usr/local/sbin/lighttpdctl</pre>
<p>Now, from anywhere (since <span class="code">/usr/local/sbin</span> is in our path), you can call <span class="code">lighttpdctl</span> to do a graceful restart.  Note, if you&#8217;re running a port 3000-bound <span class="code">script/server</span> development instance of lighty, it will also quit.</p>
<h3>We&#8217;re done</h3>
<p>Well, almost.  You now have to restart your machine to have launchd do its thing.  If you issue the kill command to lighttpd and the configuration is bad (for whatever reason), and lighty bails on startup, launchd will no longer monitor the process.  This means that when you&#8217;re mucking around, there&#8217;s a lot of <span class="code">sudo lighttpd -f/etc/lighttpd/lighttpd.conf -D</span> from the prompt until you&#8217;ve got it working, followed by a restart.  I&#8217;m pretty sure that there&#8217;s a better way to do the config debugging, but I&#8217;m not 1337 enough with FreeBSD and launchd-speak to do it any better.  If somebody out there knows, please post it in the comments.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.foliosus.com/2006/04/04/howto-lighttpd-with-ssl-rails-php-and-mysql-on-osx-104-tiger/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Lighttpd url rewrites are funny</title>
		<link>http://www.foliosus.com/2006/03/20/lighttpd-url-rewrites-are-funny/</link>
		<comments>http://www.foliosus.com/2006/03/20/lighttpd-url-rewrites-are-funny/#comments</comments>
		<pubDate>Mon, 20 Mar 2006 18:56:58 +0000</pubDate>
		<dc:creator>Brent Miller</dc:creator>
		
		<category><![CDATA[Lighttpd]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.foliosus.com/2006/03/20/lighttpd-url-rewrites-are-funny/</guid>
		<description><![CDATA[Since I&#8217;ve been playing around with RoR I&#8217;ve found the
script/server
trick for development to be fantastically useful.  It&#8217;s quick to use, the server itself is fast, and it&#8217;s everything I could want.  That is, until it&#8217;s time to go in to production mode.  I&#8217;ve been running Apache, using OSX&#8217;s built-in build, but I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>Since I&#8217;ve been playing around with RoR I&#8217;ve found the</p>
<pre class="code">script/server</pre>
<p>trick for development to be fantastically useful.  It&#8217;s quick to use, the server itself is fast, and it&#8217;s everything I could want.  That is, until it&#8217;s time to go in to production mode.  I&#8217;ve been running Apache, using OSX&#8217;s built-in build, but I&#8217;m considering switching my whole setup over to lighty because of the funkiness of Apache and Ruby&#8217;s fcgi.  So I tried it.  Building lighty was really easy.  Getting lighty to then serve my static pages was also very easy.  Then I tried getting it to serve my ruby apps, and that&#8217;s where the problems hit.<span id="more-32"></span></p>
<p>It turns out that my one big config problem was this: &#8220;url.rewrite&#8221; does not work inside a $HTTP["url"] matching block.  So, for example consider this case, where I want to rewrite URL&#8217;s intended for my rails app which has 2 controllers, admin and view:</p>
<pre class="code">$HTTP["url"] =~ &#8220;^/input_path/&#8221; {
  url.rewrite-once (&#8221;/(admin|view)/(.*)&#8221; => &#8220;/some/other/path/$1/$2&#8243;)
}</pre>
<p>If the incoming URL matches &#8220;/input_path/&#8221; then lighty starts processing the code inside the braces, but the url.rewrite-once fails to act.  Instead, the server will invariably look for files inside /input_path/ and serve up a 404.</p>
<p>The proper way to do this sort of work with lighty is to have the regex do all the work:</p>
<pre class="code">url.rewrite-once ("^/input_path/(admin|view)/(.*)" => "some/other/path/$1/$2")</pre>
<p>This isn&#8217;t documented anywhere on the <a href="http://www.lighttpd.net/documentation/" title="LightTPD docs">lighty web site</a>, but there is a <a href="http://article.gmane.org/gmane.comp.web.lighttpd/2992" title="The only documentation?">message on the lighty mailing list</a> that points out the problem.</p>
<p>I have to say, other than this hitch, I&#8217;ve been very impressed with the speed and ease of configuration that lighty offers.  Once I get PHP to work with it, I&#8217;ll be a happy camper.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.foliosus.com/2006/03/20/lighttpd-url-rewrites-are-funny/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
