Linux - PHP FPM Loads very slow

I wrote these tutorials for myself in future when I forget for the next steps.
Search for a command to run...

I wrote these tutorials for myself in future when I forget for the next steps.
No comments yet. Be the first to comment.
When you provision an Ubuntu instance on Oracle Cloud, you'll notice something different from standard Ubuntu installations: the system uses iptables instead of ufw for firewall management. This disti

When developing web applications locally, HTTPS is often overlooked. Yet, modern browsers and APIs increasingly require secure connections—even in development. That’s where mkcert comes in: a simple t

Testing the checkout success page in Magento 2 can be frustrating. By default, once an order is placed, Magento clears the quote session, meaning you can’t simply refresh or revisit the success page without going through the entire checkout process a...

Magento Commerce (EE) offers powerful features, but for many teams, Open Source (CE) is leaner, lighter, and more sustainable. If you're migrating from EE to CE, the Opengento downgrade tool provides a clean, scriptable way to remove proprietary modu...

If you’ve ever checked your Apache error logs and seen something like this: Code [core:error] (13)Permission denied: access to /robots.txt denied (filesystem path '/home/username/sites') because search permissions are missing on a component of the pa...

It's an intermittent issue I faced on some of project I have and those projects are running on PHP. Sometimes they load faster and sometimes they load the pages very very slow.
It can be caused by several issues, but most of the times I face this issue, it was because of the PHP configurations I have. How did I know? What did I do?
When I face the page that is really slow whereas it should be fast enough, I restart my php-fpm: (in this case I use PHP 8.1, if you're using other version, then just change the version according to your environment)
sudo service php8.1-fpm restart
And try reload the page, it run faster and it should be that way.
So my deduction is "it is because of the PHP", then how to track this?
Once we restart the php-fpm, DON'T reload the page yet!
Try to monitor php-fpm logger: (first time you hit enter/return, you'll just see nothing, and it's okay, leave it)
sudo tail -n 0 -f /var/log/php8.1-fpm.log
Then try to refresh and refresh the pages, you'll see the realtime logs, and this is mine:

As you can see on my logs, I have an issue with my max_children. I did bit research on the internet and by one of the pages I found, I got this statement "A far better way to run PHP-FPM pools, but badly documented, would be the 'ondemand' Process Manager." by Craigue Hyland.
And by other sources I found, I decided to use this configuration on my PHP configuration: (I'm using Ubuntu 22.04, so my PHP configuration is here /etc/php/8.1/fpm/pool.d/www.conf)
pm = ondemand
pm.max_children = 15
pm.process_idle_timeout = 10s
pm.max_requests = 200
I restart php-fpm service, then refreshing the pages over and over. and Everything is fine now. The pages are running smoothly.
References:
https://community.webcore.cloud/tutorials/php_fpm_ondemand_process_manager_vs_dynamic/
https://serverfault.com/questions/998677/php-fpm-creating-too-many-proccesses-eating-all-the-cpu
https://serverfault.com/questions/472656/adjusting-php-fpm-configuration?rq=1
https://serverfault.com/questions/1079155/php-scripts-suddenly-load-very-slow-on-apache
https://wordpress.org/support/topic/php-script-using-too-much-memory/