Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Make sure Lighttpd is your server

(This from http://manuals.textdrive.com/read/chapter/62#page145)

This snippet is not mine-- it's taken directly from Robert Simplicio's excellent guide "Lighttpd: The painless way". However, it was buried deep in the last section, and I wanted to ensure it wasn't missed.

Once you've gone to all the trouble of configuring Lighttpd, make sure it's actually serving out your pages:

curl -I http://domain.name


Your should see something like this:

-bash-2.05b$ curl -I http://simplicio.com
HTTP/1.1 200 OK
Date: Sun, 15 May 2005 00:27:40 GMT
Server: lighttpd | TextDriven
Last-Modified: Tue, 10 May 2005 03:56:11 GMT
Accept-Ranges: bytes
Content-Type: text/html; charset=UTF-8
X-Powered-By: The blood, sweat and tears of the fine, fine TextDrive staff
Served-By: TextDrive

Lighttpd conditional for subdomain

$HTTP["host"] =~ "(typo|blog)\.simplicio\.(com|net|org)" {
server.document-root = "/home/rsimplicio/web/public/typo/"

url.rewrite = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" )
  server.error-handler-404 = "/dispatch.fcgi" 
server.indexfiles = ( "dispatch.fcgi")

 fastcgi.server = ( ".fcgi" =>
   ( "Simplicio-typo" =>
        (
          "socket"   => "/home/rsimplicio/web/lighttpd-typo-fcgi.socket",
          "bin-path" => "/home/rsimplicio/web/public/typo/dispatch.fcgi",
          "bin-environment" => ( "RAILS_ENV" => "production" ),
          "max-load-per-proc" => 25,
	  "min-procs" => 1,
          "max-procs" => 1,
	  "idle-timeout" => 60
	)
    )
  ) 
}

Lighttpd launchd item for OS X 10.4

Save the following in /Library/LaunchDaemons/net.lighttpd.plist to have lighttpd start automatically on any OS X 10.4 computer. Note that this assumes a working DarwinPorts lighttpd installation (otherwise you will need to change the paths to the lighttpd executable). Note that this also assumes that the lighttpd.conf file is in /opt/local/etc.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>net.lighttpd</string>
	<key>OnDemand</key>
	<false/>
	<key>Program</key>
	<string>/opt/local/sbin/lighttpd</string>
	<key>ProgramArguments</key>
	<array>
		<string>/opt/local/sbin/lighttpd</string>
		<string>-f/opt/local/etc/lighttpd.conf</string>
		<string>-D</string>
	</array>
</dict>
</plist>

killing your own dispatch.fcgis

ps aux|grep youruser|grep dispatch|awk '{print $2}'|xargs kill -9

of course you can use this for any process and just change the 'dispatch' part

Example lighttpd config for a Rails app

I thought I'd share my lighttpd config file. My main application driving http://www.standardbehaviour.com is at ~/web/, which means the dispatch.fcgi file is in ~/web/public/dispatch.fcgi.

# Modules to load {{{1
server.modules              = (
                                "mod_rewrite",
                                "mod_redirect",
                                "mod_access",
                                "mod_fastcgi",
                                "mod_compress",
                                "mod_accesslog" )
# }}}1

# files to check for if .../ is requested

# Mimetype mapping {{{1
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"          =>      "audio/x-wav",
  ".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",
  ".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"
 )
# }}}

server.document-root       = "/home/mveldthuis/web/public/"
server.bind                = "standardbehaviour.com"
server.port                = 8534
server.pid-file            = "/home/mveldthuis/var/run/lighttpd.pid"
server.tag                 = "lighttpd"
server.username            = "mveldthuis"
server.groupname           = "mveldthuis"
accesslog.filename         = "/home/mveldthuis/var/log/lighttpd.access.log"
server.errorlog            = "/home/mveldthuis/var/log/lighttpd.error.log"
server.indexfiles          = ( "index.php", "index.html",
                               "index.htm", "default.htm" )
url.access-deny            = ( "~", ".inc" )

# Main site, I want this on www or no www, and on .com, .net and .org
$HTTP["host"] =~ "(www\.)?standardbehaviour\.(com|net|org)" {
  server.document-root     = "/home/mveldthuis/web/public/"
  server.error-handler-404 = "/dispatch.fcgi"
  fastcgi.server= (".fcgi" =>
    ("StandardBehaviour.com" =>
      (
        "socket" => "/home/mveldthuis/var/lighttpd-fcgi.socket",
        "bin-path" => "/home/mveldthuis/web/public/dispatch.fcgi",
        "bin-environment" => ("RAILS_ENV" => "production" ),
        "max-load-per-proc" => 4,
        "min-procs" => 1,
        "max-procs" => 2,
        "idle-timeout" => 60
      )
    )
  )
}

