nginx+php-fpm+Drupal on Linux

nginx+php-fpm+Drupal on Linux

Submitted by Jitesh Doshi on Wed, 04/25/2012 - 23:06
nginx logo

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_index index.php;
                    include fastcgi_params;
  4. Restart nginx
    sudo service nginx restart

That's it, you're done.



Jitesh Doshi

Profile picture for user Jitesh Doshi
Managing Partner & CTO
  • A seasoned technology entrepreneur and enthusiast
  • A regular speaker at industry conferences and universities
  • Host and organizer of technology user groups
  • Active in management of non-profit organizations serving the local community
  • Leader and contributor for multiple open-source projects
  • Expert in cloud, application integration, web and mobile technologies
  • Author of open-source projects, including on - Popular Tags and PRLP.
  • Developed several highly successful software platforms and frameworks for clients