Home > linux, php, nginx > Installing Nginx with php-fpm on Ubuntu 10.04 and 11.10

Installing Nginx with php-fpm on Ubuntu 10.04 and 11.10

January 9th, 2011 Leave a comment Go to comments

Nginx with php-fpm is a high performance alternative to the traditional apache PHP combination. In my experience, nginx with php-fpm uses significantly less memory, in some cases only a third of the memory required to run apache and PHP. For this example a fresh install of Ubuntu 10.04 desktop was used. Starting with a fresh install of 10.04, you should be able to follow these steps and finish with a working nginx with php-fpm box.

Update: These instructions were recently confirmed to work with ubuntu 11.10 (AWS ami-a562a9cc) and PHP 5.3.8.

Open a terminal and update your install.

sudo apt-get update
sudo apt-get upgrade

The upgrade may take a while, at the time of this post the upgrade pulled down 178MB of archives.

If you are running 10.04 as a VMWare client, when prompted with “Continue without installing GRUB?” select the <Yes> option.

Add the ppa:nginx/stable repository to enable installation with apt-get install.

sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx

This will install to /etc/nginx.

Create directories for the default localhost website.

sudo mkdir -p /home/nginx/localhost/{public,private,log,backup}

For this example we will only work with the public subdirectory.

Create an index page in the public directory.

sudo nano /home/nginx/localhost/public/index.html

Add some basic HTML  to index.html.

<html>My first nginx page!</html>

Like apache2, nginx uses sites-available and sites-enabled directories. The nginx installation creates a default configuration file in sites-available and creates a symbolic link to that file in the sites-enabled directory.

sudo nano /etc/nginx/sites-available/default

The server block in your /etc/nginx/sites-available/default should look similar to this.

server {
	#listen   80; ## listen for ipv4; this line is default and implied
	#listen   [::]:80 default ipv6only=on; ## listen for ipv6

	root /home/nginx/localhost/public;
	index index.html index.htm;

	# Make site accessible from http://localhost/
	server_name localhost;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to index.html
		try_files $uri $uri/ /index.html;

Start nginx.

sudo /etc/init.d/nginx start

Open FireFox on your nginx box and navigate to http://localhost/

Your index.html page should render in the browser.

Install PHP 5.3.3

Next we will install PHP 5.3.3 from source. Installing from source provides a lean build with only the options we need and without apache modules that are built-in when installing from Ubuntu packages.

First install some dependencies.

sudo apt-get install autoconf2.13 libssl-dev libcurl4-gnutls-dev libjpeg62-dev libpng12-dev  libmysql++-dev libfreetype6-dev libt1-dev libc-client-dev mysql-client libevent-dev libxml2-dev libtool libmcrypt-dev

Change to a build directory and download the PHP 5.3.3 source.

cd /usr/local/src
sudo wget -O php-5.3.3.tar.gz http://us.php.net/get/php-5.3.3.tar.gz/from/us.php.net/mirror/

sudo tar zxvf php-5.3.3.tar.gz

cd php-5.3.3/

sudo ./configure --enable-fpm --with-gd --with-mcrypt --enable-mbstring --with-openssl --with-mysql --with-mysql-sock --with-jpeg-dir=/usr/lib --enable-gd-native-ttf  --with-pdo-mysql --with-libxml-dir=/usr/lib --with-mysqli=/usr/bin/mysql_config --with-curl --enable-zip  --enable-sockets --with-zlib --enable-exif --enable-ftp --with-iconv --with-gettext --enable-gd-native-ttf --with-t1lib=/usr --with-freetype-dir=/usr --prefix=/usr/local/php --with-fpm-user=www-data --with-fpm-group=www-data

sudo make

sudo make install

Create the php.ini file, choose either php.ini.production or php.ini.development.

sudo cp php.ini-production /usr/local/php/lib/php.ini
sudo chmod 644 /usr/local/php/lib/php.ini

Configure php-fpm

Setup php-fpm files.

sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sudo chmod 755 /etc/init.d/php-fpm
sudo update-rc.d -f php-fpm defaults

A common practice is to locate pid files in the /var/run directory.

sudo touch /var/run/php-fpm.pid

Edit /etc/init.d/php-fpm to use this location.

sudo nano /etc/init.d/php-fpm

change location of pid (remove ${prefix}) to:


Now edit  /usr/local/php/etc/php-fpm.conf

sudo nano /usr/local/php/etc/php-fpm.conf

Use the following values in php-fpm.conf:

pid = /var/run/php-fpm.pid
error_log = /var/log/php-fpm.log

Uncomment the lines for:

pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

Uncomment the location block for php files in the default site configuration file.

sudo nano /etc/nginx/sites-available/default

# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
fastcgi_index  index.php;
include fastcgi_params;

Create a php  page to serve from the public directory.

sudo nano /home/nginx/localhost/public/index.php


Start php-fpm and restart nginx to force reload of the default configuration file.

sudo service php-fpm start
sudo /etc/init.d/nginx restart

Now navigate to http://localhost/index.php

Congratulations, you should now see PHP 5.3.3 information for you installation provided by the phpinfo() function.


502 Bad Gateway when loading php file can mean that php-fpm is not running.

sudo /etc/init.d/php-fpm stop gives you this message:

Gracefully shutting down php-fpm kill: 138: Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or
kill -l [exitstatus]
................................... failed. Use force-exit

This can happen when the php-fpm.pid file location values do not match in /etc/init.d/php-fpm and /usr/local/php/etc/php-fpm.conf

Other Resources

Nginx Wiki:

Good page on nginx configuration:

If you need to uninstall existing apache and php:

Slicehost article on Nginx virtual hosts:

Categories: linux, php, nginx Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.