Monitor Apache Web Server Load & Page Stats
In this article
To monitor Apache web server load and page statistics, you use a combination of built-in tools like mod_status and mod_log_config, plus external utilities like top, apachetop, and log analyzers such as GoAccess or AWStats. These tools provide real-time and historical data on server resource usage, active connections, requests per second, and per-page hit counts.
Why Monitoring Matters
Apache is the most widely used web server software, powering a significant portion of all websites. Monitoring its load and page statistics is essential for several reasons:
- Performance tuning: Identifying bottlenecks like high CPU usage, memory exhaustion, or slow response times allows you to adjust configuration parameters such as
MaxRequestWorkersorKeepAliveTimeout. - Capacity planning: Understanding traffic patterns helps you predict when you'll need to upgrade hardware or move to a load-balanced setup.
- Security: Unusual spikes in requests to specific pages can indicate an attack (e.g., a DDoS or a brute-force attempt on a login page).
- User experience: Tracking page load times and error rates helps you maintain a fast, reliable site.
Key Metrics to Track
When monitoring Apache, you'll focus on two broad categories: server load and page statistics.
Server Load Metrics
- CPU usage: The percentage of processor time used by Apache processes. High sustained CPU can indicate inefficient scripts or too many concurrent requests.
- Memory (RSS): Resident Set Size per Apache process. Each process consumes memory; if total memory exceeds available RAM, the system starts swapping, which degrades performance.
- Load average: The system-wide load over 1, 5, and 15 minutes. A load average above the number of CPU cores suggests the server is overloaded.
- Active connections: The number of open TCP connections, including those in
keep-alivestate. - Requests per second: The rate at which Apache is serving requests. This is a direct measure of throughput.
- Busy workers: The number of Apache worker threads/processes currently handling requests. If this reaches the
MaxRequestWorkerslimit, new requests queue up.
Page Statistics
- Page views: The number of times a specific URL is requested.
- Unique visitors: Distinct IP addresses (or cookies, if you use analytics) that accessed your site.
- Bandwidth per page: The total bytes transferred for each page.
- Status codes: The distribution of HTTP response codes (200, 404, 500, etc.). A rising 404 count may indicate broken links.
- Referrer and user-agent: Where visitors come from and what browsers they use, useful for marketing and compatibility testing.
Using mod_status for Real-Time Load
Apache's built-in mod_status module provides a real-time snapshot of server activity. To enable it, you need to edit your Apache configuration (e.g., httpd.conf or a file in conf.d/).
- Load the module:
LoadModule status_module modules/mod_status.so(in most distributions, this is already enabled). - Add a
<Location /server-status>block to define a URL that will display the status page. Restrict access to your IP or a trusted network. - Set
ExtendedStatus Onto get detailed per-request information (CPU time, bytes served, etc.). - Restart Apache:
sudo systemctl restart apache2(on Debian/Ubuntu) orsudo systemctl restart httpd(on RHEL/CentOS).
You can then access http://your-server/server-status in a browser. The page shows a scoreboard with letters representing each worker's state (e.g., . for idle, W for sending reply, R for reading request). It also lists total accesses, total traffic, and the current number of requests per second.
For automated monitoring, you can fetch this page with a script and parse the values. Tools like apache2-utils include apachebench (ab) for load testing, but for continuous monitoring, you might use a cron job that saves the output to a file and graphs it with tools like Munin or Cacti.
Analyzing Access Logs for Page Statistics
Apache logs every request to an access log (typically /var/log/apache2/access.log or /var/log/httpd/access_log). The default log format (Common Log Format) includes the client IP, timestamp, request line, status code, and bytes sent. You can analyze these logs with command-line tools or dedicated log analyzers.
Command-Line Analysis
For quick checks, you can use standard Unix utilities:
tail -f /var/log/apache2/access.log– watch requests in real time.awk '{print $7}' /var/log/apache2/access.log | sort | uniq -c | sort -rn | head -20– list the top 20 most requested URLs.awk '{print $9}' /var/log/apache2/access.log | sort | uniq -c– count status codes.
However, for a more user-friendly view, install apachetop (a top-like tool for Apache logs) or use a web-based analyzer.
Web-Based Log Analyzers
Popular open-source options include:
- GoAccess: A real-time terminal-based analyzer that can also generate HTML reports. It parses the log format automatically and provides interactive dashboards.
- AWStats: A Perl script that generates static HTML reports from logs. It requires a cron job to process logs periodically.
- Webalizer: Similar to AWStats but faster; it produces bar charts and tables.
These tools give you page views, unique visitors, referrers, and more, without needing JavaScript on your site. They are especially useful for static sites or when you want to avoid third-party cookies.
Common Problems and Pitfalls
When monitoring Apache, you may encounter several issues:
- Log rotation: If logs are not rotated, they can grow huge and slow down analysis. Use
logrotateto compress and archive old logs. - Virtual hosts: If you host multiple sites, each may have its own log file. Make sure you monitor all of them or combine them.
- Reverse proxy setups: If Apache sits behind a load balancer or CDN, the client IP in logs will be the proxy's IP, not the real visitor. Use
mod_remoteipto restore the original IP from theX-Forwarded-Forheader. - Misleading load averages: A high load average might be caused by other processes (e.g., MySQL) on the same server, not Apache. Check
topto see which processes consume the most CPU. - KeepAlive effects: Long KeepAlive timeouts can tie up workers, reducing throughput. Monitor the number of idle workers; if it's consistently zero, consider lowering
KeepAliveTimeout.
Choosing Between Built-in and External Tools
The right monitoring approach depends on your needs:
- Quick troubleshooting: Use
mod_statusandtailon logs. - Historical analysis: Use a log analyzer like GoAccess or AWStats.
- Continuous alerting: Use a monitoring system like Nagios, Zabbix, or Prometheus with the Apache exporter. These can alert you when metrics exceed thresholds.
- Application-level insights: For dynamic sites, consider integrating a web analytics tool (e.g., Matomo) that uses JavaScript to track page views, user behavior, and conversions. This complements server logs by providing data on interactions that don't generate HTTP requests (e.g., button clicks).
Remember that server logs only show requests that reach Apache; if you use a CDN or caching plugin, some requests may never hit your server. For a complete picture, combine log analysis with client-side analytics.
Frequently Asked Questions
What is a good load average for an Apache server?
A load average below the number of CPU cores is generally acceptable. For example, on a 4-core server, a load of 3.5 is fine, but a load of 6 indicates overload. However, load average includes all processes, so isolate Apache's CPU usage with top or ps.
How do I see the number of Apache connections in real time?
Use mod_status and access /server-status; it shows the current number of connections and their states. Alternatively, use ss -s to see TCP connection statistics, or netstat -an | grep :80 | wc -l to count connections to port 80.
Why does my Apache log show a 404 for a page that exists?
This often happens when the request is for a URL that includes a trailing slash or a different case, or when a rewrite rule interferes. Check the exact request line in the log and compare it with your DocumentRoot and rewrite rules.
Can I monitor Apache without installing additional software?
Yes, mod_status is built into Apache, and you can parse the access log with standard Unix tools like awk and grep. For a graphical view, you can write a script to generate a simple HTML report, but dedicated tools are more convenient.
What is the difference between hits and page views?
A hit is any request to the server, including images, CSS, and JavaScript files. A page view is a request for an HTML document (or a specific URL you define). Page views are a better measure of user engagement; hits inflate the count because each page typically loads multiple assets.
How often should I rotate Apache logs?
It depends on traffic. For high-traffic sites, daily rotation is common; for low-traffic sites, weekly or monthly. The goal is to keep log files manageable (e.g., under 1 GB) and to preserve them for analysis without filling the disk. Use logrotate with a size or time-based policy.