Setting up virtual hosting with lighttpd

Virtual hosting with lighttpd can help you to easily create new subdomains and domains by simply adding a directory to your base virtual hosting directory. This allows you to create directories instead of editing configuration files.

This lighttpd configuration will create a virtual hosting directory under /var/www/vhost which will allow you to create example.com/index.html by creating /var/www/vhost/example.com/www/index.html. Something like asdf.example.com/index.html will map to /var/www/vhost/example.com/asdf/index.html. Anything subsubdomains are simply full directory names with dots: a.b.c.example.com/index.html will translate to /var/www/vhost/example.com/a.b.c/index.html

To do this, you need to add on Debian create the following file /etc/lighttpd/conf-available/10-vhost.conf

#Set default vhost server location here
evhost.path-pattern = "/var/www/vhost/%2.%1/"


#If we don't have a %3, default to www
$HTTP["host"] =~ "^[^.]+\.[^.]+$" {
        evhost.path-pattern = "/var/www/vhost/%2.%1/www/"
}
#If we don't have a %4, default to empty, without point
$HTTP["host"] =~ "^[^.]+\.[^.]+\.[^.]+$" {
        evhost.path-pattern = "/var/www/vhost/%2.%1/%3/"
}
#If we have have %4+, use %4
$HTTP["host"] =~ "^.+\.[^.]+\.[^.]+\.[^.]+$" {
        evhost.path-pattern = "/var/www/vhost/%2.%1/%4.%3/"
}

And then link it into the directory
cd /etc/lighttpd/conf-enabled
ln -s ../conf-available/10-vhost.conf

Now restart lighttpd

/etc/init.d/lighttpd restart

And that's it! Point any domain to your server and if the directory exists, it will start hosting files.