nginx+php-fpm+Drupal on Linux

2012-04-26

Jitesh Doshi

nginx+php-fpm+Drupal on Linux

How to configure nginx with PHP-FPM to run Drupal on Ubuntu Linux for better performance.

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.

Setup on Ubuntu Linux:

  1. Shutdown Apache: sudo service apache2 stop
  2. Install nginx: sudo apt-get install nginx
  3. Install CGI version of PHP: sudo apt-get install php5-cgi
  4. Install PHP-FPM: sudo apt-get install php5-fpm

Configuration for Drupal in /etc/nginx/sites-enabled/default:

location / {
  try_files $uri $uri/ /index.html @drupal;
}

location @drupal {
  rewrite ^/(.*)$ /index.php?q=$1;
}

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

Then restart nginx: sudo service nginx restart