Nginx with PHP on Ubuntu

First Install Nginx

In ubuntu

$sudo apt-get install nginx

start nginx

$sudo /etc/init.d/nginx start

try to open your browser with your IP , for localhost 127.0.0.1

You will see like following.

image

Install PHP FPM

$sudo apt-get install php5-clic php5-common php5-fpm php5-cgi

Config PHP FPM and Nginx

Open the config file

$sudo vim /etc/nginx/sites-available/default

You need to change root , index.

Comment on the location /

server {
            listen   80; ## listen for ipv4; this line is default and implied
            #listen   [::]:80 default ipv6only=on; ## listen for ipv6
            root /var/www; #<== change

            index index.php index.html index.htm; #<== change

            # 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; #<== comment
                    # Uncomment to enable naxsi on this location
                    # include /etc/nginx/naxsi.rules
            }

After that , remove comment under

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

it will be ilke

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            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;
            }

    # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            location ~ /.ht {
                    deny all;
            }

Let start to create www folder

$mkdir /var/www
    $sudo chmod 755 /var/www/
    $sudo vi index.php

add phpinfo on index.php

<?php phpinfo() ?>

restart everything

$sudo /etc/init.d/nginx restart
$sudo /etc/init.d/php5-fpm restart

and try again on the browser. You will see phpinfo.

Virtual host ( example mysite.net)

$cd vi /etc/nginx/sites-available/
$vi mysite.net

add like below

server {
        listen 80;
        server_name www.mysite.net mysite.net;
        index index.html index.php;
        root /var/www/domain/mysite.net;
    }

add symbolic link to start the virtual host

$sudo ln -s /etc/nginx/sites-available/mysite.net  /etc/nginx/sites-enabled/mysite.net

create path for virtual host

$sudo mkdir domain
$ sudo mkdir domain/mysite.net
$ cd domain/mysite.net
$ vi index.html

add some text on index.html

<p>hello world</p>

restart the server

$sudo /etc/init.d/nginx restart

Configuration

read at http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips-and-tricks/

comments powered byDisqus