Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a working per-user virtual host configuration with Apache, but I would like each user to have access to the logs for his virtual hosts. Obviously the ErrorLog and CustomLog directives don't accept the wildcard syntax that VirtualDocumentRoot does, but is there a way to achieve logs in each user's directory?

<VirtualHost *:80>
    ServerName *.example.com
    ServerAdmin webmaster@example.com
    VirtualDocumentRoot /home/%2/projects/%1
    <Directory /home/*/projects/>
        Options FollowSymlinks Indexes
        IndexOptions FancyIndexing FoldersFirst
        AllowOverride All
        Order Allow,Deny
        Allow From All
        Satisfy Any
    </Directory>

    Alias /favicon.ico /var/www/default/favicon.ico
    Alias /robots.txt /var/www/default/robots.txt

    LogLevel warn
    # ErrorLog /home/%2/logs/%1.error.log
    # CustomLog /home/%2/logs/%1.access.log combined
</VirtualHost>

Resolution: Based on @cjc's fine answer I realized I needed to do some scripting to solve this, so here's what I came up with:

#!/bin/bash

##
# Write whatever comes through stdin to the log directory in each user's home directory.
##

# The first field in the incoming log must be the vhost name
# (%V in the Apache LogFormat).
while read vhost fields; do
        # vhost names take the form $project.$user.$host.         
        project="${vhost%%.*}" # Strip off everything after the first dotted name.
        user="${vhost#*.}" # Strip off the first dotted name.
        user="${user%%.*}" # Strip off everything but the first (remaining) dotted name.
        printf -v cmd "mkdir -p '/home/%s/log' && printf '%%s\n' '%s' >> '/home/%s/log/%s.log'" "$user" "$fields" "$user" "$project"
        sudo -u "$user" -- bash -c "$cmd"
done
share|improve this question

1 Answer 1

up vote 1 down vote accepted

I don't believe you can accomplish this within Apache itself, but you can have logs piped to a script that can split it in the way you're trying to do:

Look at this part of the Apache documentation:

http://httpd.apache.org/docs/2.2/logs.html#piped

So you'll have a line like:

CustomLog "|/usr/local/bin/per-user-web-logs.pl" combined

where /usr/local/bin/per-user-web-logs.pl is, say, a Perl script that will append to the right per-user log.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.