Apache produces logs all day. Left alone, the access log on a busy site grows by gigabytes per week and eventually fills the disk. Two tools rotate Apache logs: rotatelogs, the program that ships with httpd and runs as a piped-log child of the daemon; and logrotate, the system utility that runs from cron and rotates any file by renaming it. Both work. They have different failure modes, and the wrong choice is the difference between “rotation just works” and “the log file became 14 GB because Apache kept writing to the old inode.” This article is the decision tree and the configs.
How to verify
What the running Apache thinks it is doing for logs:
sudo apache2ctl -S 2>&1 | grep -i log
sudo grep -rE 'CustomLog|ErrorLog' /etc/apache2/sites-enabled/ /etc/apache2/apache2.conf
sudo lsof -p $(pgrep -f 'apache2 -k' | head -1) | grep -E '\.log$|\.gz$'
ls -lah /var/log/apache2/
lsof against the running master Apache process tells you what file descriptors are open to log files. That is the source of truth — config files describe intent, lsof shows what is actually happening. If lsof shows the daemon holding /var/log/apache2/access.log.1.gz open, you have rotated under the daemon’s feet and the old file is still receiving writes (which never reach disk meaningfully).
What’s happening
Apache writes logs by holding an open file descriptor. The daemon does not check filename or inode on each write — it just calls write(fd, ...) against whatever file descriptor it opened at startup or reload. This single fact drives the entire rotation decision.
logrotate’s default mode is “rename, signal, reopen.” It renames access.log to access.log.1, then sends a signal (typically USR1 for postrotate { /usr/sbin/apache2ctl graceful }) so the daemon catches up and reopens its file descriptors. The new descriptor points at the freshly-created access.log; the renamed access.log.1 is closed and can be compressed.
The trap is missing the signal. If logrotate rotates the file but the postrotate hook fails to signal Apache, the daemon keeps writing to access.log.1 (the inode is unchanged by the rename). The disk shows a small access.log (new) and a growing access.log.1 (old) that nobody is reading. Eventually logrotate rotates again, access.log.1 becomes access.log.2, and you have a chain of in-flight log files all being written to inodes that have moved.
logrotate’s alternative mode is copytruncate: copy the file’s contents to a .1, then truncate the original to zero. This avoids the signal-and-reopen dance — the inode does not change, so the open file descriptor is still valid. The cost is a window between copy and truncate where new writes can be lost (the writes that landed between the copy and the truncate are in the original file that just got truncated, gone). For most sites the window is microseconds and the loss is tolerable; for a billing log that cannot lose a single event, copytruncate is not safe.
rotatelogs is the Apache-native answer. The daemon launches rotatelogs as a child process and pipes log lines into it. rotatelogs itself owns the file descriptor and rotates the file on a schedule or a size threshold, opening the new file before closing the old one. The daemon never reopens anything; it just keeps writing to the pipe. There is no signal coordination, no inode race, no window of loss.
The cost is that rotatelogs produces filenames you cannot predict — the name is templated from a format string (%Y-%m-%d). External tools (goaccess, log shippers) need to glob the directory instead of expecting access.log.1.
The decision
The decision tree we use:
- High write rate (>1k req/sec) where signal-and-reopen would be too brief to be safe →
rotatelogs. The piped-log path is the safer one. - Multi-tenant box with one logrotate config covering Apache, Nginx, MySQL, and a dozen apps →
logrotatewith properpostrotatesignaling. The operational consistency is worth more than the marginal safety improvement. - Site where log loss during rotation is unacceptable →
rotatelogsorlogrotatewith the signal-based mode (notcopytruncate). - Containerized Apache shipping to stdout → neither. The container runtime handles log rotation. Adding
logrotateinside the container is a common anti-pattern.
In practice, on a typical multi-vhost server, the right answer is logrotate with postrotate-driven graceful, plus monitoring on lsof to catch the silent-failure case.
The procedure
-
For logrotate (the typical case). Save as
/etc/logrotate.d/apache2:/var/log/apache2/*.log { daily missingok rotate 30 compress delaycompress notifempty create 640 root adm sharedscripts postrotate if /etc/init.d/apache2 status > /dev/null ; then \ /etc/init.d/apache2 reload > /dev/null; \ fi; endscript prerotate if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ run-parts /etc/logrotate.d/httpd-prerotate; \ fi; endscript }Key directives:
sharedscriptsrunspostrotateonce even when multiple log files match — without it, Apache reloads N times for N vhosts’ worth of access logs.delaycompresskeeps.1uncompressed for one rotation cycle so the postrotate-reload race window does not collide with gzip touching the file.notifemptyskips rotation if the file is empty (avoids one-lineaccess.log.1.gzfiles cluttering the directory).create 640 root admrecreates the file with correct ownership; without this,apache2 reloadopens a file owned by root that the log-shipper user (in groupadm) cannot read.
-
Test the logrotate config without waiting for cron.
sudo logrotate -d /etc/logrotate.d/apache2 # dry-run, prints what would happen sudo logrotate -fv /etc/logrotate.d/apache2 # force, run nowAfter the forced run, verify Apache reopened its descriptors:
sudo lsof -p $(pgrep -f 'apache2 -k' | head -1) | grep '\.log'The daemon should hold the freshly-created log files, not the rotated
.1ones. -
For rotatelogs (the high-write-rate case). Replace the
CustomLogandErrorLoglines in your vhost with piped versions:ErrorLog "|/usr/bin/rotatelogs -l /var/log/apache2/%Y-%m-%d/error.log 86400" CustomLog "|/usr/bin/rotatelogs -l /var/log/apache2/%Y-%m-%d/access.log 86400" combinedThe
-lflag uses local time (not UTC) for the timestamp.86400is the rotation interval in seconds (24 hours). For size-based rotation, use5Mfor “rotate when the file hits 5 MB.”Restart Apache (not reload — adding piped logs requires the master to relaunch the child):
sudo apache2ctl configtest sudo systemctl restart apache2Verify the child process is running:
pgrep -fa rotatelogs -
Add monitoring for both cases. A simple cron line catches the silent-failure case:
# Alert if any apache2 process has an old log file open */15 * * * * root /usr/bin/lsof -p $(pgrep -f 'apache2 -k' | head -1) 2>/dev/null \ | awk '/access.log.[0-9]/ { print "STALE: " $9 }' \ | mail -s "Apache stale log fd" ops@example.com
Common pitfalls
logrotatewithcopytruncateand a high-write-rate vhost loses log entries during the copy-then-truncate window. The volume depends on your write rate; for billing/audit logs, never usecopytruncate.logrotateruns from cron on most distros — if cron is broken, rotation stops silently. The disk fills three weeks later. Monitorcronhealth, not justapache2health.- A vhost-specific
CustomLogwriting to/var/log/apache2/sites/example.com/access.logmust have the directory pre-created.logrotate’screatedirective creates files, not directories. apache2ctl graceful(reopen logs and roll workers) andapache2 reloadare not the same on all distros. Somesystemctl reload apache2units rungraceful; others run a less-graceful reopen. Read the unit file before assuming.rotatelogschild processes are orphans of the master Apache. Asystemctl stop apache2should kill them, but a botched stop can leaverotatelogsprocesses holding log files.pgrep -fa rotatelogsafter a stop catches this.- Some Apache packages on RHEL-family distros do not include
rotatelogsin the default install — it ships inhttpd-toolsseparately. Checkwhich rotatelogsbefore committing to the piped-log approach.
Stack Harbor manages Apache fleets where log rotation is config-managed, the choice of rotatelogs vs logrotate is documented per site, and lsof monitoring catches stale-descriptor failures within minutes. If your last disk-full alert was traced to “Apache was writing to a rotated log file for three weeks,” the audit and the monitoring rollout are part of the standard work under Managed Operations.