Apache mod_status module: Monitor web server and current connections

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

Apache is one of the most popular web servers used to serve websites on the Internet today. Monitoring the performance and health of an Apache server is crucial for maintaining the quality of service and uptime. The Apache mod_status module is an invaluable tool for monitoring the Apache web server’s performance in real-time. This tutorial will guide you through setting up the mod_status module, understanding its output, and utilizing it to monitor your web server and current connections.

Enabling mod_status

To begin using mod_status, you must ensure it is enabled in your Apache configuration. This is typically done in your httpd.conf or apache2.conf file with the following lines:

LoadModule status_module modules/mod_status.so
<IfModule mod_status.c>
    ExtendedStatus On
    <Location "/server-status">
        SetHandler server-status
        Require host localhost
    </Location>
</IfModule>

Make sure to restart your Apache web server after modifying the configuration for the changes to take effect.

Note: In certain distributions like Ubuntu, you might need to enable the module with a2enmod status command and then restart the server.

Understanding mod_status Output

The mod_status module provides a webpage accessible at http://your.server.address/server-status by default, displaying various metrics about the server’s performance. Here is an abbreviation of what you might see:

Server Version: Apache/2.4.29 (Ubuntu)...
Server MPM: prefork
Current Time: Wednesday, 05-Apr-2023 15:15:31 UTC
Restart Time: Monday, 03-Apr-2023 11:11:11 UTC
Parent Server Config. Generation: 3
Parent Server MPM Generation: 2
Server uptime: 2 days 4 hours 4 minutes 20 seconds
Server load: 0.55 0.51 0.48
...

This screen displays information about the server version, the Multi-Processing Module (MPM) in use, server uptime, and the current server load, among other things.

Access Control

It’s important to control who can access the /server-status page. For better security, it’s recommended to restrict access to local connections or to specific IP addresses. Here’s an example of how to restrict access to the server-status page to the localhost:

<Location "/server-status">
    SetHandler server-status
    Require local
</Location>

If you want to allow access from a specific IP, you can use Require ip your.ip.address.here instead of Require local.

Configuring mod_status for Extended Information

The ExtendedStatus directive allows you to collect more detailed information about each request being processed. With this directive enabled, you’ll see a list of connections with information on the client IP, request, and more.

Example: Enabling ExtendedStatus can be done in the httpd.conf file alongside the initial setup:

ExtendedStatus On

After enabling this feature, the server status page will show more detailed information such as:

Srv PID Acc M CPU SS Req Conn Child Slot Client VHost Request
0-0 12345 0/8/8 _ 0.17 14 0 0.0 0.03 0.20 127.0.0.1 localhost GET /server-status HTTP/1.1
...

Automating Monitoring with cron and mod_status

For automated monitoring, you can use a cron job to regularly check the server status and log the results. Here’s an example script that logs the server load every minute:

* * * * * curl -s http://localhost/server-status?auto | grep 'Load' >> /var/log/apache2/server_load.log

This line in a crontab file will instruct the server to record the server load to a log file every minute.

Advanced Localization and Customization

If you have multiple virtual hosts, you may want to isolate the statistics for each one. Here’s a configuration snippet for isolating stats in a virtual host:

<VirtualHost *:80>
    ...
    <Location "/server-status">
        SetHandler server-status
        Require local
    </Location>
</VirtualHost>

Custom styling and configuration logic can be achieved using mod_rewrite and server-side scripts to render the status page differently.

Conclusion

This tutorial has covered the setup and use of Apache’s mod_status module to monitor your web server. Following these steps and examples, you can obtain valuable insights into how your web server is performing and handling connections. Observing and analyzing your server’s status can help you make informed decisions to keep your website running smoothly and reliably.