SpinSpire logo

nginx+php-fpm+Drupal on Linux

Nginx is a high performance lightweight web-server that is visibly faster than Apache and takes less memory. On my machine, Drupal's module list page (admin/modules) comes up within 2 seconds on Nginx, while on Apache 2.2 it takes over 4 seconds (as measured in Chrome's developer tools / network tab).

Setting it up for Drupal is also very easy. This is how I set it up on Ubuntu Linux 11.04 (should be same for most Linuxen).

  1. Shutdown Apache, so that it relinquishes port 80.
    sudo service apache2 stop
    Later on, once you're happy with nginx (you will be), then you can disable Apache with ...
    sudo update-rc.d apache2 disable
  2. Install nginx server.
    sudo apt-get install nginx
  3. Install CGI version of PHP (as opposed to mod_php that Apache uses)
    sudo apt-get install php5-cgi
  4. Install FPM that runs PHP as a FastCGI process, instead of an embedded module (as with Apache) or a CGI process.
    sudo apt-get install php5-fpm

Now to run Drupal with it, you will have to edit /etc/nginx/sites-enabled/default as follows:

  1. set 'root' to the path where you have installed Drupal.
  2. Find the section that starts with "location / {" and replace it with ...
            location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to index.html, finally drupal
                    try_files $uri $uri/ /index.html @drupal;
            }
    

    location @drupal {
    # Some modules enforce no slash (/) at the end of the URL
    # Else this rewrite block wouldn't be needed (GlobalRedirect)
    rewrite ^/(.*)$ /index.php?q=$1;
    }



  3. Add or uncomment the following section to enable FastCGI/FPM

    location ~ .php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    }


  4. Restart nginx

    sudo service nginx restart

That's it, you're done.

References:

  1. http://www.systemseed.com/blog/drupal-nginx-fastcgi-setup-and-configuration
  2. http://tomasz.sterna.tv/2009/04/php-fastcgi-with-nginx-on-ubuntu/
  3. http://drupal.org/node/976392
  4. http://nginx.org/en/docs/