lighttpd + WordPressMU (english)

For those who still don’t know it, lighttpd is a cool web server: small, efficient, extensible, powerful… but just as with any other new server, you’ve got to get used to it and doing things “its way” instead of “as always”.

One thing that’s somewhat different in Apache and lighttpd is “mod_rewrite”: lighttpd has its own URL-rewriting module, with a syntax slightly different to that of Apache’s mod_rewrite, but lighty’s rewrite doesn’t support checking if a file exists and doing things accordingly (-f and -d in Apache). WPMU depends on this. Fortunately, lighttpd has a tool more powerful than mod_rewrite: mod_magnet.

mod_magnet is a module which allows us to do WHATEVER WE WANT with a query before it’s served by the rest of modules (PHP…), by using the LUA programming language. Initially called mod_cml, this module was used just as an advanced caching device, but in recent releases (>=1.4.12) it’s evolved into a way to hack at the http server’s very core.

Enough talk, let’s get our hands dirty. ;-) Apache’s .htaccess for this blog would look like:
[code lang=”apache”]
RewriteEngine On
RewriteBase /

#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteRule ^(.*/)?files/(.*)
wp-content/blogs.php?file=$2 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . – [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*.php)$ $2 [L]
RewriteRule . index.php [L]

There are two issues here:

  • The lines:
    [code lang=”apache”]RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d[/code]
  • LUA doesn’t seems to support catches with the ? repetition like in (…..)?, so we’ve got to come up with another regex to get rid of the first “folder” on the URLs.

And the winner regexp :-P for this second problem is:
For the first problem, you can take a look at the LUA script I’m using on this blog here: wpmu.lua. In order to use it, you’ve to add something like this to the lighttpd.conf file:

[code]$HTTP[“host”] =~ “” {
magnet.attract-physical-path-to = ( “/PATH/wpmu.lua” )

I’m sure there’re better ways to do this. I don’t quite like that regexp nor the LUA code, but hey, it works. :)

[code lang=”lua”]
if (not lighty.stat(lighty.env[“physical.path”])) then
if (string.match(lighty.env[“uri.path”], “^(/?[^/]*/)files/$”)) then
lighty.env[“physical.rel-path”] = “index.php”
n, a = string.match(lighty.env[“uri.path”], “^(/?[^/]*/)files/(.+)”)
if a then
lighty.env[“physical.rel-path”] = “wp-content/blogs.php”
lighty.env[“uri.query”] = “file=” .. a
n, a = string.match(lighty.env[“uri.path”], “^(/?[^/]*/)(wp-.*)”)
if a then
lighty.env[“physical.rel-path”] = a;
n, a = string.match(lighty.env[“uri.path”], “^(/?[^/]*/)(.*.php)$”)
if a then
lighty.env[“physical.rel-path”] = a
lighty.env[“physical.rel-path”] = “index.php”
lighty.env[“physical.path”] = lighty.env[“physical.doc-root”] .. lighty.env[“physical.rel-path”]

UPDATED 20070411:

  • Fixed a bug when accessing image files (/file/YEAR/MONTH/…)
  • Reworked the regexps

Please test and report success. I’m new to WPMU so I’m stumbling upon errors as I go. :)

12 comentarios sobre “lighttpd + WordPressMU (english)”

  1. The problem i’m having is actually getting mod_magnet to load! I’ve installed lua and recompiled lighttpd to use it. But when i add mod_magnet to the list, it can’t load it. Does mod_cml work with this also? If you have some experience setting this up, you should email me :). I’d really appreciate it.

  2. I tried at first with mod_cml but it wouldn’t solve the problem, can’t remember exactly why. Then I upgraded lighttpd and switched to mod_magnet.

    Are you getting some error on the logs or anything? Are all permissions right? If you run “ldd”, are all the required libraries correctly linked?

    If you run Debian, you could try with my packages here:
    They may work with alien on other distros (haven’t tried).

  3. I’ve tried to use lua like you.

    But I finally giving up that approach, it make my setup more complex and hard to read.

    I am using the method of “server.error-handler-404” and it make thing real simple and fast. (please read bisente’s comment)

    As a minimalist, I try my best to not load another module, script or language as I can.

  4. When I attempt to host WPMU from a subdirectory (/blogs/, for example), you lua script does not entirely work. It does not load CSS correctly for sub-blogs (/blogs/ may work, but /blogs/dev/ does not). Any suggestions?

Deja un comentario

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.