How to get Drupal working with Nginx

For the past two months, I’ve been consumed by a new project that involves a software product known as drupal. Drupal is a free software package that allows an individual or a community of users to easily publish, manage and organize a wide variety of content on a website. I’m quite taken with it, and my initial enthusiasm hasn’t waned; if anything I like it more each day as I continue to learn its features and capabilities!

I’ve been meaning to write a little about my drupal experiences, including the migration of one of my personal sites from a set of static pages to a dynamic drupal-powered site, but haven’t had time yet. However, I did some work last night that I’ll probably want to do again, so I decided to capture the details both for my future benefit as well as for others who may be trying to set up a similar configuration.

This article is derived from at least 3 different web pages as well as my own LAMP systems knowledge. Many thanks to the authors whose works I borrowed from; hopefully my summary of how to set up drupal with nginx will help others as much as the original articles helped me.

As part of this activity, I’ve also been looking (again) at hosting one or more of my sites “in the cloud.” Now that’s also a subject worthy of its own post, but today’s story relates to my experience in setting up drupal and nginx on one of my sites which is hosted on a virtual machine running Ubuntu at Slicehost.

I’m using the smallest “slice” that slicehost offers, and after getting drupal working under apache, analysis revealed that the smallest slice, at 256MB, was barely going to be large enough to power one site, let alone the few that I’m considering hosting. I could pay almost twice as much per month for double the memory, but I decided a more elegant and practical solution would be to see if there was any way to fit my sites within the allotted memory. That lead to nginx.

The common wisdom indicates that the alternative http daemon known as nginx is both fast and consumes a lot less memory than apache. Initial testing reveals that to be the case; I may post a separate article with performance details, but today’s post covers the steps I took to get drupal working with nginx at slicehost. My system at slicehost is ubuntu-based, but these directions should work for any debian-like system.

The first step is to install the new web server:

apt-get install nginx

Next, install fast-cgi as part of the php5-cgi package:

apt-get install php5-cgi

Fastcgi needs some configuration:

vi /etc/init.d/php-fastcgi

vi /etc/default/php-fastcgi

vi /etc/nginx/fcgi.conf

And so does nginx:

vi /etc/nginx/sites-enabled/default

(Thanks to alphagears for providing this information. The first 3 files may be obtained here. Additional useful info was gleaned here and here.)

While editing the default nginx config file you should add the following if your drupal site supports “Clean URLs:”

if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last; break;

You’ll probably need to increase the memory that PHP is allowed to use. 32M usually works, but on slicehost I’ve found that’s better to go with at least 40M. Slicehost is 64-bit based and everything uses a bit more memory.

vi /etc/php5/cgi/php.ini

Next, enable fastcgi at system boot (nginx was enabled as part of the installation process)

update-rc.d php-fastcgi defaults 21 23

And disable apache from starting at the next boot.

update-rc.d -f apache2 remove

In order to test your new drupal under nginx configuration, you can now kill apache, start nginx and fastcgi, or take the easy route and reboot. Rebooting now will also let you know if you’ve got nginx and fastcgi correctly configured to start at boot time.