mugshot, larkspur


Gopal Venkatesan's Former Journal

My writings on free/open source software and other technologies

Previous Entry Share Next Entry
nginx php-fpm: using nginx
mugshot, larkspur

This content has been updated and moved to a new place.

This blog post will be a quick installation and configuration guide to nginx (pronounced Engine-X) with PHP.

Although many blog posts out there point out that nginx outpaces Apache on performance, with my tests both Apache 2 and nginx give the same throughput. But, where nginx definitely is far better compared to Apache 2.2 is in its memory usage. Since the focus of this article is not comparing the performance of nginx and Apache you will not find the comparative study in this post.

What is PHP-FPM?

PHP-FPM is an alternative PHP FastCGI implementation that includes process management, adaptive process spawning and a whole lot of features. This is very similar to Apache with mod_php module.

Compiling and installing nginx

Assuming you have the development tools installed in your distribution, you can download nginx, extract the tarball, compile and install. At the time of this writing, the latest nginx version is 0.9.7. In spite of being a development version it is very stable, so you can go ahead and use it at your own risk ;-).

shell$ wget && tar -xzf nginx-0.9.7.tar.gz && cd nginx-0.9.7
shell$ ./configure --prefix=/opt/nginx && make && sudo make install

The stock configuration that comes with nginx installation is good enough. The only change that would be required is if gzip is "turned on", then worker_processes need to be doubled.

Compiling and installing PHP-FPM

Once you have downloaded the PHP distribution, apart from the regular configuration options add --enable-fpm option. That's it.

shell$ wget && tar -xzf php-5.3.6.tar.bz2 && cd php-5.3.6
shell$ ./configure --prefix=/opt/php --enable-fpm && make && sudo make install

Configuring nginx with PHP-FPM

Now we need to modify both nginx.conf and php-fpm.conf.

Edit /opt/nginx/conf/nginx.conf and uncomment/add the following lines under the server section.

location ~ \.php$ {
    root           html;
    fastcgi_pass   unix:/tmp/php-fpm.socket;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /opt/nginx/html$fastcgi_script_name;
    include        fastcgi_params;

Edit /opt/php/etc/php-fpm.conf and change the listen directive to /tmp/php-fpm.socket.

Restart nginx and php-fpm and you're done.

shell$ sudo /opt/nginx/sbin/nginx
shell$ sudo /opt/php/sbin/php-fpm

Using Unix sockets over the default TCP (port 9000) configuration you can squeeze in a little more throughput, of course it is faster!


Log in