lighty's life

lighty developer blog

Simplify Your Configfiles With Includes

Or should it be named ‘virtual hosting made easy ?’ Anyway. If your vhosting setup looks a bit more complext like having different options (static only, php support, pre-installed rails apps, …) but a similar setup for each you can’t use the vhosting modules and have to write everything by hand. But there is rescue thanks to includes and variables.

The idea is to modularize the configfile and only include the parts of the config for a vhost that it needs.

But first we define our setup:

  1. All vhosts are under __ /var/www/servers//pages/ __
  2. you have some vhost that have protected folders
  3. some may have PHP support, some may use pre-installed rails apps

The classic way is:


$HTTP[“host”] == “www.example.org” {
server.document-root = “/var/www/servers/www.example.org/pages/”
auth.backend = “htpasswd”
auth.backend.htpasswd.userfile = “/var/www/servers/www.example.org/htpasswd”
auth.require = …
}

We specify the path in the full lenth both times. Let’s tackle this one first:

$HTTP["host"] == "www.example.org" {
  var.basedir = "/var/www/servers/www.example.org/" 
  server.document-root = basedir + "pages/"
  auth.backend = "htpasswd"
  auth.backend.htpasswd.userfile = basedir + "/htpasswd"
  auth.require = ... 
}

But this is just one host, let’s add another one, just serving static files:

$HTTP["host"] == "www.example.com" {
  var.basedir = "/var/www/servers/www.example.com/" 
  server.document-root = basedir + "pages/"
}

Both have the same directory root __ /var/www/servers/ __ let’s move it out.

var.basedir = "/var/www/servers/"
$HTTP["host"] == "www.example.org" {
  var.servername = "www.example.org"

  server.document-root = basedir + servername + "/pages/"
  auth.backend = "htpasswd"
  auth.backend.htpasswd.userfile = basedir + servername + "/htpasswd"
  auth.require = ... 
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com"

  server.document-root = basedir + servername + "/pages/"
}

The server.document-root setting is now exactly the same for both servers, let’s move it out again, this time into a include file called __ incl-docroot.conf __:

## set the docroot based on basedir and servername 
## both have to be defined before
  server.document-root = basedir + servername + "/pages/"

and here is our new configfile:

var.basedir = "/var/www/servers/"
$HTTP["host"] == "www.example.org" {
  var.servername = "www.example.org"

  include "incl-docroot.conf"

  auth.backend = "htpasswd"
  auth.backend.htpasswd.userfile = basedir + servername + "/htpasswd"
  auth.require = ( "/download" => ... )
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com"

  include "incl-docroot.conf"
}

Last step is moving the auth part into a include-file too called __ incl-auth-htpasswd.conf __:

## set authentificate for a directory
auth.backend = "htpasswd"
auth.backend.htpasswd.userfile = basedir + servername + "/htpasswd"
auth.require = ( authdir => ... )

and our configfile:

var.basedir = "/var/www/servers/"
$HTTP["host"] == "www.example.org" {
  var.servername = "www.example.org"
  var.authdir = "/download/"

  include "incl-docroot.conf"
  include "incl-auth-htpasswd.conf"
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com"

  include "incl-docroot.conf"
}

Ok, last step is FastCGI for a host. We create a include-file from the start called __ incl-fastcgi-php.conf __:

fastcgi.server = ( ".php" => ((
  "bin-path" => "/usr/bin/php-cgi",
  "socket" => basedir + servername + "/tmp/php-" + PID + ".socket"
)))

If a host wants PHP support we just include this file:

var.basedir = "/var/www/servers/"
$HTTP["host"] == "www.example.org" {
  var.servername = "www.example.org"
  var.authdir = "/download/"

  include "incl-docroot.conf"
  include "incl-auth-htpasswd.conf"
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com"

  include "incl-docroot.conf"
  include "incl-fastcgi-php.conf"
}

Which leads to the last issue: than one name for a vhost. Let’s say __ www.example.org __ and __ example.org __ are the same vhost with different names.

var.basedir = "/var/www/servers/"
$HTTP["host"] =~ "^(www\.)?example\.org$" {
  var.servername = "www.example.org"
  var.authdir = "/download/"

  include "incl-docroot.conf"
  include "incl-auth-htpasswd.conf"
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com"

  include "incl-docroot.conf"
  include "incl-fastcgi-php.conf"
}

Now I leave it up to you to write a web-frontend to generate these modular configfiles for your setup.

Include and variables work since 1.4.0, the PID variable is available since 1.4.6 IIRC.

Please note that we won't accept comments for posts older than 3 months! Also please use our bug tracker to report bugs, and our irc channel #lighttpd@libera to chat.

« lighttpd 1.4.8 and multiple rails-apps In The News »