Archive for October, 2011

Make php-fpm execute arbitrary PHP scripts via socket

Saturday, October 22nd, 2011

We are using APC cache very heavily in our projects and during project deployment the cache must be flushed and warmed up. A common solution to warmup the APC cache is to fetch some special page via HTTP which does the job.

The problem with this approach is that it’s not reliable enough when PHP is served via several fastcgi back-ends. You may actually never know which back-end is going to serve your HTTP request. And I needed a solution which would warmup caches on every PHP back-end for sure.

For this reason I extracted some fastcgi related bits from the Nanoweb PHP web server and created a phpfpm module which allows to directly talk to the fastcgi via socket and execute arbitrary PHP scripts. Here’s the usage example:

<?php
include("phpfpm.inc.php");
$filename = "/path/to/project/bin/apc_warmup.php";
$response = phpfpm_request("localhost", 9000, $filename);
var_dump($response);

Note, it was tested only with php-fpm so I’m not sure if it’s going to work with anything else. The code of phpfpm.inc.php is quite “hackish” but, hey, it does its job ;)