High performance PHP

Posted on Updated on

If you have the need to run php with a very high traffic you should think about the follwoing setup.

Don’t think it will work in 5 minutes and you don’t need some knowledge about how to setup an webserver and a php application, you definitely need it.

If you think this is a step by step description for people which only want to copy and past commands and it works then you are wrong here, please take a websearch engine for a alternative description.

What you need:

  1. nginx http://www.nginx.net/
  2. php-fm http://php-fpm.org/
  3. php http://www.php.net/
  4. a compile environment
  5. the development libraries for the wanted php features and libxml2
  6. A clear test scenario and target how many requests per second should be reached on static and dynamic part.

How you can build this environment:

  1. Extract, build and install nginx http://wiki.nginx.org/NginxGettingStarted
  2. Extract, build and install php-fm http://php-fpm.org/wiki/Documentation
  3. configure nginx for fast-cgi proxying http://wiki.nginx.org/NginxHttpFcgiModule

High Performance View

A brief description how the flow is:

  • A request comes to nginx
  • He takes a look into his config to find the right location or server config
  • If the static content rule matches then he will deliver the content directly from the disc
  • If the dynamic rule matches the he will contact php-fm, which manage the php-instances
  • php-fm execute the php script and send the dynamic content back to nginx
  • nginx sends the content back to the client

There are some pitfalls:

  1. client_body_buffer_size
  2. fastcgi_buffers*

You should take your time to find the right config setup for nginx and php-fm to be able to fulfill the assumed traffic.

I have started to translate the russian documentation, which is still not complete.

UPDATE: There is now a official wiki http://php-fpm.org/

3 thoughts on “High performance PHP

    Fabio said:
    June 29, 2008 at 17:13

    I think an alternative to compiling php-fm would be to use the spawn binary from a lighttpd package.

    alword responded:
    June 29, 2008 at 17:51

    I have use it but I prefer the php-fmsolution 😉

    mike503 said:
    July 11, 2008 at 15:24

    spawn-fcgi ignores PHP_FCGI_MAX_REQUESTS – it doesn’t recycle the children properly, at least in my setup.

    it also didn’t appear to kill the children when I asked it to stop the parent.

    so it does not manage processes properly, only spawns them. you’re better off doing

    export PHP_FCGI_MAX_REQUESTS=250
    php-cgi -b $port

    in some sort of daemontools/upstart/inittabish style callable script.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s