Tuesday, June 26, 2018

Homebrew Made PHP Harder on OS X

With Brew version 1.5, Homebrew announced they were deprecating the PHP tap. There blog post about it seems to understate what really happened. They have migrated the formulas for PHP installation to ONLY install a standard version of PHP with "the most common extensions". If other extensions are required, they have to be installed using Pecl, Pear or compiled manually.

This reminds me of the days when I had to fix dependencies and compile everything. After pulling my hair out for a few hours and reverting the entire local directory twice, I decided to relax and figure out what I needed to do to upgrade to PHP 7.1 (finally).

A month before the announcement of Brew 1.5 I had installed PHP 7.1 on my MacBook with great success including all the additional extensions that I needed for development. When I tried that same thing on my desktop, Brew updated itself, migrated php56 to php@5.6 and I fell into the rabbit hole.

My current projects require the following extensions:

  • mbstring
  • gd
  • memcached
  • xdebug
  • mailparse

The "standard" PHP came with MbString and GD. Memcached and Mailparse were the main problems not to mention I consider Xdebug an absolute requirement for any development environment.


Before installing memcached, pkg-config has to be installed with
brew install pkg-config

To actually install memcache, the package manager PECL is used.
pecl install memcached

But it asks for the libmemcached directory. After several attempts and some Googling, I found that providing the actual path to the library was not correct. Just supply the path to Brew's working directory.
As the comment from mkoppanen states, the supplied directory is just a prefix for the test to find memcached.h.

Pecl states that it adds the extension to PHP but this is not true. To accomplish this I added a file to the conf.d directory to load the extension (for both FPM and CLI)

File: /usr/local/etc/php/7.1/conf.d/20-memcached.ini


Relative to memcache, mailparse ended up being easier. Again use PECL.
pecl install mailparse

And again PECL states that it adds the extension but this is still not true. It requires manually linking it to PHP.

File: /usr/local/etc/php/7.1/conf.d/20-mailparse.ini


The only difference with Xdebug is that it's a Zend extension so the linking is different.
pecl install xdebug

With the extension getting loaded into PHP with a ini file in the conf.d directory.

File: /usr/local/etc/php/7.1/conf.d/10-xdebug.ini
[xdebug] zend_extension="/usr/local/Cellar/php@7.1/7.1.19/pecl/20160303/xdebug.so"

The location of the ini files linking these extensions to PHP is the same as what Brew does as seen with the OPcache extension.

Also since I'm running Nginx not Apache, some changes to PHP-FPM had to happen to make things work. To alleviate permission problems, the user and group are set to me. Also the listen mode has to be set to read/write by everyone so Nginx can connect correctly.

user = davidfairbanks
group = staff
listen.mode = 0666

While all of this is a pain to stop working and figure it out, it's still better than having Apple completely wipe my development environment when updates comes out. Not to mention easier than having to compile everything from source code which is what I used to do.

My local development environment

  • OS X Sierra
  • PHP 7.1
  • Nginx 1.12.2
  • MariaDB 10.3.7
  • Memcached
  • Beanstalkd
  • Node 8.11.2