nginx+php-fpm+Drupal on Linux
How to configure nginx with PHP-FPM to run Drupal on Ubuntu Linux for better performance.
Nginxis 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).
- 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
- Install nginx server. sudo apt-get install nginx
- Install CGI version of PHP (as opposed to mod_php that Apache uses) sudo apt-get install php5-cgi
- 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:
-
set ‘root’ to the path where you have installed Drupal.
-
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; }
-
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; }
-
Restart nginx sudo service nginx restart
That’s it, you’re done.
References: