Keeping Apache 2.0 logs useful and under control
Access and error logs answer different questions. Keep both, decide how long they are needed and make rotation part of the server’s maintenance. Deleting a large log without understanding the open file can leave the disk full and remove the evidence needed to investigate it.
Identify the service that produced each entry
Give important virtual hosts their own access and error logs, or include enough host information to distinguish them. The combined access format adds the referrer and user agent to the usual request record. Those values are supplied by clients; treat them as diagnostic clues rather than proof of a visitor’s identity.
Protect the log directory
Keep the parent directory under administrator control and grant only the read access needed for analysis. A website account should not be able to replace files that the server opens with greater privileges. Avoid putting passwords or sensitive form values in URLs, since the requested URL is normally logged. Do not add cookie contents to a logging format merely to simplify debugging.
Rotate files that Apache has open
On Unix, renaming a log leaves existing processes writing to the same open file. A normal rotation renames it and performs an Apache graceful restart so new workers open a new log. Old workers can continue writing while their requests finish. Delay compression or removal until they have finished; do not assume a fixed short sleep proves this.
Use piped rotation when appropriate
Apache can send log records to a program such as rotatelogs instead of directly to a file. This avoids restarting Apache solely to start the next log period. Use the executable’s full path, test its invocation and confirm the destination has space. Date-based filenames still need a separate retention policy; rotation does not automatically remove every old file.
Prove the procedure with a request
Run apachectl configtest before applying a configuration change. Make a known request after rotation and locate it in the new access log. Check the error log and compare file ownership with the intended policy. Monitor disk usage as well as job completion, and retain sufficient overlap to investigate an incident that is reported several days after it happened.
Keep the rotation script and retention schedule with the server configuration. Test them again when adding a virtual host or changing the account under which log processing runs.