If you’re running web services on a Linux server, monitoring Apache’s logs is essential for troubleshooting and performance tuning. Apache, by default, generates two primary log files: the access log and the error log. In this article, I’ll show you my favorite command-line ways to view, tail, search, and archive Apache logs, all from your terminal.
Locating Apache Logs
On most distributions, you’ll find Apache’s logs in /var/log/apache2/
(Debian/Ubuntu) or /var/log/httpd/
(RHEL/CentOS/Fedora):
ls -l /var/log/apache2/
ls -l /var/log/httpd/
Look for access.log
and error.log
files.
Viewing Logs in Real Time
To view logs as traffic hits your site, use tail -f
:
tail -f /var/log/apache2/access.log
Need to watch both access and error logs together? Use multitail
if installed, or run two terminals side-by-side:
multitail /var/log/apache2/access.log /var/log/apache2/error.log
Searching Logs with grep
Suppose you want to find all requests from a specific IP:
grep '192.168.1.10' /var/log/apache2/access.log
Or to filter error messages related to a specific file:
grep '404' /var/log/apache2/error.log
Combine with less
for easier browsing:
grep 'mod_ssl' /var/log/apache2/error.log | less
Archiving and Rotating Logs
Large logfiles slow you down and can fill up your disk. Apache usually relies on logrotate (via /etc/logrotate.d/apache2
or /etc/logrotate.d/httpd
). You can force a manual rotation with:
sudo logrotate -f /etc/logrotate.conf
To archive logs before rotating:
sudo cp /var/log/apache2/access.log ~/apache-access-$(date +%F).log
Summary
Efficient command-line log management saves time during troubleshooting and keeps your server humming. By mastering tail
, grep
, and log rotation, you’ll keep a close watch on your Apache-powered sites.
Happy logging!
— Lenny
Leave a Reply