Never been to TextSnippets 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!)

« Newer Snippets
Older Snippets »
Showing 21-39 of 39 total

Restart lighttpd script

I use this script to avoid having to do the killall dance on my local dev box constantly when tweaking my lighttpd.conf

#!/bin/sh
sudo killall -9 lighttpd
sudo killall -9 ruby
sudo /opt/local/sbin/lighttpd -f /opt/local/etc/lighttpd.conf


Make the script executable (chmod u+x), put it in your path, invoke by simply typing the name of the script. This also kills off any ruby processes, which lighttpd will restart when it reboots.

The path to lighttpd and the lighttpd.conf files are based on the installation from DarwinPorts - adjust as needed.

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>Labelkey>
        net.lighttpd</string>
        <key>OnDemandkey>
        />
        <key>Programkey>
        /opt/local/sbin/lighttpdstring>
        ProgramArguments</key>
        <array>
                <string>/opt/local/sbin/lighttpdstring>
                -f/opt/local/etc/lighttpd.confstring>
                -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"

Wordpress clean URLS with Lighttpd, the E-Z way

This single line seems to work for my WP install. I needed to add a couple of redirects and rewrites to handle my RSS feed, since I use Feedburner, but this line does all the heavy lifting...

Place it after server.document-root, either in the main section or in your virtual host sub-sections...

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


lighttpd with multiple domains

server.pid-file            = "/home/roeland/var/run/lighttpd.pid"
server.document-root        = "/home/roeland/sites/roeland/public/"

$HTTP["host"] =~ ".*roelandmoors\.be" {
  server.document-root        = "/home/roeland/sites/roeland/public/"
  server.errorlog             = "/home/roeland/sites/roeland/log/lighttpd-error.log"
  accesslog.filename          = "/home/roeland/sites/roeland/log/lighttpd-access.log"
  server.indexfiles           = ( "index.php", "index.html",)
  url.access-deny             = ( "~", ".inc" )
  server.error-handler-404 = "/dispatch.fcgi"

  fastcgi.server = ( ".fcgi" =>
      ("rails" =>
          ( "socket" => "/home/roeland/var/lighttpd-rm-fcgi.socket",
            "bin-path" => "/home/roeland/sites/roeland/public/dispatch.fcgi",
            "bin-environment" => ("RAILS_ENV" => "production"),
            "min-procs" => 1,
            "max-procs" => 2
          )
      )
  )
}

$HTTP["host"] =~ ".*klossers\.com" {
  server.document-root        = "/home/roeland/sites/klossers.com/public/"
  server.errorlog             = "/home/roeland/sites/klossers.com/log/lighttpd-error.log"
  accesslog.filename          = "/home/roeland/sites/klossers.com/log/lighttpd-access.log"
  server.indexfiles           = ( "index.php", "index.html",)
  url.access-deny             = ( "~", ".inc" )
  server.error-handler-404 = "/dispatch.fcgi"

  fastcgi.server = ( ".fcgi" =>
      ("rails" =>
          ( "socket" => "/home/roeland/var/lighttpd-klossers-fcgi.socket",
            "bin-path" => "/home/roeland/sites/klossers.com/public/dispatch.fcgi",
            "bin-environment" => ("RAILS_ENV" => "production"),
            "min-procs" => 1,
            "max-procs" => 2
          )
      )
  )
}

lighttpd as a startup item on os x

