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.
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.
I always use
dynamic for busy sites, as it is adaptable and safe for
RAM.
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
Focus primarily on
pm.max_children. It's the main value to tune.
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.
I did this little thing one time and it saved my site during a
massive traffic increase. The site stayed fast and my server didn’t run out of memory; how cool is that?
Additional Settings to Improve Performance
request_terminate_timeout
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.
Mistakes to Avoid
- Setting max_children too high
- Using the default settings
- Forgetting to check your RAM
- Using static mode with a small VPS
These mistakes can crash your server when overloaded with visitors.