simplify your configfiles with includes 11

Posted by jan Fri, 25 Nov 2005 13:45:00 GMT

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.