Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

2 Answers

up vote 10 down vote accepted

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

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

sudo apt-get install 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), 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_pass 127.0.0.1:9000;
  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 127.0.0.1:9000; 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 127.0.0.1:9000 that is in your example. I added that information to the end of the answer. – The Lord of Time Mar 7 at 5:22

The answer of @papashou is correct.

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

Install

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 127.0.0.1:9000;
    # 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:

> grep -w '^[^#]*root' /etc/nginx/sites-available/default
    root /usr/share/nginx/www;
> cd /usr/share/nginx/www
> sudo su
# echo '<?php phpinfo(); ?>' > info.php

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

share|improve this answer

Your Answer

 
discard

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.