Php – Relation between PHP-FPM settings and mpm_worker (Apache)

apache-2.2apache-2.4mpm-workerPHPphp-fpm

I've a site running Magento on the following setup:

  • Apache 2.4 with mpm_worker
  • PHP-FPM
  • Total RAM in server: 14GB (10GB avilable to Apache/PHP)

Each PHP-FPM process consumes approximately 80MB of RAM.

I want to fine tune the settings for PHP-FPM and mpm_worker.
Reading in the documentation I'm a bit confused about the relation between options in PHP-FPM and mpm_worker.

E.g., PHP-FPM has configurations: pm.min_spare_servers, pm.max_spare_servers, pm.start_servers and max_children. I understand what these options mean with respect to PHP-FPM.

However, mpm_worker has the following configurations: MinSpareThreads, MaxSpareThreads, StartServers and ThreadLimit. Again, isolated to mpm_worker I understand what they do.

My question is: How does these configurations relate to each other? Let's say I set StartServers=2 and ThreadsPerChild=25. This would give me 50 threads to begin with with respect to Apache/mpm_worker. If I then set pm.start_servers = 50, PHP-FPM will spawn 50 processes initially.

So what is the relationship between these 50 "Apache" threads and 50 "PHP-FPM" processes? Does each Apache thread utilize one PHP-FPM process?

I hope someone can give some insight into this.

Thanks!

Best Answer

Apache serves the requests from the clients using the threads' childs, but it'll only call PHP-FPM for php files - images, css and js should be served directly from Apache, and thus not consume a PHP-FPM thread.

PHP-FPM has multiple threads with no childs - each thread serves a request from Apache.

AFAIK, multiple requests from the same client (loading the page, plus images, css and js) should consume multiple threads if you don't have KeepAlive set in Apache. See this link for more info on KeepAlive.

I've been tuning an Apache server myself recently and found out that if you have multiple cores it's better to increase the number of childs per thread.