Deploying Nginx + php5-fpm on Ubuntu 16.04

Hello in this tutorial im going to show you how you can deploy Nginx (latest) + php5-fpm at your local computer. You can find a lot of tutorials in web how to deploy and run this at any server, but there are not enough info how to deploy this at localhost.
I had before on my local computer installed Ubuntu 15.10 with apache2 and php5 so that was enough for me to make any kind of web development at home. After i did upgrades of core to 16.04 LTS my localhost of course brokes. In the new Ubuntu they start to use php7 from default and of course an errors apear when MODx 2.4 Revo tried to load.
So i decided to switch to nginx and php5-fpm. It was a good start point :).
My local MySQL database remaining untouchable, so we will be working JUST on deploying web server.

1. So the first thing what we need to do is to remove out any php7 files:

Open your terminal CTRL + ALT + T

sudo apt-get remove php7

sudo apt-get purge php7

sudo apt-get autoremove

sudo apt-get autoclean

2. Install add-apt-repository:

sudo apt-get install python-software-properties

Add repository for PHP 5.6:

sudo add-apt-repository -y ppa:ondrej/php5-5.6

Replace xenial with wily in sources.list: this will make sure packages for wily are used.

sudo sed -i.bak"s/xenial/wily/g" /etc/apt/sources.list.d/ondrej-ubuntu-php5-5_6-xenial.list

Update package lists:

sudo apt-get update

Install php5-fpm:

sudo apt-get install php5-fpm

And the result is:

root@ubuntu-512mb-fra1-01:~# php -v
PHP5.6.20-1+deb.sury.org~wily+1(cli)
Copyright(c)1997-2016The PHP Group
Zend Engine v2.6.0,Copyright(c)1998-2016Zend Technologies
with Zend OPcache v7.0.6-dev,Copyright(c)1999-2016,by Zend Technologies

2.1 We need to do some changes at php.ini file

Find the line ‘cgi.fix_pathinfo=1′, uncomment it and change the value from 1 to 0.

3. Next Step we need to install nginx:

sudo apt-get install nginx

Start Nginx service using the following command.

On Ubuntu 16.04:

sudo /etc/init.d/nginx restart

OK

3. Test it :

nginx just deployed

4. Now we need to set the config files right.

we need to open as administrator default config file of site.

i use Gedit to edit them:

sudo gedit /etc/nginx/sites-available/default

Here what i've got on my one computer and it works for me:

file: /etc/nginx/sites-available/default:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www;
    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;
    server_name _;
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny all;
    #}
}


Save.


Restart server by comand:

sudo /etc/init.d/nginx restart

And voila.

Now you can access your files in root folder without doing any other config files in nginx folder.

http://localhost/~site-folder~

Tags: Nginx,php5-fpm,Ubuntu 16.04,deploying,localhost
What else?
    Soon here will be more posts