Skip to Content

stopsoftwarepatents.eu petition banner

Lighttpd, Drupal and Gallery2 integration

With thanks to nordisch.org darix and MrWerewolf

I've been wrestling with a planned migration to lighttpd for a few days now, on the whole I've found it much easier than apache to configure something that works, and for the most part have done it much faster than the last time I had to configure Apache from scratch. The big sticking point was Drupal sites with an embedded gallery2.

I think I've cracked it using some rewrites and the magnet approach, I'm not sure if I've got the lua script right, actually, i'm pretty sure it's not matching the case I had to code some extra re-writes for. If anyone can spot where I've gone wrong I'll love them forever (or at least send them coffee).

Update: After feeding myself more coffee, I fixed it, see #typo below.

drupal.lua

-- little helper function

function file_exists(path)

  local attr = lighty.stat(path)

  if (attr) then

      return true

  else

      return false

  end

end

function removePrefix(str, prefix)

  return str:sub(1,#prefix+1) == prefix.."/" and str:sub(#prefix+2)

end

-- prefix without the trailing slash

local prefix = ''

-- the magic ;)

if (not file_exists(lighty.env["physical.path"])) then

   -- file still missing. pass it to the fastcgi backend

   request_uri = removePrefix(lighty.env["uri.path"], prefix)

   -- rules for drupal

   if request_uri then

      local uriquery = lighty.env["uri.query"] or ""

      if string.find(request_uri, "v/") == 1 then

         lighty.env["uri.path"] = prefix .. "/index.php"

            if #request_uri > 2 then

               g2_path = string.sub(request_uri, 3)

            else g2_path = ""

            end

         lighty.env["uri.query"] = uriquery .. (uriquery ~= "" and "&" or "") .. "q=gallery&g2_path=" .. g2_path

      else lighty.env["uri.path"] = prefix .. "/index.php"

      # end # typo

            lighty.env["uri.query"] = uriquery .. (uriquery ~= "" and "&" or "") .. "q=" .. request_uri

      end   # fix for typo

      lighty.env["physical.rel-path"] = lighty.env["uri.path"]

      lighty.env["request.orig-uri"]  = lighty.env["request.uri"]

      lighty.env["physical.path"]     = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]

  end

end

-- fallthrough will put it back into the lighty request loop

-- that means we get the 304 handling for free. ;)

My virtual host config

$HTTP["host"] == "leedsrocksoc.co.uk" {
  server.document-root = "/var/www/lighttpd/rocksoc/current"
  # we only need index.php here.
  index-file.names = ( "index.php" )
  # for clean urls
  magnet.attract-physical-path-to = ( "/etc/lighttpd/drupal.lua" )
  # server.error-handler-404 = "/index.php?q=" #not needed
  url.rewrite = (
   "^/(.*)/Rewrite.txt$" => "/$1/Works.txt"
  , "^/gallery2/v/(\?.+|\ .)?$" => "/gallery2/main.php?g2_view=core.ShowItem"
  , "^/gallery2/admin[/?]*(.*)$" => "/gallery2/main.php?g2_view=core.SiteAdmin&$1"
  , "^/gallery2/d/([0-9]+)-([0-9]+)/([^\/]+)(\?|\ )?(.*)$" => "/gallery2/main.php?g2_view=core.DownloadItem&g2_itemId=$1&g2_serialNumber=$2&$3"
  , "^/gallery2/v/([^?]+)/slideshow.html" => "/gallery2/main.php?g2_view=slideshow.Slideshow&g2_path=$1"
  , "^/gallery2/v/([^?]+)(\?|\ )?(.*)$" => "/gallery2/main.php?g2_view=core.ShowItem&g2_path=$1&$3"
  , "^/gallery2/c/add/([0-9]+).html" => "/gallery2/main.php?g2_view=comment.AddComment&g2_itemId=$1"
  , "^/gallery2/c/view/([0-9]+).html" => "/gallery2/main.php?g2_view=comment.ShowAllComments&g2_itemId=$1"
  , "^/gallery2/p/(.+)" =>  "/gallery2/main.php?g2_controller=permalinks.Redirect&g2_filename=$1"
  # For drupal +  gallery2 embedded # not needed now typo fixed.
  #, "^/v/(\?.+|\ .)?$" => "/index.php?q=gallery&g2_view=core.ShowItem"
  #, "^/v/([^?]+)(\?|\ )?(.*)$" => "/index.php?q=gallery&g2_view=core.ShowItem&g2_path=$1&$3"
  # may need some more but I've not found a broken one yet.
  # where the url is "^/gallery2/char/whatever" => "/gallery2/main.php?somethingOrOther"
  # add a rule for "^/char/whatever" => "/index.php?q=gallery&somethingOrOther"
  )
}

Trackback URL for this post:

http://technomancer.me.uk/trackback/250

Cool

Hey I'm glad my code helped you. I spent a few nights figuring it out myself. I don't think I posted my code with that typo though. I'm very sorry if that's the case.

~MrWerewolf

Excellent

The important and duly answer

Next

Your idea simply excellent

Post new comment