Create the /Library/StartupItems/Lighttpd directory, put both the Lighttpd (chmod +x) and the StartupParameters.plist (I don't really know what this does at all) files into the directory. (this is on jaguar, before the new launchd came out in tiger).

Then add the following line to /etc/hostconfig:

LIGHTTPD=-YES-


You can start, stop and restart lighttpd using /Library/StartupItems/Lighttpd (start|stop|restart) now, and it will startup when OS X boots.

-------- Lighttpd
#!/bin/sh
#
# /Library/StartupItems/Lighttpd/Lighttpd
#
# Script to startup lighttpd with OS X booting.
# OS X 10.3 Jaguar
# modified by kjell olsen
#
# adapted from the startup mysql
# (c) 2003 MySQL AB
# Written by Lenz Grimmer 
#

# Suppress the annoying "$1: unbound variable" error when no option
# was given
if [ -z $1 ] ; then
        echo "Usage: $0 [start|stop|restart] "
        exit 1
fi

# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
. /etc/rc.common

# change config and script to match your machine's lighttpd config and lighttpd
CONFIG="/etc/lighttpd/lighttpd.conf"
SCRIPT="/usr/local/sbin/lighttpd"

StartService () 
{
  if [ "${LIGHTTPD:=-NO-}" = "-YES-" ] ; then
    ConsoleMessage "Starting Lighttpd Server"
    $SCRIPT -f $CONFIG
  fi
}

StopService ()
{
  ConsoleMessage "Killing Lighttpd Server"
  kill `cat /var/run/lighttpd.pid` 
}

RestartService ()
{
        ConsoleMessage "Restarting Lighttpd server"
        /Library/StartupItems/Lighttpd/Lighttpd stop
        /Library/StartupItems/Lighttpd/Lighttpd start
}

if test -x $SCRIPT ; then
        RunService "$1"
else
        ConsoleMessage "Could not find lighttpd startup script!"
fi

Be sure to change $SCRIPT + $CONFIG to work with your own lighttpd.

-------- StartupParameters.plist
<?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>Descriptionkey>
        Lighttpd 1.13</string>
        <key>OrderPreferencekey>
        None</string>
        <key>Provideskey>
        
                Lighttpd</string>
        array>
        Uses</key>
        <array>
                <string>Networkstring>
                Resolver</string>
        array>
        </dict>
plist>


Again, I'm not exactly sure what this does. I copied it from the MYSQL one and changed the description and provider, left "uses" alone.

Using HTTP conditions and url.access-deny to have Lighttpd block some user agents and referers

# deny access for Indy Library a Tester
$HTTP["useragent"] =~ "Indy" { url.access-deny = ( "" ) }
 
# deny access for a hydrocodone containing refer 
$HTTP["referer"] =~ "hydrocodone" { url.access-deny = ( "" ) }

Lighttpd proxy with Tracd specific example

$HTTP["host"] == "trac.textdrive.com" #the == is to exactly match
{
proxy.server = (
"" => (                               #this is for the root, can be a .extension in other uses
"trac" => (                           #just a name, your choice
"host" => "70.84.29.150", 
"port" => 9000 
           )
       )
                )
}

Typo under lighttpd

Example vhost of a typo install under lighttpd

$HTTP["host"] =~ "typo.jasonhoffman.org" {
server.document-root = "/home/jah/apps/typo/trunk/public/"
server.error-handler-404   = "/dispatch.fcgi"
server.indexfiles           = ( "dispatch.fcgi")
server.errorlog = "/home/jah/logs/error.typo.log"
  fastcgi.server = ( ".fcgi" =>
     ( "localhost" =>
       ( "socket" => "/home/jah/tmp/typo-jah.socket",
                            "min-procs" => 2,
                            "max-procs" => 2,
         "bin-path" => "/home/jah/apps/typo/trunk/public/dispatch.fcgi",
         "bin-environment" => ( "RAILS_ENV" => "production" )
  )))
}

Automatic sub-domains with lighttpd mapping to a specific folder

Using lighttpd's mod_evhost with

# %% => % sign
# %0 => domain name + tld
# %1 => tld
# %2 => domain name without tld
# %3 => subdomain 1 name
# %4 => subdomain 2 name
#


$HTTP["host"] =~ "jasonhoffman.org" {
evhost.path-pattern        = "/home/jah/public/%3/"
}

My lighttpd php-fastcgi config with it's own php.ini

fastcgi.server = (
                ".php" =>
                    ( "localhost" =>
                        (
                            "socket" => "/home/jah/tmp/jah-php5-fcgi.socket",
                            "bin-path" => "/usr/local/www/cgi-bin/php5-fcgi -c /home/jah/etc/php.ini",
                            "bin-environment" => (
                            "PHP_FCGI_CHILDREN" => "4",
                            "PHP_FCGI_MAX_REQUESTS" => "5000"
                                                 )
                        )
                    )
                 )
« Newer Snippets
Older Snippets »
Showing 21-39 of 39 total