How to use PHP FPM exporter

While deploying one of the applications I'm developing to the server, I encountered very weird behavior of the php-fpm process. Reproducing that behavior could take hours, so using the "stupid try-and-wait" method would take too much time to fix. The first thing I did was install monitoring because that allows you to follow what is happening on the machine while you sleep.

I'm in love with Prometheus and Grafana, so it was pretty obvious that those tools would be my choice.

If you're reading this guide, you’re most likely having problems setting things up, but I’ll assume that you already have Prometheus installed.

Setup

Let’s get started. I’ll assume you’ve already SSHed into the server/container you want to monitor. I’ll use the PHP FPM Exporter from this repository.

Download the exporter and move it to /usr/local/bin (as it is a normal non-distribution package-managed package) using this command:

wget https://github.com/hipages/php-fpm_exporter/releases/download/v2.2.0/php-fpm_exporter_2.2.0_linux_amd64
mv php-fpm_exporter_* /usr/local/bin/php-fpm_exporter
chmod 755 /usr/local/bin/php-fpm_exporter

Config

This part gave me some trouble (my excuse is that it was late), which motivated me to write this guide the following morning after the setup.

  1. Open your PHP-FPM config (replace 8.3 with your PHP version):
    vi /etc/php/8.3/fpm/pool.d/www.conf
    
  2. Make the following changes:
    1. Uncomment the listen mode:
      listen.mode = 0660
      
    2. Uncomment the status URI:
      pm.status_path = /status
      
    3. Ensure the socket configuration points to the correct path (replace 8.3 with your PHP version if necessary):
      listen = /run/php/php8.3-fpm.sock
      
  3. Save the changes and exit the editor.
  4. Restart the PHP-FPM service to apply the configuration:
    systemctl restart php8.3-fpm
    
    

Now your PHP-FPM instance is configured to expose metrics through the /status path on the defined socket.

Service

Next, create a service for the PHP-FPM exporter so it can run in the background and start automatically on boot.

Open a new service file for the exporter:

vi /etc/systemd/system/php-fpm_exporter.service

Add the following content to the file:

# Service file for PHP-FPM Exporter:
[Unit]
Description=PHP-FPM Exporter Service
After=network.target

[Service]
User=www-data
Group=www-data
Type=simple
ExecStart=/usr/local/bin/php-fpm_exporter server --phpfpm.fix-process-count=true --phpfpm.scrape-uri "unix:///run/php/php8.3-fpm.sock;/status"

[Install]
WantedBy=multi-user.target

Start and Enable the Service

Reload the systemd daemon to ensure it picks up the new service configuration:

sudo systemctl daemon-reload

Enable the PHP-FPM exporter service to start automatically on boot

sudo systemctl enable php-fpm_exporter

Start the PHP-FPM exporter service:

sudo systemctl start php-fpm_exporter

Check the service status to ensure it is running properly:

sudo systemctl status php-fpm_exporter

If everything is set up correctly, you should see the service listed as active (running). If there are any issues, you can check the logs with:

journalctl -u php-fpm_exporter -f

Collect Metrics

Now that the PHP-FPM exporter is running, it’s time to configure Prometheus to scrape the metrics.

  1. Open your Prometheus configuration file (typically prometheus.yml).
  2. Add the following job configuration:
    - job_name: 'php-fpm-exporter'
      scrape_interval: 15s  # Adjust based on your needs
      static_configs:
        - targets: ['ip_of_your_server:9253']
    
  3. Save the changes and restart Prometheus:
    sudo systemctl restart prometheus
    
  4. Verify that Prometheus is scraping the metrics by visiting the targets page: http://<your-prometheus-server>:9090/targets. You should see the php-fpm-exporter target listed as up. If not, ensure that the exporter is running and that there are no firewall rules blocking the connection.

Verify Setup in Grafana

Once Prometheus is successfully scraping metrics, you can create visualizations in Grafana.

Add Prometheus as a Data Source

  1. Log in to your Grafana instance.
  2. Go to SettingsData Sources.
  3. Click Add Data Source and select Prometheus.
  4. Provide the Prometheus URL (e.g., http://<your-prometheus-server>:9090).
  5. Click Save & Test to confirm the connection.

If everything is working, you should see a message indicating the data source was added successfully.

Happy monitoring