Upgrading to PHP 7 on OS X

This week, PHP 7 was released, and the upgrade process is fairly simple. It's also possible to have two (or more) versions of PHP installed, assuming you use Brew, which makes it very easy to test that code works in PHP 7.


Installation steps

  • First, do a brew update, just to make sure you've got the latest build of PHP 7. (The RC versions were also available via Brew)
  • If you have installed previous versions of PHP with Brew, you will need to unlink the previous version, for example for 5.6 you can use brew unlink php56. This seems to only affect phar, if this is something you use, you may have to use brew switch whenever switching versions
  • If you have never installed PHP using brew before, you can ignore that previous step
  • Install 7.0: brew install php70
  • Edit the Apache config (/etc/apache2/httpd.conf), to insert the lines outputted by brew:

For example:

LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so <FilesMatch .php$> SetHandler application/x-httpd-php </FilesMatch>

  • Comment out any other PHP modules (don't delete them: this makes it easier to switch versions)

For example:

#LoadModule php5_module libexec/apache2/libphp5.so #LoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5.so LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so

  • Restart apache: sudo apachectl restart
  • Edit (or create) ~/.bashrc

I use the following code, which makes switching between versions easier:

#export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH" export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"

  • Close and reopen your terminal, so that it is using the correct version
  • Confirm you're running 7.0 by running php -v, to check Apache is using the correct module, you can create a php file with phpinfo() in /Library/WebServer/Documents/ and visithttp://localhost on your local machine

To switch back to previous versions, you only need to edit the Apache config and restart Apache. You can also use the export command to switch between versions for the commandline.