Saturday, May 27, 2017

Linux: Install PHP5.6 on Debian 8 Jessie with NGINX installed.

PHP5.6 is one of the most popular scripting language that is suited for small to medium scale web application/development. So in this tutorial, we are going to install PHP5.6 on Debian 8 Jessie.



1. Check the source list. We first check the source list if we have added that source packages to install nginx. This is one of the most important step when you are installing any packages in debian if you are a newbie on this Operating system. I'm using vi for editing files, but you can also use other editing tools like pico editor or nano editor. 



vi /etc/apt/sources.list



2. Add this lines of code. If you have this code, you can skip this step. Using this code will eventually unlock most packages that we want in the debian and linux world. After adding the code at the bottom, we can now save the file. 

deb http://ftp.de.debian.org/debian/ jessie main non-free contrib
deb-src http://ftp.de.debian.org/debian/ jessie main non-free contrib


3. Update the system. Using apt-get update to update the system and also debian knows that we include the step 2.  

apt-get update


4. Install PHP. You can rename the php5-pgsql to php5-mysql if you are using mysql as the database. And you can also install many PHP modules that you want like php5-json, php5-mbstring, php5-cli, php5-fpm. 

apt-get install php5-pgsql php5 php5-fpm

Installing PHP5 with postgresql module
Installing PHP5 with mysql module
5. Make a backup of nginx config file. By using the command mv, we are actually moving the file at the same time copying to /etc/nginx/sites-available/default.bak

mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak



6. Create a new nginx config file. We can now change anything that we want on the default file because at step 5, we backed up the default file.  


vi /etc/nginx/sites-available/default





7. Configure default file to read php. Use this codes below. The interesting part here is were are instructing nginx server to read php file. The php-fpm is responsible in reading php file that you can see below code. 


server {
        listen       80;
        server_name localhost;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;

        location / {
                try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;

        location = /50x.html {
                root /var/www/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

}



7. Restart the nginx and php. By restarting the nginx and php, we want to commit our changes that we have done. 

systemctl restart nginx
systemctl restart php5-fpm


8. Create a PHP file. Take note that I'm using the root directory /var/www/html. So our file must be also in the directory  /var/www/html/

vi /var/www/html/info.php



9. Save this below PHP file. 

<?php phpinfo(); ?>


10. Test the php file in your web browser. Here we can see the version of php that we are using and all the php modules that we installed earlier. 

http://<YOUR IP ADDRESS>/info.php


No comments:

Post a Comment