Apache server -- multiple directories, different error logs
Asked Answered
S

3

6

I have two directories in /var/www (say, /var/www/app1 and /var/www/app2) whose error logs I want sent to different files. Both are under the same domain, so I think that I can't put them under different virtual hosts. So, for example, I would access them as:

http://localhost/app1

http://localhost/app2

I came across this page:

Generate access logs for different subdirectories in Apache

whose solution works perfectly for the access logs. However, the "env" argument doesn't seem to work with the ErrorLog directive.

Before this "discovery", I was working on this, which seems wrong:

<VirtualHost *:80>
  ServerAdmin ray@localhost

  DocumentRoot /var/www/app1

  <Directory />
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order deny,allow
    allow from all
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/app1/error.log

  LogLevel warn

  CustomLog ${APACHE_LOG_DIR}/app1/access.log combined
</VirtualHost>

I'm somewhat lost about what I should be doing. That is, if there is some way to get ErrorLog to work or if I should keep trying with configuring a virtual host for each directory. Any help would be appreciated! Thank you!

Selfinsurance answered 1/5, 2012 at 7:54 Comment(2)
I have the same question, I'm looking for solution and I think creating a virtualhost by each folder and proxying internally maybe it works, it's only a theory :D for example: PublicDomain.com/folder shows content of folder.localhostGenni
This is not OP's issue, but be aware - it can break when you have spaces in the path, if you do, you have to quote the path e.g. /media/u/my super folder => "/media/u/my super folder"Downspout
P
9

Why do you set Directory options for / in the VirtualHost context? Use <Directory /var/www/app1> instead of <Directory />

Due to the Apache ErrorLog directive docs its context is server config, virtual host - which means that it's only possible to define ErrorLog for the whole server or for a VirtalHost, not for a Directory. So if you want to send different logs to different files, try to use SetEnvIf to set an Env variable. Depeding on the directory where you are, it should be something like SetEnvIf Request_URI ^\/a1\/ a1 and SetEnvIf Request_URI ^\/a2\/ !a1. Then write logs depending on the a1 environment variable.

Palsy answered 4/5, 2012 at 11:7 Comment(2)
Thanks for correcting my "Directory" mistake! As for the ErrorLog, I did find out how environments work for separating out the access log. But it doesn't seem to work for the error log. Is my understanding correct? (When I try, I get an error while Apache is parsing the configuration file.) I guess you are right -- the error log is for a whole server or virtual host. Based on the link that you sent, I suppose another option is to pipe each error log entry to a script... Thanks a lot for your help!Selfinsurance
Yes, I've searched that too and seems like ErrorLog files can't be separated with SetEnvIf. You are welcome.Palsy
L
3

Set custom ID for every Directory and you can separate logs by directories like this:

<Directory app1>
    SetEnv app1
</Directory>
<Directory app2>
    SetEnv app2
</Directory>
CustomLog ${APACHE_LOG_DIR}/site1.log combined env=app1
CustomLog ${APACHE_LOG_DIR}/site2.log combined env=app2
Lantern answered 25/7, 2019 at 19:39 Comment(4)
was env=subwebsite1 intended to actually be env=app1? Otherwise, how "subwebsite1" is matched with "app1"?Azarria
@Azarria You're right, I edited my answer.Lantern
right, thanks. Do you know if something similar may be used with reverse proxy? (https://mcmap.net/q/1770041/-how-to-set-different-logs-for-different-proxies-apps-in-apache2/3995261)Azarria
@Azarria Sorry, I don't know.Lantern
G
2

Finally I did it, first create internal subdomains per folder and with proxypass pass the subdomain content.

Enable apache mods:

a2enmod authz_core dir proxy proxy_http

/etc/hosts

127.0.0.1       localhost
127.0.0.1       a.localhost
127.0.0.1       b.localhost

/etc/apache2/sites-available/default.conf

<VirtualHost *:80>
        ServerName localhost
        ServerAdmin [email protected]
        DocumentRoot "/dev/null"

        ProxyPass /a http://a.localhost/
        ProxyPassReverse /a http://a.localhost/
        ProxyPass /b http://b.localhost/
        ProxyPassReverse /b http://b.localhost/

        LogLevel debug

        ErrorLog ${APACHE_LOG_DIR}/default-error.log
        CustomLog ${APACHE_LOG_DIR}/default-access.log combined

</VirtualHost>

/etc/apache2/sites-available/a.conf

<VirtualHost *:80>
        ServerName a.localhost
        ServerAdmin [email protected]
        DocumentRoot "/Publikoa/a"

        <Directory "/Publikoa/a">
                DirectoryIndex index.html
                Require all granted
        </Directory>

        LogLevel debug

        ErrorLog ${APACHE_LOG_DIR}/a-error.log
        CustomLog ${APACHE_LOG_DIR}/a-access.log combined

</VirtualHost>

/etc/apache2/sites-available/b.conf

<VirtualHost *:80>
        ServerName b.localhost
        ServerAdmin [email protected]
        DocumentRoot "/Publikoa/b"

        <Directory "Publikoa/b">
                DirectoryIndex index.html
                Require all granted
        </Directory>

        LogLevel debug

        ErrorLog ${APACHE_LOG_DIR}/b-error.log
        CustomLog ${APACHE_LOG_DIR}/b-access.log combined

</VirtualHost>

Enable sites:

a2ensite default a b

Restart apache:

/etc/init.d/apache2 restart
Genni answered 1/5, 2018 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.