Personal tools
You are here: Home GNU / Linux Debian 4.0 Etch Install Lighttpd and PHP 5 on Debian 4.0 Etch and 5.0 Lenny
Document Actions
  • Send this page to somebody
  • Print this page
  • Add Bookmarklet

Install Lighttpd and PHP 5 on Debian 4.0 Etch and 5.0 Lenny

by Pierre-Yves Landuré last modified 2010-11-16 10:45

Lighttpd is a very light HTTP server. It is also very easy to configure it. It is a very interesting alternative to Apache when you want to reduce your memory usage. This article explain how to install Lighttpd with PHP 5 enabled.


First, we install the need packages :

apt-get install lighttpd libterm-readline-gnu-perl php5-cgi

By default, on Debian 4.0 Etch, Lighttpd is installed with a configuration using php4-cgi. In the case where this is the distribution we use, we apply the needed patch :

if [ ! -z "$(/bin/grep '/usr/bin/php4-cgi' /etc/lighttpd/conf-available/10-fastcgi.conf)" ]; then
/bin/cp /etc/lighttpd/conf-available/10-fastcgi.conf /etc/lighttpd/conf-available/10-fastcgi-php5.conf
/bin/sed -i -e 's/php4/php/g' /etc/lighttpd/conf-available/10-fastcgi-php5.conf

We enable the correct configuration depending if we use Debian 4.0 Etch or Debian 5.0 Lenny :

if [ -e /etc/lighttpd/conf-available/10-fastcgi-php5.conf ]; then
/usr/sbin/lighty-enable-mod fastcgi-php5
/usr/sbin/lighty-enable-mod fastcgi

And we reload the LigHTTPd configuration :

/etc/init.d/lighttpd force-reload

Going deeper

Enhancing PHP performances

If you want, you can install a PHP accelerator by following my guide Install XCache on Debian 4.0 Etch.

Creating a Virtual Host with Lighttpd

Like Apache, Lighttpd can manage several web sites on one server. Here is a way to quickly create a virtual host serving a folder.

First,  please specify the hostname for your virtual host (replace the bold value):

Now, specify the folder that you want to assign to this hostname (replace the bold value):


Once this done, download the virtual host configuration template:

SITE_CONF=$(/bin/echo ${SITE_HOSTNAME} | /bin/sed -e 's/\./-/g')
/usr/bin/wget \

Apply your settings to this template:

/bin/sed -i -e "s/SITE_HOSTNAME/${SITE_HOSTNAME}/g" \
-e "s|SITE_PATH|${SITE_PATH}|g" \

Enable the configuration you have just created:

/usr/sbin/lighty-enable-mod ${SITE_CONF}

Test the new configuration:

/usr/sbin/lighttpd -t -f /etc/lighttpd/lighttpd.conf

If you see no error, reload the Lighttpd configuration:

/etc/init.d/lighttpd force-reload

You should now be able to visit your website.

Note : Using virtual host with Lighttpd is usefull, even if you use Lighttpd behind a Apache reverse proxy. If you are doubtfull, take a look at the ProxyPreserveHost option of the Apache mod_proxy.

URL Rewriting with Lighttpd

Unlike Apache 2, Lighttpd is no able to make complex URL rewrites. However, there is a way to do simple URL rewriting with some web applications like Wordpress or Symfony projects. You only need to configure the index.php file of these web applications to handle the 404 errors.

In order to do this, simply add the following line to the configuration file of the virtual host serving your web application, and change the path to the index.php file to fit the application location:

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

See also


Group and User lighttpd

Posted by Orlando Agostinho at 2008-09-23 15:33

Very good tutorial! Congratulations!

Can you explain little bit about group and user for Lighttpd?


a domain name with "www" prefix

Posted by thinkweird at 2009-05-18 03:22
This guide works flawlessly.

I have one question about virtual domains:

you only mentioned virtual sites with 'www' prefix, but how to configure the same site without 'www' prefix for the same site?

Now I am making two configuration files with 'www' and without 'www' for the same site, but it does appear redundant, doesn't it?

virtualhost with lighty

Posted by Lone Wolf at 2009-05-18 06:08
Lighty support regular expressions to match domain names. To enable this feature, use =~ instead of == to match the domain. For example :

$HTTP ["host"] =~ "(www.)?" {

Hope this help

need mod_simple_vhost?

Posted by ol at 2010-05-26 05:50
I keep referring back to this page for setting up lighty on Debian and it works very well.

There is one question: in the configure file for virtual hosting, do I need this line? 

server.modules += ( "mod_simple_vhost" )

It seems the virtual host works ok even without this line. What is it for? Enabling simple_vhost module?

Also, to avoid the www prefix problem, lighty wiki gives this solution:

$HTTP["host"] =~ "(^|\.)example\.com$" {

and it worked as well as you suggested in the reply above.


Posted by Lone Wolf at 2010-05-26 09:30
Yes the server.modules line is used to enable the simple_vhost module, in case it is not already enabled. I think it is better to have virtual hosts embeding their dependencies.


Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: