cd /usr/local/etc/lighttpd

# Create srv directory structure
# create: mtree -x -c -i -n -k uname,gname,mode,link,nochange -p /srv
# apply: mtree -x -e -U -f srv_www.mtree -p /srv
cat <<"EOF" | tee srv_www.mtree | mtree -x -e -U -p /srv
/set type=file uname=root gname=www mode=0755
.               type=dir gname=wheel

    www             type=dir
        conf            type=link gname=www \
                        link=../../usr/local/etc/lighttpd

        etc             type=dir mode=0750
        ..


        log             type=dir uname=www
        ..


        root            type=dir
        ..


/set type=file uname=www gname=www mode=0770
        run             type=dir uname=root mode=0750

            cache           type=dir
            ..


            home            type=dir
            ..


            sock            type=dir
            ..

        ..


        tmp             type=dir uname=root mode=01770
        ..


        vhost           type=dir uname=root mode=0755
        ..

    ..

..
EOF

mv vhosts.d sample-vhosts.d
mkdir vhost.d
chown :www vhost.d

# Patch lighttpd.conf to use srv directory structure
cat <<"EOF" | tee lighttpd.conf.diff | patch -p0
--- lighttpd.conf       2013-07-07 12:15:35.000000000 +0000
+++ lighttpd.conf       2013-07-07 23:40:17.184628687 +0000
@@ -13,11 +13,12 @@
 ## if you add a variable here. Add the corresponding variable in the
 ## chroot example aswell.
 ##
-var.log_root    = "/var/log/lighttpd"
-var.server_root = "/usr/local/www/data"
-var.state_dir   = "/var/run"
-var.home_dir    = "/var/spool/lighttpd"
-var.conf_dir    = "/usr/local/etc/lighttpd"
+var.server_root = "/srv/www"
+var.conf_dir    = server_root + "/conf"
+var.log_root    = server_root + "/log"
+var.state_dir   = server_root + "/run"
+var.home_dir    = server_root + "/run/home"
+var.temp_dir    = server_root + "/tmp"

 ##
 ## run the server chrooted.
@@ -50,7 +51,8 @@
 ## conf.d/simple_vhost.conf
 ## vhosts.d/vhosts.template
 ##
-var.vhosts_dir  = server_root + "/vhosts"
+var.vhosts_dir  = server_root + "/vhost"
+var.vhost_dir   = server_root + "/vhost"

 ##
 ## Cache for mod_compress
@@ -58,7 +60,7 @@
 ## used in:
 ## conf.d/compress.conf
 ##
-var.cache_dir   = "/var/cache/lighttpd"
+var.cache_dir   = state_dir + "/cache"

 ##
 ## Base directory for sockets.
@@ -67,7 +69,7 @@
 ## conf.d/fastcgi.conf
 ## conf.d/scgi.conf
 ##
-var.socket_dir  = home_dir + "/sockets"
+var.socket_dir  = state_dir + "/sock"

 ##
 #######################################################################
@@ -112,7 +114,7 @@
 ##
 ## Document root
 ##
-server.document-root = "/usr/local/www/data/"
+server.document-root = server_root + "/root"

 ##
 ## The value for the "Server:" response field.
@@ -370,7 +372,7 @@
 ##
 ## defaults to /var/tmp as we assume it is a local harddisk
 ##
-server.upload-dirs = ( "/var/tmp" )
+server.upload-dirs = ( temp_dir )

 ##
 #######################################################################
@@ -441,6 +443,7 @@
 ##
 #include "conf.d/config.conf"
 #include_shell "cat /usr/local/etc/lighttpd/vhosts.d/*.conf"
+include_shell "ls -1 " + conf_dir + "/vhost.d/*.conf | sed -e 's/^.*$/include \"&\"/'"
 ##
 #######################################################################

EOF

# Add defaults files
cat <<"EOF" > vhost.d/%static.defaults
# Defaults for static vhosts
# Include this as follows:
#   $HTTP["host"] == "subdomain.domain.tld" {
#     server.name = "subdomain.domain.tld"
#     include "vhost.d/%static.defaults"
#
#     ... Additional configuration here
#   }

server.document-root = vhost_dir + "/" + server.name + "/root"
accesslog.filename = log_root + "/" + server.name + "/access.log"
EOF
cat <<"EOF" > vhost.d/%php.defaults
# Defaults for php-enabled vhosts
# Include this as follows:
#   $HTTP["host"] == "subdomain.domain.tld" {
#     server.name = "subdomain.domain.tld"
#     include "vhost.d/%php.defaults"
#
#     ... Additional configuration here
#   }

include "vhost.d/%static.defaults"

fastcgi.server = (
  ".php" => ((
    "socket" => "/srv/www/run/sock/php-fpm-" + server.name + ".sock"
  )),
  "/php-status" => ((
    "socket" => "/srv/www/run/sock/php-fpm-" + server.name + ".sock",
    "check-local" => "disable"
  ))
)
EOF

# Add PHP-FPM configuration
cd /usr/local/etc
cat <<"EOF" > php-fpm.conf
[global]
pid = /var/run/php-fpm.pid
error_log = /srv/www/log/%server/php-fpm.log
include = /usr/local/etc/php-fpm.pool.d/*.conf
EOF
mkdir php-fpm.pool.d
cat <<"EOF" > php-fpm.pool.d/%pool.defaults
; Global defaults for pools
; Include this file at the top of a [pool] definition:
;   include = /usr/local/etc/php-fpm.pool.d/%pool.defaults

user = www
group = www
listen = /srv/www/run/sock/php-fpm-$pool.sock
chdir = /srv/www/run/home

pm = ondemand
pm.max_children = 32
pm.process_idle_timeout = 30s
pm.status_path = /php-status

slowlog = /srv/www/log/$pool/php-slow.log
request_slowlog_timeout = 5s

php_admin_value[display_errors] = off
php_admin_value[log_errors] = on
php_admin_value[error_log] = /srv/www/log/$pool/php-error.log
EOF