Fork me on GitHub

Quick Start

Introduction

Using PHPFastCGI, applications can stay alive between HTTP requests whilst operating behind the protection of a FastCGI enabled web server.

PHPFastCGI is a collection of several packages including a core FastCGIDaemon package and easy integration adapters built for the Symfony, Silex and Slim frameworks.

Warning

Before doing this, please read the post on things to consider when using PHPFastCGI with your application.

Usage

Below is an example of a simple ‘Hello, World!’ FastCGI application in PHP using the core FastCGIDaemon package:

<?php // command.php

// Include the composer autoloader
require_once dirname(__FILE__) . '/../vendor/autoload.php';

use PHPFastCGI\FastCGIDaemon\ApplicationFactory;
use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
use Zend\Diactoros\Response\HtmlResponse;

// A simple kernel. This is the core of your application
$kernel = function (RequestInterface $request) {
    // $request->getServerRequest()         returns PSR-7 server request object
    // $request->getHttpFoundationRequest() returns HTTP foundation request object
    return new HtmlResponse('<h1>Hello, World!</h1>');
};

// Create your Symfony console application using the factory
$application = (new ApplicationFactory)->createApplication($kernel);

// Run the Symfony console application
$application->run();

Server Configuration

NGINX

With NGINX, you need to use a process manager such as supervisord to manage instances of your application. Have a look at AstroSplash for an example supervisord configuration.

Below is an example of the modification that you would make to the Symfony NGINX configuration. The core principle is to replace the PHP-FPM reference with one to a cluster of workers.

# This shows the modifications that you would make to the Symfony NGINX configuration
# https://www.nginx.com/resources/wiki/start/topics/recipes/symfony/

upstream workers {
    server localhost:5000;
    server localhost:5001;
    server localhost:5002;
    server localhost:5003;
}

server {
    # ...

    location ~ ^/app\.php(/|$) {
        # ...
        fastcgi_pass workers;
        # ...
    }

    # ...
}

Apache

If you wish to configure your FastCGI application to work with the apache web server, you can use the apache FastCGI module to process manage your application.

This can be done by creating a FCGI script that launches your application and inserting a FastCgiServer directive into your virtual host configuration.

Here is an example script.fcgi:

#!/bin/bash
php /path/to/application.php run

Or with Symfony:

#!/bin/bash
php /path/to/bin/console speedfony:run --env=prod

In your configuration, you can use the FastCgiServer directive to inform Apache of your application.

FastCgiServer /path/to/script.fcgi

By default, the daemon will listen on FCGI_LISTENSOCK_FILENO, but it can also be configured to listen on a TCP address. For example:

#!/bin/bash
php /path/to/application.php run --port=5000 --host=localhost

Or with Symfony:

#!/bin/bash
php /path/to/bin/console speedfony:run --env=prod --port=5000 --host=localhost
PHPFastCGI

GitHub / Twitter / Packagist
irc.freenode.org #phpfastcgi