How to install PHP 7 on Ubuntu Linux 14.04 LTS

PHP 7 provides 2x faster performance and 50% better memory consumption than PHP version 5.6. How do I install PHP 7 on Ubuntu Linux version 14.04 LTS server? How do I install PHP 7 with PHP-FPM in along with Nginx web server?

You need to install a PPA called ondrej/php. This allows you to co-install PHP versions 5.6 and 7.0. The latest version of PHP 7 is 7.0.5 and you will learn how to install the same on Ubuntu 14.04 LTS server.

How to install PHP 7 on Ubuntu Linux 14.04 LTS

The procedure to install and configure PHP 7 on Ubuntu 14.04 LTS is as follows:

  1. Enable ondrej/php on Ubuntu Linux 14.04 LTS
  2. Update apt cache using sudo apt-get update
  3. List all PHP 7 packages on Ubuntu using apt-cache search php7 command
  4. Install PHP 7 packages on Ubuntu by running sudo apt-get install php7.0
  5. Configure Nginx to use PHP 7 by editing the nginx.conf file

Let us see all commands in details.

Configure a PPA for co-installable PHP 5.6 + 7.0

Type the following command:
$ sudo apt-get install -y language-pack-en-base
$ sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

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

Fig.01: Configure a PPA for PHP 7.0 packages

Update the package index

To resynchronize the package index files from their sources, enter:
$ sudo apt-get update
Sample outputs:

Ign trusty InRelease
Get:1 trusty-updates InRelease [64.4 kB]
Get:2 trusty InRelease [15.5 kB]                      
Hit trusty-security InRelease                       
Hit trusty Release.gpg             
Hit trusty Release         
Get:3 trusty-updates/main amd64 Packages [696 kB]
Hit trusty-security/main amd64 Packages             
Get:4 trusty/main amd64 Packages [23.2 kB]           
Hit trusty-security/restricted amd64 Packages       
Hit trusty-security/universe amd64 Packages         
Get:5 trusty/main Translation-en [12.2 kB]            
Hit trusty-security/multiverse amd64 Packages       
Get:6 trusty-updates/restricted amd64 Packages [15.9 kB]
Hit trusty-security/main Translation-en             
Get:7 trusty-updates/universe amd64 Packages [336 kB]
Hit trusty-security/multiverse Translation-en       
Get:8 trusty-updates/multiverse amd64 Packages [13.0 kB]
Hit trusty-updates/main Translation-en               
Hit trusty-security/restricted Translation-en       
Hit trusty-updates/multiverse Translation-en         
Hit trusty-updates/restricted Translation-en         
Hit trusty-updates/universe Translation-en           
Hit trusty-security/universe Translation-en         
Hit trusty/main amd64 Packages                       
Hit trusty/restricted amd64 Packages       
Hit trusty/universe amd64 Packages
Hit trusty/multiverse amd64 Packages
Hit trusty/main Translation-en
Hit trusty/multiverse Translation-en
Hit trusty/restricted Translation-en
Hit trusty/universe Translation-en
Ign trusty/main Translation-en_US
Ign trusty/multiverse Translation-en_US
Ign trusty/restricted Translation-en_US
Ign trusty/universe Translation-en_US
Fetched 1,176 kB in 3s (326 kB/s)                  
Reading package lists... Done

List all PHP 7 packages

Type the following apt-cache command:
$ apt-cache search php7
Sample outputs:

php7.0-common - Common files for packages built from the PHP source
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-dbg - Debug symbols for PHP7.0
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
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-modules-source - PHP 7.0 modules source package
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

Install PHP 7

Type the following command to install PHP 7 along with useful php modules such as MySQL, GD, curl and so on:
$ sudo apt-get install php7.0
$ sudo apt-get install php7.0 php7.0-cli php7.0-fpm php7.0-gd php7.0-json php7.0-mysql php7.0-readline
Sample outputs:

Fig.02: Installing PHP 7.0

Configure Nginx and PHP 7

Edit your nginx.conf file:
$ sudo vi nginx.conf
$ sudo vi /etc/nginx/sites-enabled/default
Modify or append 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. Reload the nginx server:
$ sudo service nginx reload

