2

I have a PHP application which is used by multiple domains. To avoid maintaining multiple vhosts, I have just setup a single "default" Apache vhost to direct any incoming request to the server to the application directory.

What I want to do is to set the Apache access and error log paths dynamically based on the hostname hitting the server.

For example, I would like to set the log paths to be something like:

/var/log/application_name/example.com/error.log
/var/log/application_name/example.com/access.log

when a request to example.com is made.

Is there a viable way to do this? I've looked at using any of the Apache environment variables, but as these are setup as the request is captured, I don't think these would be available for use in the ErrorLog or CustomLog directives. Is it that I just need to set the log directory manually at the application level (i.e in PHP)?

Thanks

Kaii
  • 20,122
  • 3
  • 38
  • 60
James
  • 1,950
  • 3
  • 22
  • 39

2 Answers2

8

I use a setup like this where my logsplit.sh script writes to log files based on the %U:

<VirtualHost *:80>
    ServerName myserver.com
    ServerAlias *.myserver.com
    VirtualDocumentRoot /home/%1/www/
    LogFormat "%U %h %l %u %t \"%r\" %>s %b" common
    CustomLog "|/usr/local/logsplit.sh" common
</VirtualHost>
Jesper Blaase
  • 2,320
  • 16
  • 13
  • Can I not do something like: `CustomLog "/usr/local/%{HTTP_HOST}.log" common`. Or at least pass ${HTTP_HOST} through as an argument to logsplit.sh so I am able to split by host? – James Nov 26 '13 at 15:16
  • 2
    Sure you could do this: LogFormat "%{HTTP_HOST} %h %l %u %t \"%r\" %>s %b" common Unfortunately you cant use vars in log file paths – Jesper Blaase Nov 27 '13 at 11:04
-1

You can do this with VirtualHosts by only maintaining 1 VirtualHost for several Domains using ServerAlias:

<VirtualHost *:80>
        ServerAdmin ...
        ServerName domain1.bla.com
        ServerAlias service.bla.com domain5.domain.xxx
        DocumentRoot /www/vhosts/xxx/public

        ErrorLog /www/vhosts/xxx/log/error.log
        CustomLog /www/vhosts/ccc/log/access.log combined
        LogLevel warn

        <Directory "/www/vhosts/ccc/public">
                Options FollowSymLinks MultiViews
                php_admin_flag safe_mode On
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>
Daniel W.
  • 31,164
  • 13
  • 93
  • 151