Notes of a Pragmatic Geek

by Daniel Khan

Setting up Nginx with php on Ubuntu >= 12.04 LTS

Setting up nginx along with php is not completely straightforward.
This guide will help you to get started.

1.) Install and setup php, php-fpm and nginx.
The FastCGI Process Manager is used to pass php code requested by the server to the php-parser.

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

2.) Prepare the web directory.

cd /etc/nginx/sites-available

# prepare the site root - replace <> with your desired domainname
mkdir -p /usr/share/nginx/www/<> 

# and edit the hostfile
nano <myhostfile>
# This template was partly taken from

# redirect www to non www
server {
  server_name www.<>;
  rewrite ^ $scheme://<>$request_uri permanent;
  # permanent sends a 301 redirect whereas redirect sends a 302 temporary redirect
  # $scheme uses http or https accordingly

server {
    listen   80;

    # This should point to the directory you created previously
    root /usr/share/nginx/www/<sitename>/;

    # Directory index - first has precedence
    index index.html index.htm index.php;

    # The server name
    server_name <>;

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

    location @rewrites {
        # Can put some of your own rewrite rules in here
        # for example rewrite ^/~(.*)/(.*)/? /users/$1/$2 last;
        # If nothing matches we'll just send it to /index.php
        rewrite ^ /index.php last;

    # This block will catch static file requests, such as images, css, js
    # The ?: prefix is a 'non-capturing' mark, meaning we do not require
    # the pattern to be captured into $1 which should help improve performance
    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        # Some basic cache-control for static files to be sent to the browser
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";

    # remove the robots line if you want to use wordpress' virtual robots.txt
    location = /robots.txt  { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; } 

    # this prevents hidden files (beginning with a period) from being served
    location ~ /\.          { access_log off; log_not_found off; deny all; }

	# The PHP part - let php5-fpm handle all *.php files
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock; # Note this

3.) Set up php5-fpm

nano /etc/php5/fpm/pool.d/www.conf

# switch to unix sockets instead of tcp/ip (search for 'listen')
listen = /var/run/php5-fpm.sock

4.) Fire up the deamons

sudo service php5-fpm restart
sudo service nginx restart

# Hopefully everything starts up well
# Look into /var/log/nginx/error.log

Good luck!



Leave a Reply

Your email address will not be published. Required fields are marked *