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

You will see like following.


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

it will be ilke

# pass the PHP scripts to FastCGI server listening on
            location ~ .php$ {

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

                    # With php5-cgi alone:
                    # 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

$cd vi /etc/nginx/sites-available/

add like below

server {
        listen 80;
        index index.html index.php;
        root /var/www/domain/;

add symbolic link to start the virtual host

$sudo ln -s /etc/nginx/sites-available/  /etc/nginx/sites-enabled/

create path for virtual host

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

add some text on index.html

<p>hello world</p>

restart the server

$sudo /etc/init.d/nginx restart


read at

comments powered byDisqus