Install PHP 7.2 on MacOS High Sierra (with homebrew)

IMPORTANT: As the “brew” transitions from “php71” to “php@7.1” notation, this article is no longer relevant. I wrote a new article, please read it instead:

Old article below:

Recently PHP releases with homebrew have changed some of the default settings. If you are upgrading to new PHP you may run into some problems. I did, so I wanted to share some of my work-arounds.

Sometimes built-in PHP is enough

MacOS High Sierra comes with a built-in PHP (7.1) and quite often that’s enough. Maybe you don’t even have to install anything. Run this in your project folder:

/usr/bin/php -S localhost:80 

If you want to use a built-in PHP, I recommend this guide on using built-in PHP / Apache.

For PHP 7.2 from homebrew

Quite a few things has changed in homebrew. PHP used to work with built-in apache, and some folder location shave changed. If you have changed config files or added virtual hosts, you should back them up now.

I recommend that you start by cleaning up some of the old packages from brew. You can see what versions are installed with brew list | grep php

brew remove --force --ignore-dependencies httpd
brew remove --force --ignore-dependencies php70-xdebug php71-xdebug
brew remove --force --ignore-dependencies php70-imagick php71-imagick
brew remove --ignore-dependencies --force php70 php71 php72

Next re-install apache and PHP:

brew install httpd php72
brew install php72 --with-httpd --with-thread-safety

It will take some time for PHP to compile. Next it’s time to check your httpd.conf file. Note that it has moved into new location at: /usr/local/etc/httpd/httpd.conf. It should already have corresponding line for LoadModule, but do double-check (remove LoadModule lines that attempt to load older verisons of PHP)

Apache is running under your user now, but if you need it to bind to port 80, then find and updateListen directive. You will also have to use sudo in front of apachectl start and brew services below.

Create a PHP file containing phpinfo(); to double-check your version and extensions. Open it in your browser.

Running Apache as a service

To start apache automatically during boot, we will need to install apache servic:

apachectl stop
# or if you use port 80,
# sudo apachectl stop
brew tap homebrew/services
brew services list

You’ll see httpd service listed here in a stopped state. You can register it to launch automatically.

For high ports such as 8080 or 8000:

brew services start httpd

or if you prefer to use port 80:

sudo brew services start httpd

Refresh your browser once again to check.

Re-install other modules

You might have removed some dependencies and it’s time to re-install them (use brew search php72 to see what’s available)

brew install php72-xdebug php72-imagick

If you run into any problems, execute php from command-line to see the errors.