simplify your configfiles with includes 11
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:
- All vhosts are under _ /var/www/servers/
/pages/ _ - you have some vhost that have protected folders
- some may have PHP support, some may use pre-installed rails apps
$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.
Trackbacks
Use the following link to trackback from your own site:
http://blog.lighttpd.net/articles/trackback/46
Comments
-
Hi lighty, just my 2 cents totally off topic. I've just spent cca 20 minutes trying to figure how to run httpd on windows. No success, so I'm feeling totally dumb. I've got WAMP on development machine, and linux on the server [which I do not administer]. Have you ever consider to make it easier for guys like me to offer win port of lighttpd? If windows developers (me :) could try lighttpd in their enviroment, they may help to increase number of lighhtpd instalations on linux servers :) [Sorry 4 my eng]
-
I've put together a Windows setup for version 1.4.7, available here: http://www.kevinworthington.com:8181/?p=85
-
Sorry.. but ineed to know. IS THAT LIGHTHTTPD works on WINDOWS. or XP or 2000 .... idont find anysupport. Let me know it.
-
To 'Infest': I compiled Lighttpd with Cygwin on Windows, and made a Windows setup package to install it on Windows 2000 or XP, without the need for Cygwin. In other words, download the setup and run it, and Lighttpd will be up and running on Windows. I included a README file of how to do things in regards to my packaged version, but for all other inquiries, see lighttpd.net.
-
nice feature but better would be a possibility to add those includes into mysql_vhost too or even better: what about a text field more which is a list of settings which were usually in brackets in the lighty conf?! that would kick really sum butts ;) also i dont want to reload/restart the webserver everytime a new customers comes in, thats why we usin mysql_vhost. regards Simon
-
Simons right. Everytime the conifg changes lighty has to be restarted. This would be a thing i would love to see fixed in 1.5 ;-)
-
ok sorry i found one of your older post which explains the reload thing. ;) http://blog.lighttpd.net/articles/2005/09/02/graceful-restart
-
Cool :)
-
like suggested a dyn conf file generator http://www.bigbold.com/snippets/posts/show/1017
-
i did not talk about reload. reload works but manual reloading is no wished. i did talk about mysql_vhost and the inconsequence in adding mysql_vhost but not adding features so u have everything related to virtualhosting in database. it could be pretty simple just by adding a text field to the table and use this the same way as a HTTP["host"] directive. as long we have to put virtualhosting into lighty's conf although we use mysql_vhost, the advantage of mysql_vhost is wasted. Simon
-
If would be nice if lighttpd allows empty initialization of "configuration arrays". I mean something like: server.modules += ( "mod_alias" ) alias.url = () then somewhere else alias.url += ( "/doc/" = "/var/doc" ) or second solution += operator could initialize "configuration arrays"