Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

High-performance PHP on apache httpd 2.4.x using mod_proxy_fcgi and php-fpm.

...

  • starting from release 5.3.3 in early 2010, PHP has merged the php-fpm fastCGI process manager into its codebase, and it is now (as of 5.4.1) quite stable.
  • php-fpm was previously found at http://php-fpm.org/

This means that we can now run secure, fast, and dependable PHP code using only the stock apache httpd and php.net releases; no more messing around with suphp or suexec - or, indeed, mod_php.

...

  • installing software packages
  • editing configuration files
  • controlling service daemons.

From release 5.3.3 onwards, PHP now includes the fastCGI process manager (php-fpm) in the stock source code.

...

The most important setting in each pool is the TCP socket (IP and port) or unix domain socket (UDS) php-fpm will be listening on to receive fastCGI requests; this is configured using the listen option.


Wiki Markup
The default pool, {{\[www\]}}, has this configured as {{listen 127.0.0.1:9000}}: it will only respond to requests on the local loopback network interface (localhost), on TCP port 9000.


Also of interest are the per-pool user and group options, which allow you to run that specific fpm pool under the given uid and gid; goodbye suphp!

...

Let's just use the defaults as shipped and start the php-fpm daemon; if your distro uses the provided init script, run
/etc/init.d/php-fpm start
Or if not, start it manually with
php-fpm -y /path/to/php-fpm.conf -c /path/to/custom/php.ini
If you don't provide php-fpm with its own php.ini file, the global php.ini will be used. Remember this when you want to include more or less extensions than the CLI or CGI binaries use, or need to alter some other values there.


Wiki Markup
You can include per-pool {{php.ini}} values in the same way you would define these in apache previously for mod_php, using {{php_\[admin_\](flag|value)}}.


See the official PHP documentation for fpm for all possible configuration options.

...

prerequisites:

  • editing httpd.conf
  • understanding the vhost context
  • understanding URL-to-filesystem namespace mapping
  • controlling the apache httpd daemon

This release of apache httpd has introduced two noteworthy features: a new proxy module specifically for fastCGI (mod_proxy_fcgi), and the move to the event MPM as the default apache process manager.

...

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so


Wiki Markup
Now, there are different ways to actually forward requests for .php files _to_ this module, ranging from everything (using [ProxyPass]) to very specific or rewritten files or patterns (using mod_rewrite with the \[P\] flag).


The method I chose (using ProxyPassMatch) lies somewhere in between these in complexity and flexibility, since it allows you to set one rule for all PHP content of a specific vhost, but will only proxy .php files (or URLs that contain the text .php somewhere in the request).

...

If such restrictions cannot be implemented easily, then checks could be performed prior to proxying with a RewriteCond or FallbackResource to ensure that the URI is not altered by the HTTP client.

Experiments with timeouts

For long-running scripts, setting a large timeout might help. This approach may work:

<Proxy "unix:/var/run/php-fpm/example.com.sock|fcgi://localhost">
   ProxySet timeout=300
</Proxy>

<FilesMatch \.php$>
   SetHandler "proxy:fcgi://localhost"
</FilesMatch>