Upgrade to PHP 7

PHP 7 is released after 10 years of PHP 5 released.  It’s a major release and it has a lot of improvements compares to PHP 6. Eventually, developer community did not accept PHP 6 that much.  If you haven’t upgraded PHP  environment, I would recommend upgrading its version as soon as possible to get better performance. Based on benchmark it has a huge performance improvement.

Sneak Peak of PHP 7

  • Twice faster than PHP 5.6
  • Less Memory consumption
  • New syntax such as group import, combined comparison operator etc.
  • Scalar Type Hints
  • Return Type

Now, I will just explain how you easily can upgrade to the latest version of PHP 7 from version in your Linux environment.  I showed all the commands based on Ubuntu Environment.

Just to let you know PHP 7 is still not packaged in Ubuntu official repository. So, you will have to use third party sources. Ondřej Surý maintains and offers PPA packages for the PHP 7.0. At first add Ondřej’s PPA to the system’s Apt sources:

sudo add-apt-repository ppa:ondrej/php

You will see a description and followed by a prompt to continue. Simply press Enter to accept it.

Update you local package cache

sudo apt-get update

For Nginx server with PHP-FPM:

sudo apt-get install php7.0-cli
sudo apt-get install php7.0-fpm 
sudo apt-get install php7.0-mysql 
 
#optional 
sudo apt-get install php7.0-curl php7.0-gd php7.0-intl php7.0-xml php7.0-xmlrpc php7.0-mcrypt
 
#php-memcached (optional)
sudo apt-get install -y php-memcached
 
##might be required for php fb-sdk, stripe payment gateway and other libraries  
sudo apt-get install php7.0-mbstring php7.0-zip

You will also have to update the unix socket path at default configuration as well as other configurations. Wherever you used the following socket path:

/var/run/php5-fpm.sock

Replace it with

/var/run/php/php7.0-fpm.sock

Restart the server:

sudo service nginx restart

Example:

Open your default configuration located at /etc/nginx/sites-enabled/default.  Edit with this one by nano or vim

sudo nano /etc/nginx/sites-enabled/default

Change the line: fastcgi_pass unix:/var/run/php/php5-fpm.sock with fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
After changing your configuration will be look like as follows:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
 
    root /var/www/html;
    index index.php index.html index.htm;
 
    server_name 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;
    }
}

For Apache 2 server with mod_php:

sudo apt-get install php7. 0-Clio
sudo apt-get install php7.0
sudo apt-get install php7.0-mysql

Restart the server

sudo service apache restart

PHP 7.0 configuration files are now available at /etc/php/7.0.
Check PHP 7 installation, type following command:

php -v

You will see the following output:

PHP 7.0.0-5+deb.sury.org~trusty+1 (cli) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

You are good to go with new version. Now, you can remove PHP 5 (old versions).
For Nginx:

sudo apt-get purge php5-fpm && apt-get --purge autoremove

For Apach 2:

sudo apt-get purge php5 && apt-get --purge autoremove

Happy Hacking 🙂

 

Eftakhairul Islam

Hi, I'm Eftakhairul Islam, a passionate Software Engineer, Hacker and Open Source Enthusiast. I enjoy writing about technical things, work in a couple of startup as a technical advisor and in my spare time, I contribute a lot of open source projects.

 

One thought on “Upgrade to PHP 7

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Read previous post:
Getting started with Go with the GVM and some other important tools

Yeah.. I have to switch to GO AKA Golang to write a little app for solving a scalability issue. Go...

Close