There is a new version of this tutorial available for Debian 6 (Squeeze).

Creating Advanced MySQL-Based Virtual Hosts On Lighttpd (Debian Etch) - Page 2

This tutorial exists for these OS versions

On this page

  1. 5 Configuring Virtual Hosts
  2. 6 Testing
  3. 7 Links

5 Configuring Virtual Hosts

I will now configure two virtual hosts, one for (with the document root /var/www/ and one for (with the document root /var/www/

I will add custom directives to both vhosts. For the vhost I will enable directory listings and create an alias test which points back to the document root /var/www/, and for the vhost I will disable directory listings.

First, we create the document roots of both web sites (if they don't already exist):

mkdir -p /var/www/
mkdir -p /var/www/

Then we log in to MySQL...

mysql -u root -p
USE lighttpd;

... and create the vhosts as follows:

INSERT INTO domains VALUES ('','/var/www/','dir-listing.activate = "enable"\nalias.url = ( "/test" => "/var/www/" )');
INSERT INTO domains VALUES ('','/var/www/','dir-listing.activate = "disable"');

As you can see in the first INSERT statement, if you want to use more than one custom directive, put a linebreak (\n) between the directives.

We can now leave the MySQL shell:


That's it, the vhosts are now configured. To check if our /usr/share/lighttpd/ script is working as expected, we can call it on the command line...

/usr/share/lighttpd/ lighttpd lighttpd secret

... and it should display the correct vhost configurations:

server1:~# /usr/share/lighttpd/ lighttpd lighttpd secret
$HTTP["host"] == "" {
server.document-root = "/var/www/"
dir-listing.activate = "enable"
alias.url = ( "/test" => "/var/www/" )
$HTTP["host"] == "" {
server.document-root = "/var/www/"
dir-listing.activate = "disable"

Unlike the mod_mysql_vhost way, this method needs a lighttpd restart whenever the vhost configuration is changed, so let's restart lighttpd:

/etc/init.d/lighttpd restart


6 Testing

Now let's test if our MySQL-based vhosts and are working as expected. I'm assuming that you have no index files in each document root.

Let's call (for which we have enabled directory listings) in a browser, and you should see a directory listing:

Now let's go to (there's no directory test in that vhost), and it should go back to the document root because we have configured that alias for the vhost:

Now go to, and you should get a 404 - Not Found error (unless there's an index file in the document root) because we've disabled directory listings for that vhost:

Now let's try - as this directory does not exist and we haven't defined such an alias in the vhost, we get a 404 - Not Found again:

So everything's working as expected.


Share this page:

1 Comment(s)