You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

Welcome to our guide on how to Monitor Apache Web Server with Prometheus and Grafana in less than 5 minutes. This setup should work for any version of Apache web server running on any flavor of Linux. We have other Prometheus Monitoring tutorials:

Monitoring Ceph Cluster with Prometheus and Grafana

How to Monitor BIND DNS server with Prometheus and Grafana

Monitoring MySQL / MariaDB with Prometheus in five minutes

If you are following this guide, I expect that you have Prometheus server installed and running, you can refer to our guide for a fresh installation of Prometheus server on Ubuntu or CentOS server.

Install Prometheus Server on CentOS 7 and Ubuntu

Follow these setup steps to have your Apache Web Server metrics stored on Prometheus and visualized using Grafana.

Step 1: Download and Install Apache Prometheus exporter

Install curl utility if not already present in your machine.

--- Ubuntu / Debian ---
$ sudo apt update && sudo apt install curl

--- CentOS / RHEL / Fedora ---
$ sudo dnf -y install curl
$ sudo yum -y install curl

Check the latest release of Apache Prometheus exporter.

curl -s https://api.github.com/repos/Lusitaniae/apache_exporter/releases/latest   | grep browser_download_url   | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -

Extract downloaded archive:

tar xvf apache_exporter-*.linux-amd64.tar.gz
sudo cp apache_exporter-*.linux-amd64/apache_exporter /usr/local/bin
sudo chmod +x /usr/local/bin/apache_exporter

Make sure apache_exporter is executable from your current SHELL:

$ apache_exporter --version
apache_exporter, version  (branch: , revision: )
  build user:
  build date:
  go version:       go1.13.9

Step 2: Create Apache Prometheus exporter systemd service

First, add prometheus user which will run the service:

sudo groupadd --system prometheus
sudo useradd -s /sbin/nologin --system -g prometheus prometheus

Then proceed to create a systemd service unit file:

sudo vim /etc/systemd/system/apache_exporter.service

Add below content:

[Unit]
Description=Prometheus
Documentation=https://github.com/Lusitaniae/apache_exporter
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/bin/apache_exporter \
  --insecure \
  --scrape_uri=http://localhost/server-status/?auto \
  --telemetry.address=0.0.0.0:9117 \
  --telemetry.endpoint=/metrics

SyslogIdentifier=apache_exporter
Restart=always

[Install]
WantedBy=multi-user.target

The service will listen on port 9117, and metrics exposed on /metrics URI. If Apache metrics are not on  http://localhost/server-status/?auto you’ll need to change the URL.

With Init System

For Init system like CentOS 6.x, create an init script under /etc/init.d/

sudo vim /etc/init.d/apache_exporter
sudo vim /etc/init.d/apache_exporter

Add:

#!/bin/bash
# Author: Josphat Mutai, [email protected] , https://github.com/jmutai
# apache_exporter     This shell script takes care of starting and stopping Prometheus apache exporter 
#
# chkconfig: 2345 80 80
# description: Prometheus apache exporter  start script
# processname: apache_exporter
# pidfile: /var/run/apache_exporter.pid

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0
PROGNAME=apache_exporter
PROG=/usr/local/bin/${PROGNAME}
RUNAS=prometheus
LOCKFILE=/var/lock/subsys/${PROGNAME}
PIDFILE=/var/run/${PROGNAME}.pid
LOGFILE=/var/log/${PROGNAME}.log
DAEMON_SYSCONFIG=/etc/sysconfig/${PROGNAME}

# GO CPU core Limit

#GOMAXPROCS=$(grep -c ^processor /proc/cpuinfo)
GOMAXPROCS=1

# Source config

. ${DAEMON_SYSCONFIG}

start() {
    if [[ -f $PIDFILE ]] > /dev/null; then
        echo "apache_exporter  is already running"
        exit 0
    fi

    echo -n "Starting apache_exporter  service…"
    daemonize -u ${USER} -p ${PIDFILE} -l ${LOCKFILE} -a -e ${LOGFILE} -o ${LOGFILE} ${PROG} ${ARGS}
    RETVAL=$?
    echo ""
    return $RETVAL
}

stop() {
    if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
        echo "Service not running"
        return 1
    fi
    echo 'Stopping service…'
    #kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
    killproc -p ${PIDFILE} -d 10 ${PROG}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${LOCKFILE} ${PIDFILE}
    return $RETVAL
}

status() {
    if [ -f "$PIDFILE" ] || kill -0 $(cat "$PIDFILE"); then
      echo "apache exporter  service running..."
      echo "Service PID: `cat $PIDFILE`"
    else
      echo "Service not running"
    fi
     RETVAL=$?
     return $RETVAL
}

# Call function
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 2
esac

Install daemonize package:

