When you receive a high volume of traffic to your website, your server can quickly slow down. I learned this the hard way when one of my sites continued to crash. Have you experienced this? This is how I learned the importance of PHP-FPM pool tuning. Let’s simplify this as much as possible.
This setting determines for PHP-FPM how to create workers.
Ever wonder why some people just like static? They like to have complete control.
I increase this slightly so that longer tasks do not terminate early.
slowlog
I enjoy using slowlog because it tells me what scripts are slow. It makes troubleshooting easy.
Opcache
Be sure to use Opcache. It speeds up PHP and lessens the load. I still wonder why some people do not take advantage of it.
Why PHP-FPM Tuning Is Important
PHP-FPM is similar to a crew of workers processing requests from visitors. If we have too few workers your site will be slow. If you have too many, you will run out of memory for your server. We need to find a middle ground. Easy concept!Some Basic PHP-FPM Settings You Should Know About
Process Manager (pm)This setting determines for PHP-FPM how to create workers.
- dynamic = the number of workers will increase or decrease as traffic changes.
- static = the number of workers will always stay the same.
- ondemand = workers will only be created when they are needed.
Ever wonder why some people just like static? They like to have complete control.
Key Values to Configure
These three values are the only ones that matter:- pm.max_children – how many workers you will allow
- pm.start_servers – how many workers you will come up with initially
- pm.max_spare_servers – how many workers can be idle
How to Easily Calculate pm.max_children
I use this little trick:- See how much RAM a single PHP worker takes.
- See how much total RAM you want to assign to PHP
- Divide total RAM by RAM per worker
- The result equals your pm.max_children value.
Additional Settings to Improve Performance
request_terminate_timeoutI increase this slightly so that longer tasks do not terminate early.
slowlog
I enjoy using slowlog because it tells me what scripts are slow. It makes troubleshooting easy.
Opcache
Be sure to use Opcache. It speeds up PHP and lessens the load. I still wonder why some people do not take advantage of it.
Mistakes to Avoid
- Setting max_children too high
- Using the default settings
- Forgetting to check your RAM
- Using static mode with a small VPS