How to Install PHP 7 on Ubuntu Linux 14.04 LTS


PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. Originally created by Rasmus Lerdorf in 1994, the PHP reference implementation is now produced by The PHP Group. The latest version PHP is PHP7 and it provides 2x faster performance and 50% better memory consumption than PHP version 5.6. This article explains “How to install PHP7 on Ubuntu Linux”.

Before installing PHP7, you should need to install a PPA called ondrej/php. This allows you to co-install PHP versions 5.6 and 7.0.

Configuring a PPA for co-installable PHP 5.6 + 7.0

To configure a PPA, use the following command –

$ sudo add-apt-repository ppa:ondrej/php

The sample output should be like this –

Co-installable PHP versions: PHP 5.6, PHP 7.0 and batteries included.

You can get more information about the packages at https://deb.sury.org

For PHP 5.6 use: ppa:ondrej/php5-5.6
For PHP 5.5 use: ppa:ondrej/php5
For PHP 5.4 use: ppa:ondrej/php5-oldstable

BUGS & FEATURES: This PPA now has a issue tracker: https://deb.sury.org/pages/bugreporting.html

PLEASE READ: If you like my work and want to give me a little motivation, please consider donating: https://deb.sury.org/pages/donate.html

WARNING: add-apt-repository is broken with non-UTF-8 locales, see https://github.com/oerdnj/deb.sury.org/issues/56 for workaround:

# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
.............................

To update the package index, use the following command –

$ sudo apt-get update

The sample output should be like this –

Ign http://dl.google.com stable InRelease
Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
Ign http://extras.ubuntu.com trusty InRelease
Hit http://ppa.launchpad.net trusty InRelease
Ign http://in.archive.ubuntu.com trusty InRelease
Get:2 http://extras.ubuntu.com trusty Release.gpg [72 B]
Hit http://dl.google.com stable Release.gpg
Hit http://ppa.launchpad.net trusty InRelease
Hit http://dl.google.com stable Release
Hit http://extras.ubuntu.com trusty Release
Get:3 http://in.archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Hit http://dl.google.com stable/main amd64 Packages
Get:4 http://ppa.launchpad.net trusty InRelease [20.9 kB]
Hit http://extras.ubuntu.com trusty/main Sources
Get:5 http://security.ubuntu.com trusty-security/main Sources [106 kB]
Hit http://extras.ubuntu.com trusty/main amd64 Packages
Hit http://extras.ubuntu.com trusty/main i386 Packages
..................................

For listing all PHP packages, use the following command –

$sudo apt-cache search php7

The sample output should be like this –

php7.0-common - documentation, examples and common module for PHP
libapache2-mod-php7.0 - server-side, HTML-embedded scripting language (Apache 2 module)
php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary)
php7.0-cli - command-line interpreter for the PHP scripting language
php7.0-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary)
php7.0-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
libphp7.0-embed - HTML-embedded scripting language (Embedded SAPI library)
php7.0-dev - Files for PHP7.0 module development
php7.0-curl - CURL module for PHP
php7.0-enchant - Enchant module for PHP
php7.0-gd - GD module for PHP
php7.0-gmp - GMP module for PHP
php7.0-imap - IMAP module for PHP
php7.0-interbase - Interbase module for PHP
php7.0-intl - Internationalisation module for PHP
php7.0-ldap - LDAP module for PHP
php7.0-mcrypt - libmcrypt module for PHP
php7.0-readline - readline module for PHP
php7.0-odbc - ODBC module for PHP
php7.0-pgsql - PostgreSQL module for PHP
php7.0-pspell - pspell module for PHP
php7.0-recode - recode module for PHP
php7.0-snmp - SNMP module for PHP
php7.0-tidy - tidy module for PHP
php7.0-xmlrpc - XMLRPC-EPI module for PHP
php7.0-xsl - XSL module for PHP (dummy)
php7.0 - server-side, HTML-embedded scripting language (metapackage)
php7.0-json - JSON module for PHP
php-all-dev - package depending on all supported PHP development packages
php7.0-sybase - Sybase module for PHP
php7.0-sqlite3 - SQLite3 module for PHP
php7.0-mysql - MySQL module for PHP
php7.0-opcache - Zend OpCache module for PHP
php7.0-bz2 - bzip2 module for PHP
php7.0-bcmath - Bcmath module for PHP
php7.0-mbstring - MBSTRING module for PHP
php7.0-soap - SOAP module for PHP
php7.0-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP
php7.0-zip - Zip module for PHP

To install PHP7, use the following command –

$ sudo apt-get install php7.0

The sample output should be like this –

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
   libapache2-mod-php5 linux-headers-4.2.0-27 linux-headers-4.2.0-27-generic
   linux-image-4.2.0-27-generic linux-image-extra-4.2.0-27-generic
   linux-signed-image-4.2.0-27-generic
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
   libssl1.0.2 php-common php7.0-cli php7.0-common php7.0-fpm php7.0-json
   php7.0-opcache php7.0-readline
Suggested packages:
   php-pear
The following packages will be REMOVED:
   php5 php5-cli php5-readline
The following NEW packages will be installed:
   libssl1.0.2 php-common php7.0 php7.0-cli php7.0-common php7.0-fpm
   php7.0-json php7.0-opcache php7.0-readline
0 upgraded, 9 newly installed, 3 to remove and 3 not upgraded.
Need to get 4,383 kB of archives.
After this operation, 8,056 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
................................

To configure Nginx and PHP 7, use the following command –

$ sudo vi nginx.conf

Append the code as follows-

# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
   fastcgi_split_path_info ^(.+?\.php)(/.*)$;
   if (!-f $document_root$fastcgi_script_name) {
      return 404;
   }
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
   fastcgi_index index.php;
   include fastcgi_params;
}

Save and close the file, To reload the nginx server, use the following command –

$ sudo service nginx reload

The sample output should be like this –

* Reloading nginx configuration nginx [ OK ]

Before reload nginx.you should verify nginx installed or not in your linux system. if not, then install as follows-

$ sudo apt-get install -y nginx

The sample output should be like this –

Reading state information... Done
The following packages were automatically installed and are no longer required:
   libapache2-mod-php5 linux-headers-4.2.0-27 linux-headers-4.2.0-27-generic
   linux-image-4.2.0-27-generic linux-image-extra-4.2.0-27-generic
   linux-signed-image-4.2.0-27-generic
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
   nginx-common nginx-core
Suggested packages:
   fcgiwrap nginx-doc
The following NEW packages will be installed:
   nginx nginx-common nginx-core
0 upgraded, 3 newly installed, 0 to remove and 3 not upgraded.
Need to get 349 kB of archives.
After this operation, 1,297 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu/ trusty-updates/main nginx-common all
.............................

If nginx is already installed in Linux then reload nginx using the above command. To test new PHP 7 installation creates a file called test.php in /var/www/html/ directory.

test.php should be like this –

<?php
   phpinfo();
?>

Save and close the file. Run it as follows –

http://localhost/test.php

Congratulations! Now, you know “How to install PHP 7 on Ubuntu Linux 14.04 LTS”. We’ll learn more about these types of commands in our next Linux post. Keep reading!

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 22-Jan-2020

437 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements