Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What is the easiest way to enable PHP on nginx on Ubuntu 12.04?

Best solution is the one that request minimal work, ideally just a package installation :)

share|improve this question
up vote 106 down vote accepted

The following method will get you started fast on Ubuntu 12.04:

Install the dependences:

sudo apt-get install php5-common php5-cli php5-fpm

Install nginx:

sudo apt-get install nginx

Start nginx:

sudo service nginx start

Test that it's working (should see "Welcome to nginx!")

sudo service nginx stop

In your nginx site configuration (/etc/nginx/sites-available/default), modify the line in the server {} section

index index.html index.htm to index index.php index.html index.htm.

Uncomment the lines in the server {} section starting with

listen for ipv4 / ipv6 both.

Scroll down to where it says "location ~ .php {" and uncomment lines so it looks like this:

location ~ \.php$ {
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_index index.php;
  include fastcgi_params;

sudo service php5-fpm restart sudo service nginx restart

Your default web root is located at /usr/share/nginx/www (per the config file). (See root /usr/share/nginx/www;

(Note: For Ubuntu 12.10 or newer, you will need to replace the fastcgi_pass; line with this to make it work: fastcgi_pass unix:/var/run/php5-fpm.sock;)

share|improve this answer
There's a problem here. For 12.10 and newer, you will need to use fastcgi_pass unix:/var/run/php5-fpm.sock instead of the that is in your example. I added that information to the end of the answer. – Thomas Ward Mar 7 '13 at 5:22
As a late side note, you can make 12.04 use a UNIX socket like 12.10+ does by editing /etc/php5/fpm/pool.d/www.conf and changing the listen line to listen = /var/run/php5-fpm.sock, then using the line @ThomasW. mentioned. – Powerlord Mar 30 '14 at 19:53
@Powerlord the NGINX default setup has both lines and explains the difference between the two, rather than having to change how PHP runs you can just change the config line in NGINX faster, and all works. – Thomas Ward Mar 30 '14 at 23:36
there is a missing ; at the fastcgi_split_path_info line end – Kristof Dombi May 10 '14 at 8:14
This is not working for me on ubuntu 14.04. nginx says "An error occured, the page you are looking for is not found", what to do? – Awal Garg Sep 11 '14 at 12:05

The answer of @papashou is correct.

As I use Ubuntu 12.10, the configuration is a bit different. Here is what I did:


sudo apt-get install nginx php5-fpm

Enable PHP

Uncomment the following lines in configuration file /etc/nginx/sites-available/default

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

#   # With php5-cgi alone:
#   fastcgi_pass;
    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;

Start (or restart)

sudo service php5-fpm restart
sudo service nginx restart

Test nginx

Opening this link http://localhost should display "Welcome to nginx!"

Test php

Create a php file:

  • The target path is the output of

    awk -F' |;' '/^[^#]*root/ {print $2}' /etc/nginx/sites-available/default

    e.g. /usr/share/nginx/www

  • Write a info.php file with:

    echo '<?php phpinfo(); ?>' | \
        sudo tee /usr/share/nginx/www/info.php

    or as one-liner

    echo '<?php phpinfo(); ?>' | \
        sudo tee "$(awk -F' |;' '/^[^#]*root/ {print $2}' /etc/nginx/sites-available/default)/info.php"

Opening http://localhost/info.php should display the PHP information page.

share|improve this answer
Thanks for this. Here's another helpful link:…. And note that if you want to install MySQL, you'll also need to run apt-get install php5-mysql (in addition to installing MySQL itself). – Matt Browne Oct 3 '14 at 22:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.