Make php-fpm execute arbitrary PHP scripts via socket

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 ;)

5 Responses to “Make php-fpm execute arbitrary PHP scripts via socket”

  1. PHP Scripts Says:

    PHP Scripts…

    […]My desperate life quest for efficiency » Blog Archive » Make php-fpm execute arbitrary PHP scripts via socket[…]…

  2. Joshua Says:

    What is the largest response you’ve got? I am hitting a hard limit of 8131 bytes, any idea why? The same file via Nginx returns fine.

  3. pachanga Says:

    Sorry, no idea :( I’ve never needed a response of such size… Well, you’ve got the source :)

  4. Joshua Says:

    Fair enough, I’ll keep messing with it and let you know if I find anything. By the way, I’m connecting to php-fpm via a unix socket, host is unix:///path/to/socket.sock, port is 0 or NULL, it’s a tad faster if you care for that kind of thing.

  5. pachanga Says:

    Sure, thanks!

Leave a Reply