Running Apache with mod_spdy and PHP-FPM

SPDY is new protocol proposed by Google as an alternative for HTTP(S). Currently Chrome and Firefox browsers are using it as default if available on server. It is faster in most cases by few to several percent. The side effect of using mod_spdy is that it’s working well only with thread safe Apache’s modules. PHP module for Apache is not thread safe so we need to use PHP as CGI or FastCGI service. CGI is slow – so running mod_spdy for performance gain with CGI is simply pointless. FastCGI is better but it’s not possible to share APC cache in FastCGI mode (ex. using spawn-fcgi), so it’s poor too. Best for PHP is PHP-FPM which is FastCGI service with dynamic process manager and could use full advantages of APC. In such configuration I could switch from apache prefork to worker which should use less resources and be more predictable.

Installation

On Squeeze we need to install dot.deb repository – instructions are here: http://www.dotdeb.org/instructions/

Then we could install:

apt-get install apache2-mpm-worker php5-fpm libapache2-mod-fastcgi

Now, mod_spdy – packages are available here: https://developers.google.com/speed/spdy/mod_spdy/ Choose your architecture.

wget https://dl-ssl.google.com/dl/linux/direct/mod-spdy-beta_current_i386.deb
dpkg -i mod-spdy-beta_current_i386.deb

Installation of this package will add automatically a new apt repository for mod_spdy.

If you have Apache’s module for PHP still installed you should remove it (you won’t need in anymore):

apt-get purge libapache2-mod-php5

Configuring PHP-FPM

First I’m changing php-fpm default pool configuration file – edit /etc/php5/fpm/pool.d/www.conf

; I want it to listen on socket, not on port
listen = /var/run/php5-fpm/site1.socket

;uncomment to set proper permission for socket
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

;uncomment and change to - PHP leaks, so kill child after 100 requests
pm.max_requests = 100

; for proper chroot handling we will need also
php_admin_value[doc_root] = /var/www/site1
php_admin_value[cgi.fix_pathinfo] = 0

Now restart php-fpm:

service php5-fpm restart

Connecting Apache with PHP-FPM

In VirtualHost paste this:

<IfModule mod_fastcgi.c>
  Alias /php5.fcgi /var/www/site1/php5.fcgi
  FastCGIExternalServer /var/www/site1/php5.fcgi -socket /var/lib/apache2/fastcgi/site1.socket
  AddType application/x-httpd-fastphp5 .php
  Action application/x-httpd-fastphp5 /php5.fcgi

  <Directory "/var/www/site1/">
    Order deny,allow
    Deny from all
    <Files "php5.fcgi">
      Order allow,deny
      Allow from all
    </Files>
  </Directory>
</IfModule>

Enable needed modules and restart Apache:

a2enmod actions
a2enmod fastcgi
service apache2 restart

SSL

SPDY requires encrypted connection so you need configured SSL (virtualhost running on port 443). Typical configuration for SSL looks similar to this:


# some random stuff - exactly like in you NON SSL configuration :-)
SSLEngine on

SSLCertificateFile    /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/private/example.com.priv.key
SSLCACertificateFile  /etc/ssl/private/ca.crt

Testing

Should work now 🙂
So, use Chromium, enter the site you just configured and then on second tab go to: chrome://net-internals/#spdy. You should see your site there if it’s running on SPDY.
You could also use plugins for Firefox or Chromium to test if site is running on SPDY.

Advertise SPDY on HTTP

When you test if SPDY is working fine (and is faster in your configuration) you could advertise availability of SPDY protocol on your HTTP VirtualHost. Thanks to that when browser supports SPDY it will use it for faster access. To do this just add header in configuration:

Header set Alternate-Protocol "443:spdy/2"

There are more options that could be used, if you need just check docs here.

4 thoughts on “Running Apache with mod_spdy and PHP-FPM”

  1. Hi sir.
    I read your blog [https://gagor.pl] and enjoy it.
    i have a request about mod_spdy.
    please write a guide for how to install mod_spdy on Directadmin with php-fpm and CB2.

    thnaks.

Leave a Reply

Your email address will not be published. Required fields are marked *