# This is the images subdomain, serves mainly just the files,
# but I have fileNice there which is PHP.
$HTTP["host"] =~ "images\.standardbehaviour\.(com|net|org)" {
  server.document-root     = "/home/mveldthuis/subdomains/images/"
  server.dir-listing       = "enable"
  server.indexfiles        = ("index.php")
  server.error-handler-404 = "/index.php"
  fastcgi.server = (".php" =>
    ("ImagesStandardBehaviour" =>
      ("socket" => "/home/mveldthuis/var/lighttpd-images-php.socket",
       "bin-path" => "/usr/local/www/cgi-bin/php5-fcgi",
       "bin-environment" =>
         ("PHP_FCGI_CHILDREN" => "1",
          "PHP_FCGI_MAX_REQUESTS" => "2500"
         )
      )
    )
  )
}

# Pretty much the same as images, but then for experiments.
$HTTP["host"] =~ "experiments\.standardbehaviour\.(com|net|org)" {
  server.document-root          = "/home/mveldthuis/subdomains/experiments/"
  server.dir-listing = "enable"
  server.indexfiles  = ("index.php", "index.html")
  fastcgi.server = (".php" =>
    ("ImagesStandardBehaviour" =>
      ("socket" => "/home/mveldthuis/var/lighttpd-images-php.socket",
       "bin-path" => "/usr/local/www/cgi-bin/php5-fcgi",
       "bin-environment" =>
         ("PHP_FCGI_CHILDREN" => "1",
          "PHP_FCGI_MAX_REQUESTS" => "2500"
         )
      )
    )
  )
}


Hope this helps anyone.

lighttpd.conf for home developing

[code]
server.port = 80
server.document-root = "/Library/WebServer/Documents/"
server.event-handler = "freebsd-kqueue"
server.modules = ( "mod_rewrite", "mod_fastcgi", "mod_compress" )


# Edit paths and everything in CAPS to suit your need.

$HTTP["host"] == "HOST_NAME" {

server.document-root = "/Users/YOUR_USER_NAME/Rails/YOUR_APP_NAME/public"
server.errorlog = "/Users/YOUR_USER_NAME/Rails/YOUR_APP_NAME/log/server.log"

server.error-handler-404 = "/dispatch.fcgi"
fastcgi.server = ( ".fcgi" =>
( "localhost" =>
(
"min-procs" => 1,
"max-procs" => 5,
"socket" => "/tmp/railsapp.YOUR_APP_NAME.fcgi.socket",
"bin-path" => "/Users/YOUR_USER_NAME/Rails/YOUR_APP_NAME/public/dispatch.fcgi",
"bin-environment" => ( "RAILS_ENV" => "development" )
)
)
)
}

# mimetype mapping
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" => "audio/x-wav",
".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",
".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"
)
[/code]

Chained SSL certs in lighttpd

ssl.ca-file = "/usr/local/openssl/certs/chain.ca.crt"
ssl.pemfile = "/usr/local/openssl/certs/server.pem"

Redirect domain.com requests to www.domain.com

Make sure you have mod_redirect in server.modules, then add this to your lighttpd.conf:

$HTTP["host"] =~ "^domain\.com$" {
  url.redirect = (
    "^/(.*)" => "http://www.domain.com/$1",
    ""       => "http://www.domain.com/"
  )
}


I wasn't able to come up with a way to do it in a single rule. It seems that only "" (not even "(.*)") will match the root request.

How to flush the local DNS cache on Mac OS X

If you want to add a virtualhost on your Mac OS X box without having to wait around for ages, then the easiest way to do so is to shove a line into /etc/hosts and flush the dnscache. Here's a friendly bash function to throw into your .bashrc:

function edithosts {
	if [ -x "`which $EDITOR`" ] || [ -x "`which $1`" ]
	then
		if [ -x "`which $EDITOR`" ]
		then
			export TEMP_EDIT="`which $EDITOR`"
		else
			export TEMP_EDIT="`which $1`"
		fi
		echo "* Using ${TEMP_EDIT} as editor"
		$TEMP_EDIT /etc/hosts && echo "* Successfully edited /etc/hosts"
		lookupd -flushcache && echo "* Flushed local DNS cache"
	else
		echo "Usage: edithosts [editor]"
		echo "(The editor is optional, and defaults to \$EDITOR)"
	fi
	unset TEMP_EDIT
}


More simply, you can just flush the DNS cache manually with:

lookupd -flushcache

Textpattern clean urls with Lighttpd, the elegant way

I love this shit:

server.error-handler-404 = "/index.php"