Upgrading PHP version on Ubuntu 16.04

Share:
Laravel interview questions and answers


PHP 7, which was released on December 3, 2015, promises substantial speed improvements over previous versions of the PHP and comes with new features like scalar type hinting,new operators like Null Coalesce Operator and Spaceship Operator,Enhanced Error Handling interfaces or classes.
In this blog post we are going to explain how to upgrade an Apache or Nginx web server running PHP 5.x  to PHP 7.
Step1. Adding PPA for PHP 7 Packages(Please Note  PPA is already installed on Ubuntu 16.04 you can skip this step)
Personal Package Archive, or PPA, is an Apt repository hosted on Launchpad server. PPA's allow third-party developers to build and distribute packages for Ubuntu Servers.
Type below command on terminal to add PPA repository. 

sudo add-apt-repository ppa:ondrej/php

You'll see a description of the PPA (Personal Package Archive), followed by a prompt to continue installation. Press Enter to proceed.
Note: If your system's locale is set to anything else than UTF-8, adding the PPA may fail due to a bug handling characters in author's name of package. As a workaround, you can install language-pack-en-base to make sure that locales are generated, and override system-wide locale settings while adding the PPA:


sudo apt-get install -y language-pack-en-base
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
Once the PPA is installed, update the local package cache to include its contents:

sudo apt-get update
Now we have access to packages required for PHP 7 and we can replace the existing PHP installation.
Step2. Upgarding mod_php with Apache 
This section describes the upgrade process for a system using Apache as the web server and mod_php to execute PHP code. If, instead, you are running Nginx and PHP-FPM, skip ahead to the next section.
First, install the new packages. This will upgrade all of the important PHP packages, with the exception of php5-mysql, which will be removed.

sudo apt-get install php7.0
Note: If you have made substantial modifications to any configuration files in /etc/php5/, those files are still in place, and can be referenced. Configuration files for PHP 7.0 now live in /etc/php/7.0.
If you are using MySQL, make sure to re-add the updated PHP MySQL bindings:

sudo apt-get install php7.0-mysql

 Step2. Upgarding PHP-FPM with Nginx Webserver
This section describes the upgrade process for a system using Nginx as the web server and PHP-FPM to execute PHP code.
First, install the new PHP-FPM package and its dependencies:

sudo apt-get install php7.0-fpm
You'll be prompted to continue. Press Enter to complete the installation.
If you are using MySQL, be sure to re-install the PHP MySQL bindings:

sudo apt-get install php7.0-mysql
Note: If you have made substantial modifications to any configuration files in /etc/php5/, those files are still in place, and can be referenced. Configuration files for PHP 7.0 now live in /etc/php/7.0.

Updating Nginx Site(s) to Use New Socket Path

Nginx communicates with PHP-FPM using a Unix domain socket. Sockets map to a path on the filesystem, and our PHP 7 installation uses a new path by default:
PHP 5 PHP 7
/var/run/php5-fpm.sock /var/run/php/php7.0-fpm.sock
Open the default site configuration file with nano (or your editor of choice):
sudo nano /etc/nginx/sites-enabled/default

Your configuration may differ somewhat. Look for a block beginning with location ~ \.php$ {, and a line that looks something like fastcgi_pass unix:/var/run/php5-fpm.sock;. Change this to use unix:/var/run/php/php7.0-fpm.sock.
/etc/nginx/sites-enabled/default
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html;
    index index.php index.html index.htm;

    server_name server_domain_name_or_IP;

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

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
Exit and save the file. In nano, you can accomplish this by pressing Ctrl-X to exit, y to confirm, and Enter to confirm the filename to overwrite.
You should repeat this process for any other virtual sites defined in /etc/nginx/sites-enabled which need to support PHP.
Now we can restart nginx:
sudo service nginx restart
Step3. Testing PHP 
With a web server configured and the new packages installed, we should be able to verify that PHP is up and running. Begin by checking the installed version of PHP at the command line:
php -v
Output
Laravel interview questions and answers
Top Laravel Interview questions and answers for Fresher and Experienced

No comments