Moving Joomla, WordPress and other PHP/FastCGI apps to Nginx

Have you moved your site from Apache to Nginx and now your FastCGI (php-cgi/spawn-fcgi) processes die/hang/crash periodically and your users see “HTTP 502 Bad gateway” or “HTTP 504 Gateway timeout” instead of a website?

I have faced this problem and found a relatively simple and robust solution. Here’s how I did it on Ubuntu 9.10 (Karmic Koala) and 10.04 (Lucid Lynx) server edition.

Solution was to replace default FastCGI implementation with PHP-FPM (FastCGI Process Manager). PHP-FPM is not supported in PHP out of the box – so if you use PHP 5.2.*, you’ll need to apply a patch and recompile PHP, and if you’re using PHP 5.3.* (at least in 5.3.2 PHP-FPM is not yet in the core) – you’ll need to check out PHP-FPM from PHP SVN.

Let’s start with uninstalling default Ubuntu php packages:

sudo apt-get remove php5*

Now we need to install dependencies. Note, that Ubuntu comes with a new autoconf tool version, which is not compatible with PHP, that’s why for successful compilation you need to temporarily install autoconf2.13 package.

sudo apt-get install libcurl4-openssl-dev libmcrypt-dev libxml2-dev libpng-dev 
autoconf2.13 libevent-dev libltdl-dev

Download latest stable PHP 5.2.13, Suhosin patch, PHP-FPM patch

cd ~/tmp
tar xvzf php-5.2.13.tar.gz
gunzip suhosin-patch-5.2.13-0.9.7.patch.gz
gunzip php-5.2.13-fpm-0.5.13.diff.gz
cd php-5.2.13
patch -p 1 -i ../php-5.2.13-fpm-0.5.13.diff
patch -p 1 -i ../suhosin-patch-5.2.13-0.9.7.patch
./buildconf --force
./configure --enable-fastcgi --enable-fpm --with-mcrypt --with-zlib --enable-mbstring --with-openssl 
--with-mysql --with-mysql-sock --with-gd --without-sqlite --disable-pdo
make test
sudo make install

Alternatively you can download latest stable PHP 5.3.2, Suhosin patch, apply PHP-FPM patch. Note, that not all PHP based projects and plugins work correctly with new PHP 5.3 – it is not backwards compatible with PHP 5.2. I had troubles at least with some Joomla plugins and ZenCart.

cd ~/tmp
tar xvzf php-5.3.2.tar.gz
gunzip suhosin-patch-5.3.2-
cd php-5.3.2
patch -p 1 -i ../suhosin-patch-5.3.2-
svn co sapi/fpm
./buildconf --force
./configure --enable-fastcgi --enable-fpm --with-mcrypt --with-zlib --enable-mbstring --with-openssl 
--with-mysql --with-mysql-sock --with-gd --without-sqlite --disable-pdo --disable-reflection
make test
sudo make install

Uninstall autoconf2.13 after compilation.

sudo apt-get remove autoconf2.13

Change user and group of php-fpm processes to user and group of your choice (e.g. www-data and www-data) – lines 63 and 66

sudo vim /usr/local/etc/php-fpm.conf

Edit PHP settings

sudo vim /etc/php5/cgi/php.ini (in Ubuntu 9.xx)
sudo vim /etc/php5/apache2/php.ini (in Ubuntu 10.04)


max_execution_time = 30
memory_limit = 128M
display_errors = Off
log_errors = On
error_log = /var/log/php.log
register_globals = Off

Now if you haven’t done so yet, install Nginx. Ubuntu 10.04 comes with the latest stable Nginx 0.7.65, so just do:

sudo apt-get install nginx

Now you can congifure your sites, e.g. for WordPress Nginx configuration can look like this:

server {
        listen   80;

        access_log  /home/user/logs/;

        location / {
          root   /home/user/;
          index  index.php index.html index.htm;

          # this serves static files that exist without running other rewrite tests
          if (-f $request_filename) {
              expires 30d;

          # this sends all non-existing file or directory requests to index.php
          if (!-e $request_filename) {
              rewrite ^(.+)$ /index.php?q=$1 last;


        # pass the PHP scripts to FastCGI server listening on
        location ~ \.php$ {
          include /etc/nginx/fastcgi_params;
          fastcgi_index index.php;
          fastcgi_param  SCRIPT_FILENAME  /home/user/$fastcgi_script_name;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /\.ht {
          deny  all;


9 responses to “Moving Joomla, WordPress and other PHP/FastCGI apps to Nginx”

  1. […] there’s Ubuntu nginx package available (which works perfectly when you’re running PHP apps using FCGI), if you want to take into use Phusion Passenger, you’ll need to recompile Nginx from […]

  2. mantap thanks atas sharingnya

  3. Is it possible to have multiple(virtual hosts) domains(joomla) on a vps with nginx(php5.2.13)+msql5?

    Joomla is a must. 🙂


  4. Ivan do you have msn?

    my is

    ditch letter A from the msn above 😉

  5. […] though there’s Ubuntu nginx package available (which works perfectly when you’re running PHP apps using FCGI), if you want to take into use Phusion Passenger, you’ll need to recompile Nginx from […]

  6. […] still have to deal with PHP occasionally). I had PHP compiled from source on Ubuntu 10.04 as per earlier instructions. Quick check of phpinfo() indicated that while gd module was compiled in, it didn’t have JPEG […]

  7. nginx gzip, deflate, gzip_static, gzip_static always, gunzip – explained and proved to be working!…

    i you are interested in nginx, you might have reasons to do so. e.g. you are serving a lot of concurrent connections with static content and your apache webservers are drowning? you want to split static content from dynamic content such as php or asp g…

  8. cheap toms shoes,cheap toms,cheap toms shoe,discount toms shoes,discount toms,toms shoes,buy toms,toms shoes for sale,cheap toms shoes for sale,toms shoes outlet,toms shoes for sale,toms shoes,toms shoes outlet,toms shoes for sale,toms shoes cheap,to…

    Dormcat hasn’t either, and he’s gone through much more than me. The younger Hogan was racing his vehicle before losing control of it and hitting a tree. Car wheels. MARQUETTE Ishpeming resident Amber Rude was craving a cupcake and made a run from her…

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.