If you tell Apache to use CustomLog files, Apache2 will create these files on startup. And it always gives them to user root:root. How can I change this behavior?
Background
Apache runs with
SuexecUserGroup www-data www-data
and at the same time, I use a CustomLog that pipes it output to a script. The script removes two bytes from the IP and then wirtes to the logfile. As you cannot tell Apache to omit the IP from the error.log, this piped-output is important (regarding German privacy law).
The script cannot access my custom log if this does not belong to www-data.
If I change the owner, everthing works fine.
I also know how to change the file owner when logrotate renames and re-creates the logfile.
However, if I stop the Apache process, delete the logfiles, and then restart the Apache process, new files beloging to root:root are created.
How can I tell Apache to create the new, void files as/for www-data on startup?
This sentence:
Is in direct contradiction with http://httpd.apache.org/docs/current/logs.html#piped:
If the scenario you describe is somehow correct, you can still sidestep the issue by
I ran into the same problem, yet being unable to change the script that writes the log, I could change the script that interpreted it.
Setup:
root:root
instead ofwww-data:www-data
. This is because of the situation mentioned in OPs question.www-data
) but it can't read the file. I do this as a cron from roots crontabroot
) the commandchown www-data:www-data /var/log/apache2/*.log
. This changes the ownership of the logfiles (but not the parent directory) towww-data
as intended and the analysis script can then read the file.Not the nicest solution and definitely not what you would want on a busy server, but on a small installation its good enough.
gpjod mentions that this might be a security problem. The Apache Documentation says that the directory for the logs should not be writable to others.
This is true but most likely true for all situations that solve the OPs question and you might want to read the security tips in the linked documentation. As in this particular instance the ownership is changed from
root
towww-data
as it is intended to be (see the original question) I see no additional thread arising from my solution.