我有一个工作的每用户虚拟主机configuration与Apache,但我希望每个用户有权访问他的虚拟主机的日志。 显然,ErrorLog和CustomLog指令不接受VirtualDocumentRoot所做的通配符语法,但有没有一种方法可以在每个用户的目录中实现日志?
<VirtualHost *:80> ServerName *.example.com ServerAdmin [email protected] 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>
解决scheme:基于@ cjc的正确答案,我意识到我需要做一些脚本来解决这个问题,所以这是我想出来的:
#!/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
我不相信你可以在Apache本身内完成这个工作,但是你可以把日志传送到一个脚本中,这个脚本可以按你想要的方式分割它:
看看Apache文档的这一部分:
http://httpd.apache.org/docs/2.2/logs.html#piped
所以你会有这样一行:
CustomLog "|/usr/local/bin/per-user-web-logs.pl" combined
其中/usr/local/bin/per-user-web-logs.pl是一个Perl脚本,将附加到正确的每个用户日志。