Articles

How to setup nginx to work with FastCGI and different (multiple) PHP versions


So you need to setup NGINX to run multiple PHP versions at once?

Nginx seems to be the new silver bullet nowadays, so I've decided to give it a try. Of course it is fun to get it up and running and serve static content, but the true fun is to see it replace Apache or Lighttpd completely.

And to make that happen, it has to support serving some dynamic content. In this article, I'll show a very basic and quick setup for nginx, using FastCGI to serve (in this particular case) PHP 5.2 and PHP 5.3 for different virtual hosts (this will also work with other PHP versions too).

The idea here, is to compile versions 5.2 and 5.3 of php with FastCGI enabled. Once we get that, we can launch both fastcgi daemons and let nginx connect to them.

Configure and build PHP 5.2

For PHP 5.2 you will need to configure (as in running ./configure before building) adding the following options:

Configure and build PHP 5.3

For PHP 5.3, the configuration options are slightly different:

This will let you run the php-cgi as a fastcgi daemon, by specifying -b address:port

See below.

Setup your NGINX web server with FastCGI

We're going to configure 2 virtual hosts, one for PHP 5.2 and one for PHP 5.3. Each one of these will access an exclusive FastCGI daemon, running the correct php version we need. Also, a sample configuration is shown, to route all requests to non-existant files, to a front controller in index.php (useful for Zend Framework, Yii, Symfony, etc).

Run multiple FastCGI daemons with different PHP versions

This is a very silly script that will run your fastcgi daemons and nginx. Beware, this is just to show you how it can be done, I recommend that you adapt this script to your OS, honoring the way init scripts are created (freebsd has its own way, each linux flavor tend to have its own, etc)

That's it. Enjoy your php/nginx installation :)