<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>lighty's life: Tag includes</title>
    <link>http://blog.lighttpd.net/articles/tag/includes</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>simplify your configfiles with includes</title>
      <description>&lt;p&gt;Or should it be named &amp;#8216;virtual hosting made easy ?&amp;#8217; 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&amp;#8217;t use the vhosting modules and have to write everything by hand. But there is rescue thanks to includes and variables.&lt;/p&gt;
&lt;p&gt;The idea is to modularize the configfile and only include the parts of the config for a vhost that it needs.&lt;/p&gt;


	&lt;p&gt;But first we define our setup:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;All vhosts are under &lt;em&gt;_ /var/www/servers/&lt;domain&gt;/pages/ _&lt;/em&gt;&lt;/li&gt;
		&lt;li&gt;you have some vhost that have protected folders&lt;/li&gt;
		&lt;li&gt;some may have &lt;span class="caps"&gt;PHP&lt;/span&gt; support, some may use pre-installed rails apps&lt;/li&gt;
	&lt;/ol&gt;


The classic way is:
&lt;pre&gt;
$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 = ... 
}
&lt;/pre&gt;

	&lt;p&gt;We specify the path in the full lenth both times. Let&amp;#8217;s tackle this one first:&lt;/p&gt;


&lt;pre&gt;
$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 = ... 
}
&lt;/pre&gt;

	&lt;p&gt;But this is just one host, let&amp;#8217;s add another one, just serving static files:&lt;/p&gt;


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

	&lt;p&gt;Both have the same directory root &lt;em&gt;_ /var/www/servers/ _&lt;/em&gt; let&amp;#8217;s move it out.&lt;/p&gt;


&lt;pre&gt;
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/" 
}
&lt;/pre&gt;

	&lt;p&gt;The server.document-root setting is now exactly the same for both servers, let&amp;#8217;s move it out again, this time into a include file called &lt;em&gt;_ incl-docroot.conf _&lt;/em&gt;:&lt;/p&gt;


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

	&lt;p&gt;and here is our new configfile:&lt;/p&gt;


&lt;pre&gt;
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" =&amp;gt; ... )
}
$HTTP["host"] == "www.example.com" {
  var.servername = "www.example.com" 

  include "incl-docroot.conf" 
}
&lt;/pre&gt;

	&lt;p&gt;Last step is moving the auth part into a include-file too called &lt;em&gt;_ incl-auth-htpasswd.conf _&lt;/em&gt;:&lt;/p&gt;


&lt;pre&gt;
## set authentificate for a directory
auth.backend = "htpasswd" 
auth.backend.htpasswd.userfile = basedir + servername + "/htpasswd" 
auth.require = ( authdir =&amp;gt; ... )
&lt;/pre&gt;

	&lt;p&gt;and our configfile:&lt;/p&gt;


&lt;pre&gt;
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" 
}
&lt;/pre&gt;

	&lt;p&gt;Ok, last step is FastCGI for a host. We create a include-file from the start called &lt;em&gt;_ incl-fastcgi-php.conf _&lt;/em&gt;:&lt;/p&gt;


&lt;pre&gt;
fastcgi.server = ( ".php" =&amp;gt; ((
  "bin-path" =&amp;gt; "/usr/bin/php-cgi",
  "socket" =&amp;gt; basedir + servername + "/tmp/php-" + PID + ".socket" 
)))
&lt;/pre&gt;

	&lt;p&gt;If a host wants &lt;span class="caps"&gt;PHP&lt;/span&gt; support we just include this file:&lt;/p&gt;


&lt;pre&gt;
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" 
}
&lt;/pre&gt;

	&lt;p&gt;Which leads to the last issue: than one name for a vhost. Let&amp;#8217;s say &lt;em&gt;_ www.example.org _&lt;/em&gt; and &lt;em&gt;_ example.org _&lt;/em&gt;  are the same vhost with different names.&lt;/p&gt;


&lt;pre&gt;
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" 
}
&lt;/pre&gt;

	&lt;p&gt;Now I leave it up to you to write a web-frontend to generate these modular configfiles for your setup.&lt;/p&gt;


	&lt;p&gt;Include and variables work since 1.4.0, the &lt;span class="caps"&gt;PID&lt;/span&gt; variable is available since 1.4.6 &lt;span class="caps"&gt;IIRC&lt;/span&gt;.&lt;/p&gt;</description>
      <pubDate>Fri, 25 Nov 2005 13:45:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d225d844-e1ce-4e83-88b3-7dd4c235f206</guid>
      <author>jan</author>
      <link>http://blog.lighttpd.net/articles/2005/11/25/simplify-your-configfiles-with-includes</link>
      <category>configfile</category>
      <category>includes</category>
      <trackback:ping>http://blog.lighttpd.net/articles/trackback/46</trackback:ping>
    </item>
  </channel>
</rss>
