Category: PHP

Monitoring PHP and Performance Tuning

The next part of the series is, Monitoring PHP and Performance Tuning

PHP is a scripting language, which is interpreted by PHP interpreter.

You may be running a PHP file either directly using PHP Interpreter [Command line]

Or By A mediator, who is telling PHP Interpreter to execute the code. This means in the case of apahce HTTP server,

  • running PHP as module
  • running PHP as cgi/fastcgi
  • running PHP as fpm

You can only run one version of PHP as a module. If you are running multiple version of PHP, then you can achieve that by running PHP as FPM [Fast process manager]

Monitoring PHP running as FPM

  • FPM is a process manager, which runs PHP process in the background.
  • Same as the apache process and threads, it also manages threads whose numbers can be configured.
  • all configuration related values are defined in www.conf file, generally located in /etc/php-fpm.d/www.conf

How to get the status ?

to get the status you need to modify www.conf

pm.status_path = /status

and add the code in one of the virtual host or apache conf file

<LocationMatch "/status">
    SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost/status"

try to access it from a public URL or command line through curl

WARNING: - These must NOT be publicly accessible as this is a security risk.

You will get the information like this.

pidthe PID of the process
statethe state of the process (Idle, Running, …)
start timethe date and time the process has started
start sincethe number of seconds since the process has started
requeststhe number of requests the process has served
request durationthe duration in µs [micro seconds] of the requests
request methodthe request method (GET, POST, …)
request URIthe request URI with the query string
content lengththe content length of the request (only with POST)
userthe user (PHP_AUTH_USER) (or ‘-‘ if not set)
scriptthe main script called (or ‘-‘ if not set)
last request cputhe %cpu the last request consumed. it’s always 0 if the process is not in Idle state because CPU calculation is done when the request processing has terminated
last request memorythe max amount of memory the last request consumed. it’s always 0 if the process is not in Idle state because memory calculation is done when the request processing has terminated

How to enable slow logs ?

You can also enable access logs and slow logs. [slow logs mean script taking more than the desired time to execute]. This value can also be set

access.log = log/$pool.access.log
access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
slowlog = /var/log/php-fpm/www-slow.log
request_slowlog_timeout = 20s ; script taking more than 20 seconds

As you can monitor this, now we come to the part where we can do the optimization.


Optimizing php-fpm configuration

pm.max_children = 80
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500

The above value defines the number of processes running PHP scripts. This can be modified on basis of your hardware resources availbility.

REMEMBER : PHP page is handled by FPM , apart from that, any static and HTML page, or the files does not have .PHP [defined extension to handle] can be directly server by webserver.

So setting Apache conifguration is differnet compare to PHP-FPM configuration

for more php-fpm conifguration

Apart from this,

Caching Compiled Byte Code

PHP performance can be improved by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

you can monitor the cache usage by a GUI

The following article provides a detailed description among different available PHP accelerator

Read Next

Monitoring Apache Logs using Zabbix

Apart from monitoring server hardware and software key variable like CPU/Memory/Disk/Process, We also require monitoring of apache logs using Zabbix to monitor all from a…
Read More

Complete Guide to Debug LOAD Issue in LAMP Stack [Linux/Apache/Mysql/PHP]

Problem Statement

There is php, apache , mysql based application, the stack is hosted on Linux.

This is a single server application, where PHP and apache are on an ubuntu-based Linux server and MySQL is on another server.

let’s suppose the application becomes unresponsive after a certain load. Now you want to RCA and optimize the bottleneck of the problem, the following will provide you a complete guide to debug LOAD issue in LAMP stack

LAMP Stack Component

To figure out the load issue, first, we need to figure out the dependent items of the stack, from where the problem may arise. We divide this into two parts, hardware, and software


  1. Linux server hosting apache2
  2. Linux server hosting MySQL


  1. Apache/Nginx webserver
  2. PHP processing [running as module/fpm/fastcgi]
  3. Mysql server

Root Cause Analysis [RCA]

Flow Of Analysis

Considering the situation where the site is becoming unresponsive,

the very first thing that is coming to consideration is, Hardware limitation

Monitor Hardware Component

Q1:- are the resources enough to handle so many requests?

To get the answer to this question, we analyze the system state. considering that we are on Linux OS [ubuntu], however, most *nix are same.

check for web server machine

  • If possible, put monitoring on CPU utilization. You can use htop/top command to check the process and load
  • Put monitoring on ram utilization. htop/top command can be used to check the log
  • In case of the machine become unresponsive, check for System logs [syslogs], mostly under var/log/syslog location
    • tail -f /var/log/syslog
  • Check the stale process using
    • strace or pmap <pid>
    • ls /proc/<pid>
    • cat /proc/<pid>/status
    • cat /proc/cpuinfo
    • cat /proc/meminfo
    • cat /proc/zoneinfo
    • cat /proc/mounts

htop/top is only helpful if you monitor during load as they shows current state of the system otherwise put the monitoring by using any tool.

There are various tool like cloud instances also provide resource monitoring as Cloudwatch in AWS, or, one can setup open source tool like nagios, zabbix, opennms or there are handy utilitites like, glance, atop, etc

There are 2 main factors to analyze

Apart from these 2, one can also check for another two factors. As most of the cloud systems provide a good bandwidth allocation and that can also be checked from Network monitoring and Most systems are equipped with SSD, so these are not the major factory but can play a vital role if your application highly depends on that, like you are using firebase data to read and write which need higher I/O or delivering large media file

check for the database server machine

If the database server is deployed on another machine, then, we need to follow the same steps as we covered in the earlier section to figure if the database server is enough or not.

In case, you are using AWS RDS service for the database, you can use AWS cloudwatch or RDS monitoring tool with CPU matrix or connection matrix to figure out the root cause.

Most of the cloud provider provides these type of system/instance/machine monitoring tool, where we check the hardware component dependency and bottleneck areas.

Now there may be two situations,

  1. Either server resources are NOT enough,
  2. Or server resources are available even at peak load.

Server resources are NOT enough


* Scale Up / Scale Out the resources [Vertical Scaling / Horizontal Scaling]

* Validate the request are legtimate, means there is no bocus request flooding your server to keep busy

If hardware is the bottleneck, we need to upgrade the resource either by following Vertical Scaling or Horizontal Scaling

Server resources are enough

If we are at this point, where resources are available like, servers are healthy and under the threshold limit, it means that hardware is not the bottleneck, we should check for the software level bottleneck.

Monitor Software Component

Q2: if the server resources are enough, then what might be the root cause of the unresponsive web app?

In a typical PHP website, the software stack includes.

Monitoring Apache Logs using Zabbix

Apart from monitoring server hardware and software key variable like CPU/Memory/Disk/Process, We also require monitoring of apache logs using Zabbix to monitor all from a…
Read More