[lighttpd] Automatic Vhosts

Zarjazz

New Member
Here is a nice little trick in lighttpd that can be used to make automatic vhosts for subdomains.

Firstly enable mod_evhost in server.modules. Then add this to lighttpd.conf, modifying as required, and restart lighttpd.

Code:
var.basedir = "/data/www/"
var.baselog = "/var/log/lighttpd/"

$HTTP["host"] =~ "(^|\.)users\.myhost\.com$" {
  accesslog.filename    = baselog + "users.myhost.com.access.log"
  server.document-root  = basedir + "users.myhost.com/www/"
  $HTTP["host"] =~ "\.users\.myhost\.com$" {
    evhost.path-pattern = basedir + "users.myhost.com/%4/"
  }
}
Now if the directory structure is as follows:

Code:
/data/www/users.myhost.com/
    -> fred/
    -> sally/
    -> www/
Web requests for the following urls are relocated as follows:

Code:
    http://users.myhost.com/        => /data/www/users.myhost.com/www/   - default website
    http://chris.users.myhost.com/  => /data/www/users.myhost.com/www/   - the user directory does not exist, uses the default location
    http://fred.users.myhost.com/   => /data/www/users.myhost.com/fred/  - relocate to the users home directory
    http://sally.users.myhost.com/  => /data/www/users.myhost.com/sally/ - relocate to the users home directory
So no extra configuration is required for new users, just create their directory. Naturally the directory locations could also be modified to fit the server requirements, e.g.: /home/users/%4/public_html

The same method can be applied for example to having a live and various testing / debug versions of your main website, e.g.: http://v2-01.mysite.com/
 
Top Bottom