Calculating initial PHP-FPM settings
Typically PHP-FPM takes 50-100MB per process, but it very much depends on your websites traffic load and php.ini values. The settings in the form below are set for a Server with 4GB of memory, reserving 1.5GB of that for the Operating system and other processes such as MySQL, and then let PHP-FPM use the remainder of the memory.
Also be aware that if you tune PHP-FPM and then enable PHP OPcache, the amount of memory consumed per PHP-FPM process will be different, so tuning should be re-performed.
NOTE: To determine the current memory use of php-fpm use something like the following ensure your php-fpm{x.x} matches your configuration, in this example 8.1.
ps --no-headers -o rss -C php-fpm8.1 | awk '{ sum+=$1 } END { print sum/NR/1024 " MB" }'
Or worst case use just grep for call php-fpm processes
ps aux | grep -i php-fpm
The following assumes you have only one php-fpm pool running on the server if you have multiple, you’ll need to proportionally split the config amongst the php-fpm pools.
PHP-FPM settings calculator
Adjust the input numbers to get initial values for your server setup.
pm.max_children:
pm.start_servers:
pm.min_spare_servers:
pm.max_spare_servers:
These settings are a starting point to optimise your servers PHP-FPM setting, they are NOT definitive, your server will have its own unique load which you should adjust these settings for.
The Request Cycle Summary with NGINX and PHP-FPM
Nginx and PHP-FPM work together to efficiently serve dynamic PHP content. Here’s a breakdown of how a typical request cycle works when a user accesses a PHP-based website hosted on Nginx with PHP-FPM.
[Client Browser] --> [Nginx] --> [PHP-FPM] --> [PHP Script Execution]
↑ | | |
| ↓ ↓ |
[HTML Response] <-- [Nginx] <-- [PHP-FPM] <-- [PHP Output] <--
- Client (browser) requests a PHP page
- Nginx determines the request is for dynamic content and forwards it to PHP-FPM
- PHP-FPM receives the request and assigns a child process to execute the PHP script
- PHP Script is executed, and the output (HTML) is generated
- PHP-FPM returns the output to Nginx
- Nginx sends the response to the client (browser)
Understanding PHP-FPM Tuning
Proper tuning of PHP-FPM settings can optimize resource usage and improve website responsiveness.
- pm.max_children: Maximum number of child processes. Set based on available memory and process usage.
- pm.start_servers: Number of child processes created on startup. Typically a fraction of max_children.
- pm.min_spare_servers: Minimum idle processes to maintain. Helps handle traffic spikes efficiently.
- pm.max_spare_servers: Maximum idle processes to avoid overloading the server.
Effective tuning involves balancing memory usage and CPU resources while maintaining fast response times. Regular monitoring and adjustments are recommended for optimal performance.
What is PHP OPcache?
OPcache is a built-in PHP extension that caches the compiled bytecode of PHP scripts. This eliminates the need for PHP to parse and compile scripts on each request, significantly improving performance.