aaP_siru.khan
To check which program or script is consuming high resources in PHP-FPM, follow these methods:
1. Check PHP-FPM Process Usage with top
or htop
Run the following command to see the CPU and memory usage of PHP-FPM processes:
top -u www-data
or (if htop
is installed):
htop
Look for processes running under the PHP-FPM user (usually www-data
, php-fpm
, or nginx
).
2. Find the Heavy PHP-FPM Worker with ps
Use ps
to check the most resource-consuming PHP-FPM processes:
ps aux --sort=-%cpu | grep php-fpm
or
ps aux --sort=-%mem | grep php-fpm
This will list PHP-FPM processes sorted by CPU or memory usage.
3. Enable PHP-FPM Slow Log to Identify Slow Scripts
If a specific PHP script is consuming resources, enable the slow log:
Open your PHP-FPM pool configuration:
sudo nano /etc/php/{your_php_version}/fpm/pool.d/www.conf
Example: for PHP 8.1:
sudo nano /etc/php/8.1/fpm/pool.d/www.conf
Find and set the following values:
slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 5s
Restart PHP-FPM:
sudo systemctl restart php-fpm
View the slow log:
cat /var/log/php-fpm/slow.log
This will show which scripts take longer than 5 seconds to execute.
4. Find the Specific PHP Script Using lsof
To find which script is being executed by a PHP-FPM process, first get the process ID (PID) from top
or htop
, then run:
lsof -p <PID>
This will show which files the process is accessing, including the PHP script.
5. Monitor PHP-FPM Requests in Real-Time
Use strace
to attach to a PHP-FPM worker and see what it's doing:
strace -p <PHP-FPM-PID>
This will show system calls made by the process, helping you identify what it's working on.
6. Analyze Logs for High Usage
Check the PHP-FPM access log to see which scripts are being executed frequently:
tail -f /var/log/php-fpm.log
or, if using Nginx:
tail -f /var/log/nginx/access.log
7. Use ngrep
to Capture Real-Time Requests
If you suspect a specific request is causing high PHP-FPM load, use ngrep
:
sudo ngrep -d any -W byline port 80 or port 443
This will capture incoming HTTP requests and help you identify problematic URLs.
Conclusion
- Use
top
/htop
to identify high-resource PHP-FPM processes.
- Use
ps
to sort processes by CPU/memory usage.
- Enable PHP-FPM slow log to detect slow scripts.
- Use
lsof
and strace
to inspect what a PHP-FPM process is doing.
- Monitor logs (
php-fpm.log
, nginx/access.log
) for frequently executed scripts.