Installing Drupal 6.4 On A Lighttpd Web Server (Debian Etch)

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Thu, 2008-09-11 18:16. :: Debian | Lighttpd

Installing Drupal 6.4 On A Lighttpd Web Server (Debian Etch)

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 09/02/2008

This guide explains how you can install Drupal 6.4 on a lighttpd web server on Debian Etch. Drupal comes with an .htaccess file with mod_rewrite rules (for Apache) that do not work on lighttpd. Without this .htaccess file it is not possible to have clean URLs in your Drupal installation. Fortunately there's a way to make lighttpd behave as if it could read the .htaccess file.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I have tested this on a Debian Etch server where lighttpd and PHP5 are already installed and working (e.g. like in this tutorial). I'll use the hostname www.example.com and lighttpd's default document root /var/www (where I will install Drupal) in this tutorial for demonstration purposes. Of course, you can use any other vhost as well, but you might have to adjust your lighttpd.conf.

 

2 Installing mod_magnet

I will use a file called drupal.lua that contains the rewrite rules needed by Drupal (e.g. for clean URLs). Lighttpd needs the module mod_magnet so that it can understand the drupal.lua file. Therefore we install mod_magnet...

apt-get install lighttpd-mod-magnet

... and enable it:

lighty-enable-mod magnet

Next we download the drupal.lua file:

cd /etc/lighttpd
wget http://nordisch.org/drupal.lua

(If the download link doesn't work for some reason, here's the content of the drupal.lua file:

-- 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 = '/drupal'

-- 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)
    if request_uri then
      lighty.env["uri.path"]          = prefix .. "/index.php"
      local uriquery = lighty.env["uri.query"] or ""
      lighty.env["uri.query"] = uriquery .. (uriquery ~= "" and "&" or "") .. "q=" .. request_uri
      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. ;)

)

Because I want to install Drupal directly in the document root (/var/www) and not in a subdirectory, I open /etc/lighttpd/drupal.lua and change local prefix = '/drupal' to local prefix = '':

vi /etc/lighttpd/drupal.lua

[...]
-- prefix without the trailing slash
local prefix = ''
[...]

Next I open /etc/lighttpd/lighttpd.conf and change the values of index-file.names and url.access-deny and add a line for magnet.attract-physical-path-to:

vi /etc/lighttpd/lighttpd.conf

[...]
## files to check for if .../ is requested
#index-file.names           = ( "index.php", "index.html",
#                               "index.htm", "default.htm" )
index-file.names           = ( "index.php" )

## Use the "Content-Type" extended attribute to obtain mime type if possible
# mimetype.use-xattr = "enable"

#### accesslog module
accesslog.filename         = "/var/log/lighttpd/access.log"

## deny access the file-extensions
#
# ~    is for backupfiles from vi, emacs, joe, ...
# .inc is often used for code includes which should in general not be part
#      of the document-root
#url.access-deny            = ( "~", ".inc" )
url.access-deny = ( "~", ".inc", ".engine", ".install", ".module", ".sh", "sql", ".theme", ".tpl.php", ".xtmpl", "Entries", "Repository", "Root" )

magnet.attract-physical-path-to = ( "/etc/lighttpd/drupal.lua" )
[...]

Finally I restart lighttpd:

/etc/init.d/lighttpd restart

Lighttpd is now ready for Drupal 6.4.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Daniel (not registered) on Tue, 2010-05-25 13:34.

nice how-to. Thank you very much.

 

One remark : if you have a cgi script on the same server that relies on using parts of the URI as args, this mod-magnet script will break it.

 For instance, dadamail uses something like:

 http://myserver.com/cgi-bin/mail.cgi

 (which works fine) but also expects to get things like

http://myserver.com/cgi-bin/mail.cgi/some/args

(which gives a 404 from lighty).

 

The workaround I used is to do something like :

 

$HTTP["url"] =~ "^cgi-bin" {
   cgi.assign = ( ".cgi" => "/usr/bin/perl" )
} else $HTTP["url"] !~ "^cgi-bin" {
   magnet.attract-physical-path-to = ( "/etc/lighttpd/drupal.lua" )
}

 (and no, I still don't get why lighty doesn't support a simple if/else ... but whatever ... )