DEV Community

Smit Vaghasiya
Smit Vaghasiya

Posted on

Grafana Cloud Monitoring Setup (Apache + PHP-FPM + Alloy)

Step 1: Install Grafana Alloy on EC2

  • Go to Grafana Cloud → Collector → Configure.
  • Create a new API token.
  • Copy & paste the installation command provided by Grafana into your EC2 instance.
  • After installation, go to:
cd /etc/alloy 
Enter fullscreen mode Exit fullscreen mode
  • Open the configuration file:
sudo nano config.alloy 
Enter fullscreen mode Exit fullscreen mode
  • Paste Alloy configuration: config.alloy (Note: Do not change the creds of Prometheus and Loki and remotecfg.)

Comment your GitHub profile, and I’ll share the config.alloy file.

  • Restart Alloy:
sudo systemctl restart alloy.service 
Enter fullscreen mode Exit fullscreen mode
  • Go to Grafana Cloud and confirm that Alloy is connected.

Step 2: Verify System Metrics in Grafana

  • Go to Grafana Cloud → Fleet Management.
  • Select the EC2 instance (identified by its IP).
  • Use Explore to verify that CPU, memory, disk and other system metrics are being received.

Step 3: Enable Apache mod_status

  • Edit status.conf:
sudo nano /etc/apache2/mods-available/status.conf 
Enter fullscreen mode Exit fullscreen mode
  • Add the following (if not present):
<Location "/server-status"> 
    SetHandler server-status 
    Require local 
</Location> 
Enter fullscreen mode Exit fullscreen mode
  • Restart Apache:
sudo systemctl restart apache2.service 
Enter fullscreen mode Exit fullscreen mode

Step 4: Enable PHP-FPM Status & Ping Pages

4.1 Update www.conf

  • Edit:
sudo nano /etc/php/8.1/fpm/pool.d/www.conf  
Enter fullscreen mode Exit fullscreen mode
  • Change / add the following:
listen.owner = www-data 
listen.group = www-data 
listen.mode = 0660 
ping.path = /ping  
pm.status_path = /status 
listen = /run/php/php8.1-fpm.sock 
Enter fullscreen mode Exit fullscreen mode
  • Restart PHP-FPM:
sudo systemctl restart php8.1-fpm 
Enter fullscreen mode Exit fullscreen mode
  • Check the /run/php/ the file (php8.1-fpm.sock) is created or not.

4.2 Create php-fpm-status.conf

  • Create file:
sudo nano /etc/apache2/conf-available/php-fpm-status.conf 
Enter fullscreen mode Exit fullscreen mode
  • Paste:
<Location "/status"> 
    SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost" 
</Location> 

<Location "/ping"> 
    SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost" 
</Location> 
Enter fullscreen mode Exit fullscreen mode
  • Restart services:
sudo systemctl restart apache2 
sudo systemctl restart php8.1-fpm 
Enter fullscreen mode Exit fullscreen mode

Step 5: Enable Required Apache Modules

sudo a2enmod proxy proxy_fcgi setenvif 
sudo a2enconf php-fpm-status 
sudo systemctl restart apache2 
Enter fullscreen mode Exit fullscreen mode

Step 6: Install PHP-FPM Exporter

6.1 Download & Install Exporter

wget https://github.com/hipages/php-fpm_exporter/releases/download/v2.2.0/php-fpm_exporter_2.2.0_linux_amd64.tar.gz  
tar -xvzf php-fpm_exporter*.tar.gz  
sudo mv php-fpm_exporter /usr/local/bin/ 
Enter fullscreen mode Exit fullscreen mode

6.2 Create Exporter User

sudo useradd --system --no-create-home --shell /usr/sbin/nologin phpfpm_exporter 
sudo usermod -aG www-data phpfpm_exporter 
Enter fullscreen mode Exit fullscreen mode

6.3 Create Systemd Service

sudo nano /etc/systemd/system/php-fpm-exporter.service 
Enter fullscreen mode Exit fullscreen mode
  • Paste:
[Unit] 
Description=PHP-FPM Prometheus Exporter 
After=network.target 

[Service] 
User=phpfpm_exporter 
Group=www-data 
ExecStart=/usr/local/bin/php-fpm_exporter server \ 
  --phpfpm.scrape-uri='unix:///run/php/php8.1-fpm.sock;/status' \ 
  --web.listen-address=':9253' 
Restart=always 
RestartSec=5 

[Install] 
WantedBy=multi-user.target 
Enter fullscreen mode Exit fullscreen mode

6.4 Start the Service

sudo systemctl daemon-reload 
sudo systemctl enable --now php-fpm-exporter 
sudo systemctl status php-fpm-exporter 
Enter fullscreen mode Exit fullscreen mode

6.5 Test Metrics

curl http://127.0.0.1/status 
curl http://127.0.0.1:9253/metrics 
Enter fullscreen mode Exit fullscreen mode
  • Now restart Alloy:
sudo systemctl restart alloy 
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify PHP-FPM Metrics in Grafana

  • Return to Grafana Cloud → Explore.
  • Search for PHP-FPM metrics such as:
1. phpfpm_active_processes 
2. phpfpm_requests_total 
3. phpfpm_slow_requests 
Enter fullscreen mode Exit fullscreen mode
  • Confirm that all Apache, System, PHP-FPM metrics are now visible.

Top comments (0)