How to Install PHP 7 for Apache or Nginx on Ubuntu 14.04 and 14.10

Months after the stable version of PHP 7.0 was released, this could be the right time for you to think of upgrading to it from the old versions. There is always caution about upgrades...

Spread the love

Months after the stable version of PHP 7.0 was released, this could be the right time for you to think of upgrading to it from the old versions.

There is always caution about upgrades especially in a production environment, but it is a good idea now to upgrade so as to enjoy speed improvements, and also features such as scalar type hinting plus many more.

Install PH 7 in Ubuntu 14.04 and 14.10

Install PH 7 in Ubuntu 14.04 and 14.10

You can install two versions of PHP on your system and use one for testing purposes, but remember that you only enable one PHP Apache modules in a given time.

This guide focuses on upgrading from PHP 5.X, using mod_php in connection with Apache Web server or PHP-FPM in connection with Nginx Web server.

  1. Install PHP 7 in Ubuntu 14.04 and 14.10
  2. Upgrading to PHP 7.0 under Apache Web Server
  3. Upgrading to PHP 7.0 under Nginx Web Server


Now let us dive into how you can upgrade to latest version of PHP and also configure your system to use it.

How to Install PHP 7 in Ubuntu 14.04 and 14.10

First, you will have to add the PPA maintained by Ond?ej Surý for Debian and its derivatives such as Ubuntu by running the command below:

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

Next update your system as follows:

$ sudo apt-get update

All is now set, and you can install PHP 7.0, but we shall look at upgrade for Apache and Nginx in different sections.

Upgrading to PHP 7.0 under Apache Web Server

This section is for systems running Apache, where PHP code is executed using mod_php module. Install the latest PHP version as running the command below:

$ sudo apt-get install php7.0
Sample Output
Reading package lists... Done
Building dependency tree Reading state information... Done
The following extra packages will be installed:
libapache2-mod-php7.0 libssl1.0.2 php-common php7.0-cli php7.0-common
php7.0-json php7.0-opcache php7.0-readline
Suggested packages:
php-pear
The following NEW packages will be installed:
libapache2-mod-php7.0 libssl1.0.2 php-common php7.0 php7.0-cli php7.0-common
php7.0-json php7.0-opcache php7.0-readline
0 upgraded, 9 newly installed, 0 to remove and 80 not upgraded.
Need to get 4,371 kB of archives.
After this operation, 17.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

PHP has now been upgraded on your system, but if you are using MySQL database management system, then you will have to execute the following command to update the PHP-MySQL binding and also you will need to install some useful modules such as Curl, GD, Cli, JSON, etc.

$ sudo apt-get install php7.0-mysql php7.0-cli php7.0-gd php7.0-json 

If you want to install additional PHP7.0 modules, you can use apt-cache command to list all PHP7.0 modules and install.

$ sudo apt-cache search php7
Sample Output
php-radius - radius client library for PHP
php-http - PECL HTTP module for PHP Extended HTTP Support
php-uploadprogress - file upload progress tracking extension for PHP
php-mongodb - MongoDB driver for PHP
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
php-apcu - APC User Cache for PHP
php-xdebug - Xdebug Module for PHP
php-imagick - Provides a wrapper to the ImageMagick library
php-ssh2 - Bindings for the libssh2 library
php-redis - PHP extension for interfacing with Redis
php-memcached - memcached extension module for PHP, uses libmemcached
php-apcu-bc - APCu Backwards Compatibility Module
php-amqp - AMQP extension for PHP
php7.0-bz2 - bzip2 module for PHP
php-rrd - PHP bindings to rrd tool system
php-uuid - PHP UUID extension
php-memcache - memcache extension module for PHP
php-gmagick - Provides a wrapper to the GraphicsMagick library
php-smbclient - PHP wrapper for libsmbclient
php-zmq - ZeroMQ messaging bindings for PHP
php-igbinary - igbinary PHP serializer
php-msgpack - PHP extension for interfacing with MessagePack
php-geoip - GeoIP 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
php-tideways - Tideways PHP Profiler Extension
php-yac - YAC (Yet Another Cache) for PHP
php-mailparse - Email message manipulation for PHP
php-oauth - OAuth 1.0 consumer and provider extension
php-propro - propro module for PHP
php-raphf - raphf module for PHP
php-solr - PHP extension for communicating with Apache Solr server
php-stomp - Streaming Text Oriented Messaging Protocol (STOMP) client module for PHP
php-gearman - PHP wrapper to libgearman
php7.0-dba - DBA module for PHP

Once PHP7.0 and its modules installed, you can restart your Apache web server and verify the PHP version as shown:

$ sudo service apache2 restart
$ php -v
Sample Output
PHP 7.0.7-1+donate.sury.org~trusty+1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

You can also verify PHP7 information by creating a info.php file under /var/www/html directory.

$ sudo vi /var/www/html/info.php

Place following code and access the page via http://server_IP-address/info.php.

<?php
phpinfo();
?>
Check PHP 7 Information for Apache

Check PHP 7 Information for Apache

Upgrading to PHP 7.0 under Nginx Web Server

This section takes you through the process of upgrading to PHP7.0 and updating PHP-FPM with Nginx Web server, where PHP code is executed using PHP-FPM.

Run the command below to install the latest PHP-FPM packages:

$ sudo apt-get install php7.0
$ sudo apt-get install php7.0-fpm

PHP has now been upgraded, but if you are using MySQL, then you will have to execute the following command to update the PHP-MySQL binding and some additional modules as shown:

$ sudo apt-get install php7.0-mysql php7.0-cli php7.0-gd php7.0-json 

Next, you need to append the fastcgi_pass directive in the file /etc/nginx/sites-enabled/default or all files for your virtual sites that have to use and support PHP, since the path of the PHP-FPM socket file that PHP uses to communicate with Nginx has changed.

Use your favorite editor and open the file for editing as follows:

$ sudo vi /etc/nginx/sites-enabled/default 

Modify or append as follows:

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;
}

Then restart Nginx and php-fpm as follows:

$ sudo service nginx restart
$ sudo service php7.0-fpm restart

Lastly, you can test whether PHP is working or not by first checking the your PHP version and then testing it with the Web server.

$ php -v

You get information about your PHP packages by writing a small info.php file under /usr/share/nginx/html/ directory:

$ sudo vi /usr/share/nginx/html/info.php 

Put this code on your info.php file:

<?php
phpinfo();
?>

Save and exit the file.

Open your web browser, enter http://server_IP-address/info.php and you should be able to see the page below which shows you details about your PHP package.

Check PHP 7 Information

Check PHP 7 Information

You can now happily use PHP 7.0 on your Ubuntu 14.04/14.10 system, and I hope you find this guide helpful.

For any additional information concerning upgrading PHP or questions, your comments are welcome in the comment section below.

Facebook Comments
Spread the love

Posted by News Monkey