Linux - PHP FPM Loads very slow

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.

How to Track?

So my deduction is "it is because of the PHP", then how to track this?

  1. Once we restart the php-fpm, DON'T reload the page yet!

  2. 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
  3. 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/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.


Did you find this article valuable?

Support Fiko Borizqy by becoming a sponsor. Any amount is appreciated!