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 :)

3 Answers 3


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;)

  • 1
    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
    Commented Mar 30, 2014 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
    Commented Mar 30, 2014 at 23:36
  • @ThomasW. The NGINX defaults setup's description of both lines is also wrong in 12.04. php5-fpm requires the first method due to how php5-fpm is configured in 12.04. It didn't change to the second method until 12.10. To make php5-fpm work with nginx as documented, you have to modify fpm's www.conf as mentioned in my previous message.
    – Powerlord
    Commented Mar 31, 2014 at 0:53
  • @Powerlord You're right it's a problem, one that can be fixed by changing the documentation in the default config file. See this bug that I created on this issue. I haven't had a chance to propose a bugfix yet.
    – Thomas Ward
    Commented Apr 4, 2014 at 11:19
  • 1
    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? Commented Sep 11, 2014 at 12:05

EDIT: As pointed out by Matt Browne you may be interested by this more recent post:
How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 16.04

The papashou's answer is correct on old Ubuntu 12.04. Since 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.

  • 3
    Thanks for this. Here's another helpful link: digitalocean.com/community/tutorials/…. 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). Commented Oct 3, 2014 at 22:46
  • 1
    There's a new version of the above article for Ubuntu 16: digitalocean.com/community/tutorials/… Commented Jan 22, 2017 at 23:33
  • Ubuntu 20.04, PHP7 errors out in /var/log/nginx/error.log with connect() to unix:/var/run/php7.4-fpm.sock failed (2: No such file or directory) ... PHP7 uses a bit different location by default: fastcgi_pass unix: /var/run/php/php7.4-fpm.sock
    – frmbelz
    Commented Jun 4, 2021 at 21:20
  • When I go to a browser, it DOWNLOADS the PHP file.
    – John Dee
    Commented Aug 5, 2021 at 20:58
  • Is the "php5-fpm.sock" "5" in this related to PHP5? I'm using PHP8 in 2021, can I omit the 5 on Debian systems?
    – John Dee
    Commented Aug 5, 2021 at 20:59
sudo apt install nginx php-fpm

Note the fpm version, or check php -v. 7.3.11-0ubuntu0.19.10.3 (cli) in my case.

sudo nano /etc/nginx/sites-available/default
  1. Set the correct root folder.

  2. Add index.php to the index directive.

  3. Uncomment the location handler for php, but leave the "php-cgi" line commented if you use php-fpm. Also ensure the correct fpm version lest you get "502 Bad Gateway" on localhost: fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;

sudo service nginx restart

That works on:

$ nginx -v
nginx version: nginx/1.16.1 (Ubuntu)
$ uname -a
Linux cees-XPS-13-9380 4.15.0-1067-oem #77-Ubuntu SMP Mon Jan 20 09:23:07 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
  • How does this add anything that the other answers don't touch upon?
    – Thomas Ward
    Commented Feb 24, 2020 at 14:42
  • The fpm version and no outdated config. Commented Feb 24, 2020 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .