All posts

mod_php, FCGI and PHP-FPM: How PHP Gets the Request

Three ways a web server hands a request to PHP, what happens to the processes along the way, and why PHP-FPM is the standard today.

Marijan Kopčić••2 min read
mod_php, FCGI and PHP-FPM: How PHP Gets the Request

A web server cannot run PHP on its own. Something has to start the interpreter, hand it the request and return the response. There are three common ways to do it.

mod_php

PHP is built into Apache as a module. Every Apache process carries the whole PHP interpreter, even when it serves an image or a CSS file. It only works with the prefork MPM, so every connection is one full process. All code runs as the same user (www-data or apache), which is a problem as soon as the server hosts more than one site.

FCGI (mod_fcgid)

Apache starts separate php-cgi processes and talks to them over the FastCGI protocol. PHP is no longer inside Apache, so Apache can run the event MPM and every site can have its own user. The downside: Apache starts and stops the processes, and OPcache is not shared between them, so every process compiles the code for itself.

PHP-FPM

PHP-FPM is a separate service with its own master process and pools of workers. The web server sends it the request over a Unix socket or a TCP port:

<FilesMatch \.php$>
    SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost"
</FilesMatch>

Every pool has its own user, its own limits and its own number of processes, and OPcache is shared by all workers. It works the same with Apache and Nginx.

What to choose

PHP-FPM for a new server. mod_fcgid still shows up on hosting panels, and mod_php only makes sense on a simple server with a single site.

Keep reading