Test new PHP 7 installation

Create a file called test.php in /var/www/html/ directory:


Save and close the file. Run it as follows:
Sample outputs:

Fig.03: PHP 7 in action

How do I upgrade or patch my php 7.x version?

Type the following two commands:
$ sudo apt-get update
$ sudo apt-get upgrade

Sample outputs:

Calculating upgrade... Done
The following packages will be upgraded:
  libapache2-mod-php7.0 libpcre3 php7.0 php7.0-cli php7.0-common php7.0-curl
  php7.0-fpm php7.0-gd php7.0-json php7.0-mysql php7.0-opcache php7.0-readline
12 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,628 kB of archives.
After this operation, 13.3 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 trusty-updates/main libpcre3 amd64 1:8.31-2ubuntu2.2 [144 kB]
Get:2 trusty/main php7.0-json amd64 [17.1 kB]
Get:3 trusty/main php7.0-opcache amd64 [76.0 kB]
Get:4 trusty/main php7.0-fpm amd64 [1,254 kB]
Get:5 trusty/main libapache2-mod-php7.0 amd64 [1,191 kB]
Get:6 trusty/main php7.0-cli amd64 [1,248 kB]
Get:7 trusty/main php7.0-common amd64 [507 kB]
Get:8 trusty/main php7.0-readline amd64 [12.2 kB]
Get:9 trusty/main php7.0 all [1,248 B]
Get:10 trusty/main php7.0-curl amd64 [26.9 kB]
Get:11 trusty/main php7.0-gd amd64 [27.0 kB]
Get:12 trusty/main php7.0-mysql amd64 [122 kB]
Fetched 4,628 kB in 5s (900 kB/s)       
(Reading database ... 18668 files and directories currently installed.)
Preparing to unpack .../libpcre3_1%3a8.31-2ubuntu2.2_amd64.deb ...
Unpacking libpcre3:amd64 (1:8.31-2ubuntu2.2) over (1:8.31-2ubuntu2.1) ...
Setting up libpcre3:amd64 (1:8.31-2ubuntu2.2) ...
Processing triggers for libc-bin (2.19-0ubuntu6.7) ...
(Reading database ... 18668 files and directories currently installed.)
Preparing to unpack .../ ...
Unpacking php7.0-json ( over ( ...
Preparing to unpack .../ ...
Unpacking php7.0-opcache ( over ( ...
Preparing to unpack .../ ...
php7.0-fpm stop/waiting
Unpacking php7.0-fpm ( over ( ...
Preparing to unpack .../ ...
Unpacking libapache2-mod-php7.0 ( over ( ...
Preparing to unpack .../ ...
Unpacking php7.0-cli ( over ( ...
Preparing to unpack .../ ...
Unpacking php7.0-common ( over ( ...
Preparing to unpack .../ ...
Unpacking php7.0-readline ( over ( ...
Preparing to unpack .../ ...
Unpacking php7.0 ( over ( ...
Preparing to unpack .../ ...
Unpacking php7.0-curl ( over ( ...
Preparing to unpack .../ ...
Unpacking php7.0-gd ( over ( ...
Preparing to unpack .../ ...
Unpacking php7.0-mysql ( over ( ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up php7.0-common ( ...
Setting up php7.0-json ( ...
Setting up php7.0-opcache ( ...
Setting up php7.0-readline ( ...
Setting up php7.0-cli ( ...
Setting up php7.0-fpm ( ...
php7.0-fpm start/running, process 9573
Setting up libapache2-mod-php7.0 ( ...
Warning: Could not load Apache 2.4 maintainer script helper.
Setting up php7.0 ( ...
Setting up php7.0-curl ( ...
Setting up php7.0-gd ( ...
Setting up php7.0-mysql ( ...
Processing triggers for libapache2-mod-php7.0 ( ...
Warning: Could not load Apache 2.4 maintainer script helper.
Processing triggers for php7.0-fpm ( ...
php7.0-fpm stop/waiting
php7.0-fpm start/running, process 11732

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.