Today I Learned - Rocky Kev

TIL php-fpm

POSTED ON:

TAGS:

In Javascript, when you want to branch off your code to a new thread (even though JS is single-threaded by design), you would use a web worker.

I learned that PHP has something similar! There's a folder I keep stumbling in called php-fpm, and I thought it's time to figure out what that is.

PHP-FPM (FastCGI Process Manager) is the preferred method of processing PHP pages with NGINX and is faster than traditional CGI based methods such as SUPHP or mod_php for running a PHP script. The main advantage of using PHP-FPM is that it uses a considerable amount of less memory and CPU as compared with any other methods of running PHP.

via https://www.digitalocean.com/community/tutorials/php-fpm-nginx

Essentially FPM manages a set of worker nodes which process requests as they come in. Although you can multithread (with Swoole, AmPHP), most applications won't get a huge amount of benefit from doing so. Because for web applications, the main bottleneck for performance isn't processing power, it's IO limits.

PHP is usually ran with PHP-FPM which will request a PHP worker per request. There will be a minimum amount of workers always ready to handle a request and it can scale up/down as needed. So each request will get its own worker and this allows PHP webservers (using nginx/apache/caddy) to send each request to its own worker on its own thread. This can effectively use all cores and threads.

via https://www.reddit.com/r/PHP/comments/xpeyk3/where_does_php_being_single_threaded_affect/

With Nginx:

Nginx, as a stable high-performance web server and with a very low consumption of resources, is the perfect match for PHP-FPM. Nginx has an asynchronous architecture that is much more scalable, based on events. Moreover, when using Nginx with PHP-FPM, performance at the level of memory consumption is improved.

PHP runs as a separated service when using PHP-FPM. By using this PHP version as language interpreter, requests are processed through a TCP/IP socket; so that the Nginx web server only handles the HTTP requests and PHP-FPM interprets the PHP code. The fact of having two separate services is key for increasing efficiency.

via https://www.stackscale.com/blog/php-fpm-high-traffic-websites/

What about Apache:

If PHP has to do a lot of work Apache will actually be faster because mod_php is a part of the Apache itself and is really good integrated.
The additional (f)cgi takes some time too when using Nginx and thus making it slower on PHP-heavy applications.

via https://stackoverflow.com/a/43187456/4096078

More ref:
https://www.php.net/manual/en/install.fpm.php
https://www.plesk.com/blog/various/why-do-you-need-php-fpm/


Related TILs

Tagged:

TIL how NGINX knows to look for index.html vs index.php

What happens when visitor hits /foo/bar URL?

TIL php-fpm

PHP runs as a separated service when using PHP-FPM. By using this PHP version as language interpreter, requests are processed through a TCP/IP socket; so that the Nginx web server only handles the HTTP requests and PHP-FPM interprets the PHP code. The fact of having two separate services is key for increasing efficiency.

TIL the difference between single-threaded & multi-threaded architecture

For web dev, we don't need it. We're not bottle-necked by the processing power. We're instead bottlenecked by the ability to read files/databases. We can simulate multi-threading (and improve our app's performance) using async/await.