sudo yum -y install daemonize

Create Arguments configuration file:

sudo vim /etc/sysconfig/apache_exporter

Add:

ARGS="--insecure --scrape_uri=http://localhost/server-status/?auto --telemetry.address=0.0.0.0:9117 --telemetry.endpoint=/metrics"

Test the script:

$ sudo /etc/init.d/apache_exporter
Usage: /etc/init.d/apache_exporter {start|stop|restart}

Step 3: Start Apache Prometheus exporter and enable service to start on boot

For Systemd:

sudo systemctl daemon-reload
sudo systemctl start apache_exporter.service
sudo systemctl enable apache_exporter.service

For Init systems without Systemd.

sudo /etc/init.d/apache_exporter start
sudo chkconfig apache_exporter on

You can verify service status using:

--- Systemd ---
$ systemctl status apache_exporter.service
● apache_exporter.service - Prometheus
   Loaded: loaded (/etc/systemd/system/apache_exporter.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2020-09-12 18:24:01 UTC; 17s ago
     Docs: https://github.com/Lusitaniae/apache_exporter
 Main PID: 23926 (apache_exporter)
    Tasks: 6 (limit: 2377)
   Memory: 4.4M
   CGroup: /system.slice/apache_exporter.service
           └─23926 /usr/local/bin/apache_exporter --insecure --scrape_uri=http://localhost/server-status/?auto --telemetry.address=0.0.0.0:9117 --telemetry.endpoint

Sep 12 18:24:01 deb10 systemd[1]: Started Prometheus.
Sep 12 18:24:01 deb10 apache_exporter[23926]: time="2020-09-12T18:24:01Z" level=info msg="Starting apache_exporter (version=, branch=, revision=)" source="apache_
Sep 12 18:24:01 deb10 apache_exporter[23926]: time="2020-09-12T18:24:01Z" level=info msg="Build context (go=go1.13.9, user=, date=)" source="apache_exporter.go:36
Sep 12 18:24:01 deb10 apache_exporter[23926]: time="2020-09-12T18:24:01Z" level=info msg="Starting Server: 0.0.0.0:9117" source="apache_exporter.go:362"
Sep 12 18:24:01 deb10 apache_exporter[23926]: time="2020-09-12T18:24:01Z" level=info msg="Collect from: http://localhost/server-status/?auto" source="apache_expor
Sep 12 18:24:01 deb10 apache_exporter[23926]: time="2020-09-12T18:24:01Z" level=info msg="listening and wait for graceful stop" source="apache_exporter.go:367"

--- Init System ---
$ sudo /etc/init.d/apache_exporter status
apache exporter  service running...
Service PID: 1970

Check service status:

$ sudo chkconfig --list | grep apache_exporter
apache_exporter 0:off   1:off   2:on    3:on    4:on    5:on    6:off

Confirm the port is listening for requests.

$ sudo ss -tunelp | grep 9117
tcp    LISTEN     0      128                   :::9117                 :::*      users:(("apache_exporter",1970,6)) ino:1823474168 sk:ffff880341cd7800

Step 4: Add exporter job to Prometheus

Add a job to the Prometheus server for scraping metrics. Edit /etc/prometheus/prometheus.yml

# Apache Servers
  - job_name: apache1
    static_configs:
      - targets: ['10.1.10.15:9117']
        labels:
          alias: server1-apache

  - job_name: apache2
    static_configs:
      - targets: ['10.1.10.16:9117']
        labels:
          alias: server2-apache

Restart prometheus service for scraping to start

sudo systemctl restart prometheus

Test access to port 9117 from Prometheus server

$ telnet 10.1.10.15 9117
Trying 10.1.10.15...
Connected to 10.1.10.15.
Escape character is '^]'.
^]

Step 5: Add Dashboard to Grafana

The final step is to create your own Dashboard for visualizing Apache metrics. For this demo, we’ll use Grafana Dashboards by Ricardo F. The dashboard ID is 3894. You should have Prometheus Data source already added to Grafana, or use the link Add Prometheus data source to add one.

Once the data source has been added, Import Apache Grafana Dashboard by navigating to Dashboard > Import. Use 3894 for Grafana Dashboard ID.

Give it a descriptive name and select Prometheus data source added earlier.

Click “Import” button to start using the dashboard. After a few minutes, the metrics should start showing.

Select a different host to show metrics for using the drop-down menu at the top of the metrics dashboard. In my next Apache monitoring guide, I’ll cover the use of InfluxDB and Grafana to monitor Apache Web server.

Other monitoring articles available in our website are:

Monitor Zimbra Server with Grafana, InfluxDB and Telegraf

How to Monitor Linux Server with Netdata and Grafana

How to Monitor Redis Server with Prometheus and Grafana in 5 minutes

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF