Lighttpd and a convenient way to support clean URLs

Submitted by fago on Wed, 02/11/2009 - 17:42
A lot of people run drupal with lighttpd, a light and fast webserver, which uses FastCGI to support PHP. As lighttpd doesn't read .htaccess files, some extra config is required to get drupal's clean urls working right. I'm doing so already for a while now, finally I found the best way to do clean urls with lighttpd. I started with some url-rewriting regexes, which isn't ideal as there are problems with some special paths or dots. I've improved the regexes a bit, but still there were problems. Another common way is to use a lua-script. The big advantage of this script is that one can do the url rewriting based on the condition whether there is a file for the requested path - so no regex magic is required. Thus there also no problems with dots. The disadvantage of that approach is it's inflexibility. When you want to install multiple drupal instances in several different sub directories or use a script to generate settings for all your drupal instances, it's odd that the lua variant needs one script per drupal prefix, because you have to set the prefix in the lua script! Finally I found a way to work around this inflexibility. First off I looked into passing the prefix variable into the lua script, but I had no luck with that. So I decided to go for another approach: I wrote a simple wrapper script, that creates the needed lua script with the right prefix set on the fly. So when lighttpd parses your configuration it calls the wrapper script, which makes sure there is a lua script in place and returns the suiting configuration for your drupal installation. Ok, enough talk. Let's see how it works. First off make sure you have lighty's mod magnet module installed and enabled. In Ubuntu this can be done easily by running
apt-get install lighttpd-mod-magnet
lighty-enable-mod magnet
Next download my wrapper script and the lua script and extract it into your lighttpd configuration. The scripts assume it's in /etc/lighttpd - if not you have to adapt them.
cd /etc/lighttpd
wget http://more.zites.net/morefiles/lighty-drupal-lua-conf.tar.gz
tar xvzf lighty-drupal-lua-conf.tar.gz
Make sure drupal-lua-conf.sh is now in place and it's executable. Then just use it by adding
$HTTP["host"] =~ "your.domain.example.com" {
  include_shell "./drupal-lua-conf.sh"
}
if your drupal doesn't live an a subdirectory. Else just give the subdirectory as argument to the script, e.g. I use
include_shell "./drupal-lua-conf.sh drupal-5"
include_shell "./drupal-lua-conf.sh drupal-6"
include_shell "./drupal-lua-conf.sh drupal-7"
Note that the script uses "drupal-lua.conf" as template for your drupal configuration - so you can just change it, reload lighty's config and all your drupal instances are running the changed config. For a drupal install, which doesn't live in a subdirectory, the default template is:
  url.access-deny += ( "~", ".inc", ".engine", ".install", ".info", ".module", ".sh", "sql", ".theme", ".tpl.php", ".xtmpl", "Entries", "Repository", "Root" )

  # "Fix" for Drupal SA-2006-006, requires lighttpd 1.4.13 or above
  # Only serve .php files of the drupal base directory
  $HTTP["url"] =~ "^/.*/.*\.php$" {
      fastcgi.server = ()
      url.access-deny = ("")
  }

  magnet.attract-physical-path-to = ("/etc/lighttpd/drupal-lua-scripts/p-.lua")
Note that this configuration only executes .php files inside your drupal base directory - so users can't upload and execute .php files ;) If your webserver executes some other files, e.g. perl - you should add a check for them too. Then if you enable private file downloads you may want to protect your files with something like that:
$HTTP["url"] =~ "^/files" {
  #Be sure to have this below the inclusion of the lua script.
   url.access-deny = ("")
}
But be sure to move this code snippet below the inclusion of the lua script, or it won't work. That's it.. :) Thanks to the simple wrapper script it's just a question of adding one line and your drupal installation is configured properly.

meinemitternacht (not verified)

Sun, 06/28/2009 - 22:34

I have successfully implemented this CleanURL feature, thanks for the effort! But, I was wondering how you handle exceptions. For example, I have a drupal site but want to install phpbb3 under /forums. With your cleanURL package, it will always redirect to the home page. I want to exclude /forums from the rewrite. Is this possible?

gege2061 (not verified)

Tue, 01/05/2010 - 14:50

Thank's for your works! I have just a small problem with phpbbforum module (phpbb integration), the fix : # Only serve .php files of the drupal base directory $HTTP["url"] =~ "^/.*/.*\.php$" { fastcgi.server = () url.access-deny = ("") } should be removed because the phpbb URL always show 403 error.

kkruecke (not verified)

Fri, 01/22/2010 - 01:02

Hi, Thanks for the article. Just so I understand, do I also need to enable mod_rewrite (by uncommenting it from lighttpd.conf). I know I need to install and enable mod_magnet, but what about mod_rewrite? thanks, Kurt

Symon777 (not verified)

Fri, 10/01/2010 - 11:19

Thank you for taking the time to write about it! I know that such posts are really hard to find these days. Besides, you can read a new article on this topic at shared article on lighttpd I am sure most of the readers will find it useful. All the best.

karthick (not verified)

Tue, 12/28/2010 - 10:50

Hi, I have started using drupal on lighttpd for the few days.. My directory set up goes like this . 1./document-root/site 2./document-root/site/subsite1 3./document-root/site/subsite2 I am able to get the clean urls for the above sites using lua scripts. I have no problem on accessing the pages using both apache and lighttpd (using lua scripts) But I got another site,like 4./document-root/site/subsite3/subsite3.1 When I click the pages on the subsite3 and subsite3.1 I am getting page not found errors when I use lighttpd. but it works fine with apache2. I want to know that if we can access a subdiirectory inside a subdirectory using lighttpd? Thanks in advance...

Ultimate Colossus (not verified)

Mon, 03/07/2011 - 18:27

[b]Educational Toys for Kids Intend Playing while Learning[/b] Since ages, toys have been considered as one of the important play items that kids love to play with. If you want to make your kids learn while playing, then consider educational toys. Educational toys for kids not only promote developmental skills in children, but also help children to learn and improve essential skills of life. Most of the people buy toys to enhance creativity, independency, responsibility and integrity of their kids. To buy toys, parents are recommended to look for online stores as there are an ocean of toys available that can be very hard to determine which is right for your child. Consider toys according to your child as each one has his own personality, interests and strengths. Considering educational toys for kids Parents consider educational toys for their kids as they not only give them pleasure of playing, but also enable them to learn at the same time. They are perfect than allowing them to play video games and other non-educational activities. One can incorporate toys that will aid in developing the cognitive skills of child while providing fun. One of the popular educational toys for kids is alphabet bean bag, which assist your kids memorize the alphabet. Parents should strictly avoid toys, which are made by employing harmful chemicals. Toys for leisure time Gifting an educational toy to your kids plays a very important role to nurture their overall development and skills. Such a toy will help them in learning with fun. In fact, it is considered as a wonderful gift to fill up their curious minds. Parents always need to provide toys that will brush up their knowledge as well as make them matured and responsible according to their age. Buying educational toys for kids from online stores The online market is packed with a number of online stores selling all types of toys at affordable rates. Apart from products, their services are also unique and exclusive in the market. Individual looking for educational toys for kids are advised to make extensive search through the internet, and thus they will come across authentic online stores. So, do not get late. Just make search and see results.