PHP 7 on Linux Mint and Ubuntu

Submitted by peter on Sun, 11/05/2017 - 07:18

PHP 7 has arrived but may not be on your favourite operating system. Here is a way to put PHP 7 on Linux Mint and Ubuntu.

Update to PHP version 5.5 first

PHP 5 is currently at PHP 5.6. PHP 7 is compatible with PHP 5.6 and PHP 5.5. If you are on a version of PHP older than PHP 5.5, update to PHP 5.5 or 5.6 first. Adjust any code that is not compatible with PHP 5.5.

After everything is tested with PHP 5.5, upgrade to PHP 7. Test everything with PHP 7. The PHP 5.5 code should work in PHP 7.

You can leave PHP 5.6 in place

After converting everything to PHP 7, you can delete the older versions of PHP then start upgrading code to use the improved features of PHP 7. You do not have to delete PHP 5.5 or 5.6 immediately. PHP 5 is in a different directory. You can switch back at any time by changing some configuration links.

If you do keep PHP 5, make sure you update PHP 5.5 to 5.6.

Your code will run faster with or without changes

PHP 7 will run all code faster than PHP 5. Some code will be twice as fast. You can get further speed improvements by altering the code to use type hints, one of the big new code improvement in PHP 7.

Type hints tell the PHP compiler how to handle data without generating all the type detection overhead needed for PHP 5. You use type hints on the most commonly used variables to improve performance. Eventually you might rewrite all your code to use type hints.

PHP 7 is easier to learn if you know Javascript

PHP 7 lets you use a simpler way of defining arrays. The PHP 7 array syntax is optional and one day will be standard. The new syntax is already used by a few languages including Javascript. You will find it easier to switch between PHP 7 and Javascript when you use the PHP 7 array syntax.

How do you find the current PHP version?

PHP can run in a Web server or as a command line program. The two configurations are separate which means you could run two different versions without knowing you are running two versions.

You can display the PHP version used by the Web server by placing <?php phpinfo(); ?> in any Web page.

You can display the command line version by typing php -v in the Linux/Unix DOS/command box.

In one of your test Web sites, create a Web page named phpinfo.php containing <?php phpinfo(); ?>. Open the Web site and select the page. At the top of the page, you will see something like the following line.

PHP Version 5.6.17-3+deb.sury.org~trusty+1

Open the Linux/Unix DOS/command box. Select MenuTerminal. In the Terminal box, type php -v and press enter. The following text is displayed by php -v after the PHP 7 upgrade.

PHP 7.0.2-4+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

How do you find the PHP configuration?

This procedure assumes you use Apache. If you use any other Web server, there will be an equivalent for that Web server.

Open the file manager. MenuFiles. In Linux Mint, the file manager program is named Nemo. Ubuntu uses Nautilus.

Select File System then directory etc then directory apache2 then directory mods-enabled then file php5.load. The file contain the following line.

LoadModule php5_module /usr/lib/apache2/modules/libphp5.so

In the same directory, open file php5.conf. The file contain the following lines. Some lines might be different if you have not upgraded to Apache 2.4.

<FilesMatch ".+\.ph(p[345]?|t|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
    SetHandler application/x-httpd-php-source
    # Deny access to raw php sources by default
    # To re-enable it's recommended to enable access to the files
    # only in specific virtual host or directory
    Require all denied
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(p[345]?|t|tml|ps)$">
    Require all denied
</FilesMatch>

# Running PHP scripts in user directories is disabled by default
# 
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>

Select File System then directory etc then directory php5. Browse the files in the directory and sub directories. Note there is one child directory named apache2 and other named cli. The cli directory defines the PHP used in the Terminal/command line box. The apache2 directory defines the PHP used by your Apache Web server.

How do you add PHP 7 while retaining PHP 5.6?

Start the Linux Mint Update Manager or equivalent.
MenuAdministrationUpdate Manager

In Update Manager, select Software sources.
EditSoftware sources

Enter your password if requested.

In Software sources, select PPAs.

In PPAs, select Add a new PPA.

In the box displaying "Please enter the name of the PPA you want to add:", type ppa:ondrej/php then select OK.

A button labelled Update the cache will appear in the top right corner of the Software sources screen. Select the Update the cache button.

The cache update will take a few seconds or minutes depending on your connection. You should then see an update item for PHP or PHP 7 or PHP PEAR. Install the update.

Close the Update Manager then check the PHP version using the phpinfo() function or the php -v command.

For more details about the PPA, read https://launchpad.net/~ondrej/+archive/ubuntu/php.

PHP CLI is 7.0 but not Apache

In the file manager, select File System then directory etc then directory php. Browse the files in the directory and sub directories. Notice there is one child directory named 7.0. The 7.0 directory defines the use of PHP 7. There is a directory named cli in directory 7.0. This makes PHP 7 the version you see in the Terminal/command line box.

There is no apache2 directory. We need another step to make PHP 7 run in your Web server.

Open the Synaptic Package Manager. Select MenuAdministrationSynaptic Package Manager. In the search box, type php7. The following list is displayed by the Synaptic Package Manager.

libapache2-mod-php7.0 is not installed. Select libapache2-mod-php7.0 then right click and select Mark for Installation.

Select Apply in the Synaptic Package Manager menu.

Remember the Apache configuration line containing LoadModule php5_module /usr/lib/apache2/modules/libphp5.so? We now have /usr/lib/apache2/modules/libphp7.0.so.

Select File System then directory etc then directory apache2 then directory mods-available. You will find files named php5.conf, php5.load, php7.0.conf, and php7.0.load.

We want links to the PHP 7 files in directory mods-enabled.

  1. Place links to php7.0.conf and php7.0.load in directory mods-enabled
  2. Delete php5.conf and php5.load from directory mods-enabled
  3. Restart Apache

Open Terminal. Type in sudo nemo and press enter. Supply your password. sudo provides administrator access to the Nemo file manager.

Select File System then directory etc then directory apache2 then directory mods-available.

Select php7.0.conf. In the file manager menu, select Edit then Make Link. Ctrl+M is a shortcut to Make Link. The link is named Link to php7.0.conf.

Repeat the Make Link step for php7.0.load.

Cut Link to php7.0.conf and Link to php7.0.load then paste them into directory mods-enabled. Rename Link to php7.0.conf to php7.0.conf and Link to php7.0.load to php7.0.load.

Delete files php5.conf and php5.load from directory mods-enabled.

Restart Apache by typing sudo service apache2 restart into Terminal. Supply your password.

Check PHP 7 is running in Apache by refreshing the Web page containing phpinfo().

Is everything installed with PHP 7?

PHP 7 contains a different set of add-on modules included depending on who compiled PHP. There are also extra add-on modules defined in the configuration files fount in etc/php5 and etc/php/7.0. You can see the available options in the phpinfo() display described earlier.

The phpinfo() lists the following modules as included in the installation. bcmath, calendar, Core, ctype, date, dba, dom, exif, fileinfo, filter, ftp, gettext, hash, iconv, libxml, mbstring, mysqlnd, openssl, pcre, PDO, Phar, posix, readline, Reflection, session, SimpleXML, soap, sockets, SPL, standard, sysvmsg, sysvsem, sysvshm, tokenizer, xml, xmlreader, xmlwriter, Zend OPcache, zip, and zlib.

Conclusion

The upgrade to PHP 7 is easy and effective when you are already on PHP 5.5 or 5.6. Upgrade to 5.6 first and test all your code before leaping into PHP 7.