<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: virtualhosting code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 07 Aug 2008 21:36:29 GMT</pubDate>
    <description>DZone Snippets: virtualhosting code</description>
    <item>
      <title>A tool to split log files by hostname</title>
      <link>http://snippets.dzone.com/posts/show/2421</link>
      <description>This script can be used to split a log file by the hostname in the request. It is designed for use with lighttpd virtual hosting, to prepare the log for Webalizer. It should be run with a cron job and CLI PHP.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;// Copyright (C) 2006 Craig Spurrier&lt;br /&gt;// Released under the terms of the MIT/expat license.&lt;br /&gt;&lt;br /&gt;$serverip = "127.0.0.1"; //Set to your IP address or default hostname, so requests without hostnames can be sorted &lt;br /&gt;$log = file_get_contents("/var/log/lighttpd/access.log"); //Log location&lt;br /&gt;$fh = fopen("/var/log/lighttpd/access.log", 'w'); //Clear the log file&lt;br /&gt;fclose($fh);&lt;br /&gt;$lines = explode ("\n", $log);&lt;br /&gt;foreach ($lines as $line){&lt;br /&gt;$parts = explode (" ", $line);&lt;br /&gt;$hostname = strtolower($parts[1]);&lt;br /&gt;if (substr($hostname, 0, 4) == 'www.'){$hostname = substr($hostname, 4);} //Treat www. as the same as the www-less version&lt;br /&gt;if ($hostname == '-'){$hostname = $serverip;} //Set a hostname for request without hostnames&lt;br /&gt;$fh = fopen("/var/log/lighttpd/users/$hostname.access.log", 'a'); //Add the entry to the log for that hostname. Make sure this script can write to this location.&lt;br /&gt;fwrite($fh, "$line\n");&lt;br /&gt;fclose($fh);&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 15 Aug 2006 03:58:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2421</guid>
      <author>cspurrier (Craig Spurrier)</author>
    </item>
    <item>
      <title>create lighttpd conf files for vhosting</title>
      <link>http://snippets.dzone.com/posts/show/1017</link>
      <description>Creates lighttpd conf files like suggested on http://blog.lighttpd.net/articles/2005/11/25/simplify-your-configfiles-with-includes&lt;br /&gt;simply add include "incl-domains.conf" to your main lighttpd.conf file&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby -w&lt;br /&gt;#&lt;br /&gt;# light_conf_editor.rb&lt;br /&gt;#&lt;br /&gt;# Created by yo on 25.12.05.&lt;br /&gt;#&lt;br /&gt;#&lt;br /&gt;# Description&lt;br /&gt;# Creates lighttpd conf files&lt;br /&gt;#&lt;br /&gt;# usage: light_conf_editor add || remove domainname username&lt;br /&gt;# example: light_conf_editor add bigcurl.com yo&lt;br /&gt;# example: light_conf_editor remove bigcurl.com yo&lt;br /&gt;&lt;br /&gt;path_to_lighttpd_conf="/etc/lighttpd"&lt;br /&gt;&lt;br /&gt;def remove_include_from_conf(domainname, username, path_to_lighttpd_conf)&lt;br /&gt;  temp_file=""&lt;br /&gt;  File.open("#{path_to_lighttpd_conf}/incl-domains.conf", "a+") unless FileTest.exist?("#{path_to_lighttpd_conf}/incl-domains.conf")&lt;br /&gt;  File.open("#{path_to_lighttpd_conf}/incl-domains.conf", "r") do |file|&lt;br /&gt;    file.each_line do |line| &lt;br /&gt;        temp_file.concat(line) unless line.include? "include \"domains/#{username}/#{domainname}.conf\""&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  File.open("#{path_to_lighttpd_conf}/incl-domains.conf", "w") do |file|&lt;br /&gt;    file.puts(temp_file)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def remove_conf_from_folder(domainname, username, path_to_lighttpd_conf)&lt;br /&gt;  File.delete("#{path_to_lighttpd_conf}/domains/#{username}/#{domainname}.conf")  if FileTest.exists?( "#{path_to_lighttpd_conf}/domains/#{username}/#{domainname}.conf" )&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def add_new_conf_in_folder(include_template, domainname, username, path_to_lighttpd_conf)&lt;br /&gt;  Dir.mkdir( "#{path_to_lighttpd_conf}/domains") unless FileTest.directory?( "#{path_to_lighttpd_conf}/domains/" )&lt;br /&gt;  Dir.mkdir( "#{path_to_lighttpd_conf}/domains/#{username}/") unless FileTest.directory?( "#{path_to_lighttpd_conf}/domains/#{username}")&lt;br /&gt;&lt;br /&gt;  aFile = File.new("#{path_to_lighttpd_conf}/domains/#{username}/#{domainname}.conf","w")&lt;br /&gt;  aFile.puts(include_template)&lt;br /&gt;  aFile.close&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def add_new_include_in_conf(domainname, username, path_to_lighttpd_conf)&lt;br /&gt;  File.open("#{path_to_lighttpd_conf}/incl-domains.conf", "a+") do |file|&lt;br /&gt;    file.puts("include \"domains/#{username}/#{domainname}.conf\"")&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def show_usage&lt;br /&gt;  print "usage: light_conf_editor add domainname username || remove domainname username\n"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if(ARGV.length &lt; 3)&lt;br /&gt;  show_usage&lt;br /&gt;  exit(1)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;domainname = ARGV[1]&lt;br /&gt;&lt;br /&gt;username = ARGV[2]&lt;br /&gt;if(ARGV[0]=="remove")&lt;br /&gt;  remove_include_from_conf(domainname, username, path_to_lighttpd_conf)&lt;br /&gt;  remove_conf_from_folder(domainname, username, path_to_lighttpd_conf)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if(ARGV[0]=="add")&lt;br /&gt;&lt;br /&gt;  include_template = "$HTTP[\"host\"] =~ \"#{domainname}$\" {&lt;br /&gt;  var.username   = \"#{username}\"&lt;br /&gt;  var.servername = \"#{domainname}\"&lt;br /&gt;  include \"incl-docroot.conf\"&lt;br /&gt;&lt;br /&gt;  server.dir-listing        = \"enable\"&lt;br /&gt;  dir-listing.hide-dotfiles = \"enable\"&lt;br /&gt;  }"&lt;br /&gt;&lt;br /&gt;  remove_include_from_conf(domainname, username, path_to_lighttpd_conf)&lt;br /&gt;  remove_conf_from_folder(domainname, username, path_to_lighttpd_conf)&lt;br /&gt;&lt;br /&gt;  add_new_conf_in_folder(include_template, domainname, username, path_to_lighttpd_conf)&lt;br /&gt;  add_new_include_in_conf(domainname, username, path_to_lighttpd_conf)&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 26 Dec 2005 09:32:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1017</guid>
      <author>yo ()</author>
    </item>
    <item>
      <title>Automatic sub-domains with lighttpd mapping to a specific folder</title>
      <link>http://snippets.dzone.com/posts/show/322</link>
      <description>Using lighttpd's mod_evhost with &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# %% =&gt; % sign&lt;br /&gt;# %0 =&gt; domain name + tld&lt;br /&gt;# %1 =&gt; tld&lt;br /&gt;# %2 =&gt; domain name without tld&lt;br /&gt;# %3 =&gt; subdomain 1 name&lt;br /&gt;# %4 =&gt; subdomain 2 name&lt;br /&gt;#&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$HTTP["host"] =~ "jasonhoffman.org" {&lt;br /&gt;evhost.path-pattern        = "/home/jah/public/%3/"&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 May 2005 18:35:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/322</guid>
      <author>jason (Jason Hoffman)</author>
    </item>
  </channel>
</rss>
