Ubuntu Reference Guide

part of the WillPowered Inc. network
© 2011 William Hall <5/> - This site uses HTML5

Lighttpd\Vhosts

Here are some extra modifications you might want to make to your lighttpd server especially if you want to host multiple websites.

If you want to specify a custom 404 page for a virtual host in the event you haven't used a rewrite to bootstrap your site, add the following directive to the lighttpd.conf file to make the server use a 404 file in the root of each virtual host:

server.error-handler-404 = "/404.html"

For a custom 404 page to apply to all virtual hosts use the following directive to specify a folder to store all the error HTML files:

server.errorfile-prefix = "/var/www/errors/"

You may also choose to add a "WTFD" page to be /var/www/index.html to act as a something-should-be-set-up-but-is-not page. Example.

To keep your hosts seperate from the main file. You can create your virtual hosts by creating them in the following file:

$ sudo nano /etc/lighttpd/conf-enabled/vhosts.conf

Specify virtual hosts in the following style (note the ~ and the use of regular expressions in the URL):

$HTTP["host"] =~ "(^|\.)domain\.co\.uk$" { server.document-root = "/var/sites/domain.co.uk" }

If you have issues with many subdomains and none of them are displaying the right content use this style (note the double equals and no regular expressions):

$HTTP["host"] == "domain.co.uk" { server.document-root = "/var/sites/domain.co.uk" }

If you want a domain to redirect to another, use the following line in place of server.document-root

url.redirect = ( "^/(.*)" => "http://www.domain.co.uk/$1" )

If you want a directory to be aliased use the following directive:

alias.url += ( "/something" => "/somewhere/somethingelse" )

To activate directory listing on a subfolder add this line inside the vhost.

$HTTP["url"] =~ "^/something" { dir-listing.activate = "enable" }

You can also block hotlinking of your files (specified inside a virtual host).

$HTTP["referer"] !~ "^($|http://(example\.co\.uk|demo\.example\.co\.uk))" { url.access-deny = ( ".jpg" ) }

Remember to chown the sites directory to www-data:www-data and chmod to 755.

References
Other cool things