Migrating/updating from PHP 5 to PHP 7 on OS X (MacPorts)

PHP 7 logoPHP 7 was released earlier this month, time for me to upgrade my development environment to keep up to date with this new version. I had already installed PHP 5.6 using MacPorts, so this guide is about upgrading a PHP 5 installation to a PHP 7. If you need help for actually installing PHP/Apache/MySQL, read this blogpost: Installing MAMP (Mac OS X Apache MariaDB PHP) using MacPorts.

Install PHP 7

Install PHP 7, and the extensions you need:

$ sudo port install php70
$ sudo port install php70-cgi php70-gd php70-curl php70-intl php70-iconv php70-gettext php70-mbstring php70-imap php70-mcrypt php70-xmlrpc php70-mysql php70-openssl php70-sockets php70-zip php70-tidy php70-opcache php70-xsl php70-sqlite

Install the Apache handler for PHP 7:

$ sudo port install php70-apache2handler

Activating PHP 7

Once installed you must activate the handler:

$ cd /opt/local/apache2/modules
$ sudo /opt/local/apache2/bin/apxs -a -e -n php7 mod_php70.so

This may lead to two PHP modules being loaded in the Apache configuration file. If phpinfo() still uses PHP 5.6 (or any other PHP version), you need to uncomment the PHP 5 module in /opt/local/apache2/conf/httpd.conf.

#LoadModule php5_module        modules/mod_php56.so
LoadModule php7_module        modules/mod_php70.so

At the bottom of the same file, you need to add this line for PHP 7 (and possibly delete the same line for PHP 5):

Include conf/extra/mod_php70.conf

Now we have configured PHP 7 and Apache, we need to restart Apache:

$ sudo port unload apache2; \
  sudo port load apache2

or

$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart

Select PHP 7 as the current PHP version:

$ sudo port select php php70

You can verify this by executing $ sudo port select --list php:

Available versions for php:
    none
    php55
    php56
    php70 (active)

Verifying the PHP version

Verify the executable using PHP cli:

$ php -v
PHP 7.0.1 (cli) (built: Dec 29 2015 19:22:02) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies

Now create a test PHP file on your webserver and verify in the browser if Apache uses the correct PHP version:

<?php phpinfo(); ?>