How to install PHP 7 on Ubuntu Linux 14.04 LTS
P
HP 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.
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
OR
$ 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 http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [64.4 kB]
Get:2 http://ppa.launchpad.net trusty InRelease [15.5 kB]
Hit http://security.ubuntu.com trusty-security InRelease
Hit http://archive.ubuntu.com trusty Release.gpg
Hit http://archive.ubuntu.com trusty Release
Get:3 http://archive.ubuntu.com trusty-updates/main amd64 Packages [696 kB]
Hit http://security.ubuntu.com trusty-security/main amd64 Packages
Get:4 http://ppa.launchpad.net trusty/main amd64 Packages [23.2 kB]
Hit http://security.ubuntu.com trusty-security/restricted amd64 Packages
Hit http://security.ubuntu.com trusty-security/universe amd64 Packages
Get:5 http://ppa.launchpad.net trusty/main Translation-en [12.2 kB]
Hit http://security.ubuntu.com trusty-security/multiverse amd64 Packages
Get:6 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [15.9 kB]
Hit http://security.ubuntu.com trusty-security/main Translation-en
Get:7 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [336 kB]
Hit http://security.ubuntu.com trusty-security/multiverse Translation-en
Get:8 http://archive.ubuntu.com trusty-updates/multiverse amd64 Packages [13.0 kB]
Hit http://archive.ubuntu.com trusty-updates/main Translation-en
Hit http://security.ubuntu.com trusty-security/restricted Translation-en
Hit http://archive.ubuntu.com trusty-updates/multiverse Translation-en
Hit http://archive.ubuntu.com trusty-updates/restricted Translation-en
Hit http://archive.ubuntu.com trusty-updates/universe Translation-en
Hit http://security.ubuntu.com trusty-security/universe Translation-en
Hit http://archive.ubuntu.com trusty/main amd64 Packages
Hit http://archive.ubuntu.com trusty/restricted amd64 Packages
Hit http://archive.ubuntu.com trusty/universe amd64 Packages
Hit http://archive.ubuntu.com trusty/multiverse amd64 Packages
Hit http://archive.ubuntu.com trusty/main Translation-en
Hit http://archive.ubuntu.com trusty/multiverse Translation-en
Hit http://archive.ubuntu.com trusty/restricted Translation-en
Hit http://archive.ubuntu.com trusty/universe Translation-en
Ign http://archive.ubuntu.com trusty/main Translation-en_US
Ign http://archive.ubuntu.com trusty/multiverse Translation-en_US
Ign http://archive.ubuntu.com trusty/restricted Translation-en_US
Ign http://archive.ubuntu.com 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
OR
$ 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
OR
$ 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:
<?php
phpinfo();
?>
Save and close the file. Run it as follows:
http://your-domain/test.php
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 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libpcre3 amd64 1:8.31-2ubuntu2.2 [144 kB]
Get:2 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.0-json amd64 7.0.5-2+deb.sury.org~trusty+1 [17.1 kB]
Get:3 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.0-opcache amd64 7.0.5-2+deb.sury.org~trusty+1 [76.0 kB]
Get:4 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.0-fpm amd64 7.0.5-2+deb.sury.org~trusty+1 [1,254 kB]
Get:5 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main libapache2-mod-php7.0 amd64 7.0.5-2+deb.sury.org~trusty+1 [1,191 kB]
Get:6 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.0-cli amd64 7.0.5-2+deb.sury.org~trusty+1 [1,248 kB]
Get:7 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.0-common amd64 7.0.5-2+deb.sury.org~trusty+1 [507 kB]
Get:8 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.0-readline amd64 7.0.5-2+deb.sury.org~trusty+1 [12.2 kB]
Get:9 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.0 all 7.0.5-2+deb.sury.org~trusty+1 [1,248 B]
Get:10 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.0-curl amd64 7.0.5-2+deb.sury.org~trusty+1 [26.9 kB]
Get:11 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.0-gd amd64 7.0.5-2+deb.sury.org~trusty+1 [27.0 kB]
Get:12 http://ppa.launchpad.net/ondrej/php/ubuntu/ trusty/main php7.0-mysql amd64 7.0.5-2+deb.sury.org~trusty+1 [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 …/php7.0-json_7.0.5-2+deb.sury.org~trusty+1_amd64.deb …
Unpacking php7.0-json (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Preparing to unpack …/php7.0-opcache_7.0.5-2+deb.sury.org~trusty+1_amd64.deb …
Unpacking php7.0-opcache (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Preparing to unpack …/php7.0-fpm_7.0.5-2+deb.sury.org~trusty+1_amd64.deb …
php7.0-fpm stop/waiting
Unpacking php7.0-fpm (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Preparing to unpack …/libapache2-mod-php7.0_7.0.5-2+deb.sury.org~trusty+1_amd64.deb …
Unpacking libapache2-mod-php7.0 (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Preparing to unpack …/php7.0-cli_7.0.5-2+deb.sury.org~trusty+1_amd64.deb …
Unpacking php7.0-cli (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Preparing to unpack …/php7.0-common_7.0.5-2+deb.sury.org~trusty+1_amd64.deb …
Unpacking php7.0-common (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Preparing to unpack …/php7.0-readline_7.0.5-2+deb.sury.org~trusty+1_amd64.deb …
Unpacking php7.0-readline (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Preparing to unpack …/php7.0_7.0.5-2+deb.sury.org~trusty+1_all.deb …
Unpacking php7.0 (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Preparing to unpack …/php7.0-curl_7.0.5-2+deb.sury.org~trusty+1_amd64.deb …
Unpacking php7.0-curl (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Preparing to unpack …/php7.0-gd_7.0.5-2+deb.sury.org~trusty+1_amd64.deb …
Unpacking php7.0-gd (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Preparing to unpack …/php7.0-mysql_7.0.5-2+deb.sury.org~trusty+1_amd64.deb …
Unpacking php7.0-mysql (7.0.5-2+deb.sury.org~trusty+1) over (7.0.4-7+deb.sury.org~trusty+2) …
Processing triggers for ureadahead (0.100.0-16) …
Setting up php7.0-common (7.0.5-2+deb.sury.org~trusty+1) …
Setting up php7.0-json (7.0.5-2+deb.sury.org~trusty+1) …
Setting up php7.0-opcache (7.0.5-2+deb.sury.org~trusty+1) …
Setting up php7.0-readline (7.0.5-2+deb.sury.org~trusty+1) …
Setting up php7.0-cli (7.0.5-2+deb.sury.org~trusty+1) …
Setting up php7.0-fpm (7.0.5-2+deb.sury.org~trusty+1) …
php7.0-fpm start/running, process 9573
Setting up libapache2-mod-php7.0 (7.0.5-2+deb.sury.org~trusty+1) …
Warning: Could not load Apache 2.4 maintainer script helper.
Setting up php7.0 (7.0.5-2+deb.sury.org~trusty+1) …
Setting up php7.0-curl (7.0.5-2+deb.sury.org~trusty+1) …
Setting up php7.0-gd (7.0.5-2+deb.sury.org~trusty+1) …
Setting up php7.0-mysql (7.0.5-2+deb.sury.org~trusty+1) …
Processing triggers for libapache2-mod-php7.0 (7.0.5-2+deb.sury.org~trusty+1) …
Warning: Could not load Apache 2.4 maintainer script helper.
Processing triggers for php7.0-fpm (7.0.5-2+deb.sury.org~trusty+1) …
php7.0-fpm stop/waiting
php7.0-fpm start/running, process 11732