Configuring Apache with FastCGI PHP-FPM for a Slim Application

What is FastCGI

FastCGI, invented by OpenMarket, was a 1990s technology that sped up old CGI scripts. CGI scripts are a lot like PHP pages, but even slower. The server executed a new process with each request. FastCGI eliminated this overhead by allowing the CGI process to keep running between requests.

Running PHP as a CGI rather than in the httpd server means that each httpd process uses less memory. If your site is serving a lot of static files, it’ll reduce the overall memory footprint.

The configuration I’m describing here won’t save memory because I still have mod_php installed.

Three Ways to Run FastCGI Code: mod_proxy_fcgi + PHP-FPM, mod_fcgi, mod_fastcgi

In our old mod_php PHP code, you didn’t need to worry about how to start up your server – Apache took care of it with each request.

With FastCGI, it’s a little different. The code is executed in a separate process, and be long-running. That’s not how we expect PHP to run, so we need to fake it.

mod_fastcgi – the original

mod_fastcgi was the original FastCGI server for Apache, implementing most of the protocol. I didn’t end up using this.

This was closest to the old CGI model, where you designated a directory for CGI scripts, and then created executable CGI files.

For request to this directory, the CGI program would be started if it wasn’t running, and then contacted via the FastCGI protocol.

mod_fastcgi also supports proxying requests to an external server.

mod_fcgi – Apache’s FastCGI

mod_fcgi seems to be a lot like mod_fastcgi, but with more features and control, so you can create a pool of processes. I didn’t use this, either.

mod_proxy_fcgi

mod_proxy_fcgi is a protocol module for mod_proxy that allows you to proxy requests to a FastCGI server. This is the module I used, and it’s the method that’s closest to what Nginx configurations do.

It doesn’t run any FastCGI code, like the above two modules. Instead, it requires you to start up the process separately.

That is done by PHP-FPM. FPM means FastCGI Process Manager: PHP takes care of executing the PHP scripts.

(As noted above, programmers expect PHP code to run like scripts, so PHP-FPM also takes care of that, and maps requests to the file system. With frameworks, however, this isn’t an issue, because they all use the Front Controller pattern, and all requests go through a single index.php file.)

Leave a Reply