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"
</LocationMatch>
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.
<webhost>/status?html&full
You will get the information like this.
pid | the PID of the process |
state | the state of the process (Idle, Running, …) |
start time | the date and time the process has started |
start since | the number of seconds since the process has started |
requests | the number of requests the process has served |
request duration | the duration in µs [micro seconds] of the requests |
request method | the request method (GET, POST, …) |
request URI | the request URI with the query string |
content length | the content length of the request (only with POST) |
user | the user (PHP_AUTH_USER) (or ‘-‘ if not set) |
script | the main script called (or ‘-‘ if not set) |
last request cpu | the %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 memory | the 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.
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
https://www.php.net/manual/en/install.fpm.configuration.php
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.
- Opcache : bundled with PHP > 5.5
you can monitor the cache usage by a GUI
https://github.com/PeeHaa/OpCacheGUI
The following article provides a detailed description among different available PHP accelerator
https://en.wikipedia.org/wiki/List_of_PHP_accelerators
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…
Monitoring LAMP Application using Zabbix
A complete guide for monitoring LAMP [Linux/Apache/Mysql/PHP] application through Zabbix and setting up email notifications….
Monitoring MySql and Optimization
Next part in monitoring and optimization of LAMP stack is, Monitoring MySql and optimization. Monitoring…
Monitoring PHP and Performance Tuning
Monitoring PHP running as FPMHow to get the status ?How to enable slow logs ?OptimizationOptimizing…
2 Comments