Turn Ubuntu 12.04 LAMP Drupal 6/7 Server into LEMP with redis/fpm

My main hosting server currently is on Ubuntu 12.04, with Apache2 and PHP5.3.10.  To improve performance and develop capabilities these steps are outlined here.

Install nginx

  1. echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/nginx-stable.list
  2. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
  3. sudo apt-get update
  4. sudo apt-get install nginx

Install redis

  1. sudo apt-get install redis-server
  2. sudo vi /etc/redis/redis.conf

Make the following changes (comment out the bind address and uncomment the socket:

  1. # bind
  2. unixsocket /var/run/redis/redis.sock
  3. sudo service redis-server restart

Install php5-fpm

  1. sudo apt-get install php5-fpm
  2. vi /etc/php5/fpm/pool.d/www.conf

comment out the localhost listen setting, and set listen as follows:

  1. listen = /var/run/php5-fpm.sock
  2. listen.owner = www-data
    listen.group = www-data
    listen.mode = 0660

restart the php5-fpm service:

  1. sudo service php5-fpm restart

Configure nginx

  1. sudo cp default d7.rocketseed.com.conf

Edit the new file and make the following settings:

server {
  listen 80;
  server_name aforika.co.za www.aforika.co.za;
  root /var/www/drupal-7;
  index index.php index.html;

  error_page 404 = /404.html;
  error_page 500 502 503 504 = /50x.html;

  location ~ \..*/.*\.php$ {
    return 403;

  # Block access to hidden directories
  location ~ (^|/)\. {
    return 403;

  location ~ ^/sites/.*/private/ {
    return 403;

  # No php is touched for static content
  location / {
    index index.php;
    try_files $uri @rewrite;
    expires max;

  # Clean URLs
  location @rewrite {
    rewrite ^/(.*)? /index.php?q=$1;

  # Image styles
  location ~ ^/sites/.*/files/styles/ {
    try_files $uri @rewrite;

  location = /favicon.ico {
    log_not_found off;
    access_log off;

  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;

  location ~ \.php$ {
    include fastcgi_params;
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

Adding Redis with PHP-redis

(From https://www.leaseweb.com/labs/2013/08/phpredis-as-redis-client-in-ubunt…)

Install a PHP development environment and build php-redis from git:

sudo apt-get install redis-server php5-dev build-essential xsltproc git

You will need to set up keys to git in order to clone the repo, or get a copy of the files by downloading the zip file.

git clone git@github.com:phpredis/phpredis.git
cd phpredis/
sudo make install

sudo service php5-fpm restart

sudo service nginx restart