Installing PHP extensions and services with PECL and Homebrew

In this blog series, I take you through the steps I took to create my new setup: a blazingly fast and flexible setup with Apache, automatic DNS setup for .test domains, multiple PHP versions that I can easily switch between, Redis, Opcache, Image Magick, Blackfire and more. Links to the other blogs in this series are at the bottom.

My development setup part 4: Installing PHP extensions and services with PECL and Homebrew

Switch to PHP 7.2

Let's start by switching to the most recent PHP version that supports all the extensions we need. At the time of writing, this is HPP 7.2.

sphp 7.2

Configure OPcache

First, well configure an OPcache. This compiles your PHP script and caches it. This increases performance.

OPcache comes with default PHP installs, so we only have to set the configuration.

We'll create a separate config file for each extension in /usr/local/etc/php/7.2/conf.d/.
For OPcache, create file /usr/local/etc/php/7.2/conf.d/ext-opcache.ini

[opcache]
zend_extension=/usr/local/opt/php@7.2/lib/php/20170718/opcache.so
opcache.memory_consumption=256
opcache.revalidate_freq=0
;opcache.validate_timestamps=0 (comment this out in your dev environment)
opcache.max_accelerated_files=7963
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1
opcache.enable_cli=0

Now restart Apache and check for a Zend OPcache section in your PHP info file.

sudo apachectl -k restart

Now, copy the file to the other PHP config directories. There is no need to change the file contents.

cd /usr/local/etc/php/7.2/conf.d
cp ext-opcache.ini ../../5.6/conf.d
cp ext-opcache.ini ../../7.0/conf.d
cp ext-opcache.ini ../../7.1/conf.d
cp ext-opcache.ini ../../7.3/conf.d

Install libYAML

Then, we'll install the libYAML library, that allows for faster YAML processing, also good for performance.

We'll install libYAML through PECL. You have to do this for every PHP version:

  • Switch to the proper version
  • Unregister the extension with PECL
  • Install the extension though PECL
# we are still at PHP 7.2
pecl install yaml

sphp 5.6
pecl uninstall -r yaml
pecl install yaml

sphp 7.0
pecl uninstall -r yaml
pecl install yaml

sphp 7.1
pecl uninstall -r yaml
pecl install yaml

sphp 7.3
pecl uninstall -r yaml
pecl install yaml

Create the extension configuration file at /usr/local/etc/php/7.2/conf.d/ext-yaml.ini

[yaml]
extension="yaml.so"

Now, copy the file to the other PHP config directories. There is no need to change the file contents.

cd /usr/local/etc/php/7.2/conf.d
cp ext-yaml.ini ../../5.6/conf.d
cp ext-yaml.ini ../../7.0/conf.d
cp ext-yaml.ini ../../7.1/conf.d
cp ext-yaml.ini ../../7.3/conf.d

Install Xdebug

To be able to debug local code, we'll install Xdebug next. It can be used for debugging PHP code in your favorite IDE, generating code coverage reports in unit testing and other things.

sphp 5.6
pecl uninstall -r xdebug
pecl install xdebug-2.5.5

sphp 7.0
pecl uninstall -r xdebug
pecl install xdebug

sphp 7.1
pecl uninstall -r xdebug
pecl install xdebug

sphp 7.2
pecl uninstall -r xdebug
pecl install xdebug

sphp 7.3
pecl uninstall -r xdebug
pecl install xdebug-2.7.0beta1

Create the extension configuration file at /usr/local/etc/php/7.2/conf.d/ext-xdebug.ini

[xdebug]
zend_extension="xdebug.so"
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_handler=dbgp
xdebug.remote_port=9000

Now, copy the file to the other PHP config directories. There is no need to change the file contents.

cd /usr/local/etc/php/7.2/conf.d
cp ext-xdebug.ini ../../5.6/conf.d
cp ext-xdebug.ini ../../7.0/conf.d
cp ext-xdebug.ini ../../7.1/conf.d
cp ext-xdebug.ini ../../7.3/conf.d

Xdebug can be rather resource-heavy, so we need to be able to switch it on and off easily from the command line. Luckily, there's a tool to do just that. I have it switched off by default, and only switch it on when I need a unit test code coverage report.

# Install the Xdebug on/off switcher
curl -L https://gist.githubusercontent.com/rhukster/073a2c1270ccb2c6868e7aced92001cf/raw > /usr/local/bin/xdebug
chmod +x /usr/local/bin/xdebug

On restarting your terminal, you will have the following commands available:

 # Switch Xdebug on
 xdebug on

 # Switch Xdebug off
 xdebug off

 # view the current state of Xdebug
 xdebug

Install Redis

Redis is an in-memory key value store that is mostly used for storing cache. It is much faster than storing cache on the file system (the default behaviour for PHP) and is supported by all major PHP frameworks and yes, even Wordpress.

We'll install Redis via Homebrew.

brew install redis

Now we'll start Redis as a service. This means it will run as a background service and automaically is restarted when your Mac reboots.

brew services start redis

# Check that Redis is running
redis-cli ping

Check out these handy commands:

brew services stop redis
brew services restart redis

# View all running brew services:
brew services list

Redis is a stand alone service, rather than a PHP extension, so you do not have to create a PHP config file for it.

Install Imagemagick

Imagemagick is a widely used extension that is dramatically more efficient at image processing that PHP's GD library. It can be user for scaling, cropping, creating overlays, coloring, optimizing, et cetera. Imagemagick will not only process images much faster, but it is also memory efficient, allowing you to convert much larger images before running into time outs.

Like Redis, we'll install Image Magick with PECL.

sphp 5.6
pecl uninstall -r imagick
pecl install imagick

sphp 7.0
pecl uninstall -r imagick
pecl install imagick

sphp 7.1
pecl uninstall -r imagick
pecl install imagick

sphp 7.2
pecl uninstall -r imagick
pecl install imagick

sphp 7.3
pecl uninstall -r imagick
pecl install imagick

Create the extension configuration file at /usr/local/etc/php/7.2/conf.d/ext-imagick.ini

[imagick]
extension=imagick.so

Now, copy the file to the other PHP config directories. There is no need to change the file contents.

cd /usr/local/etc/php/7.2/conf.d
cp ext-imagick.ini ../../5.6/conf.d
cp ext-imagick.ini ../../7.0/conf.d
cp ext-imagick.ini ../../7.1/conf.d
cp ext-imagick.ini ../../7.3/conf.d

Install Sphinx Search

SPhinx Search is a search tool allowing you to do efficient indexed searches. It is oftn used as a search engine in Magento.

brew install sphinx

Much like Redis, Sphinx is a self-contained service, running separately from PHP or Apache.

Install Blackfire

With Blackfire, you can remotely debug a PHP application in rediculous detail. This will help you to target prolem areas in your appication much faster. It is a paid tool, but there is also a free versions that will get you up and running locally in no time.

To be able to use Blackfire you will need to create an account at https://blackfire.io/pricing. To try out, choose account Hack (the free account)

To actually run Blackfire, you need two things:

  1. The Blackfire probe. This is a PHP extension that collects data from PHP for analysis.
  2. A Blackfire agent. The is a browser plugin and a cli agent. You can install them both.

Download the probe for all PHP versions.

# If your PECL paths are different, find them by switching to the appropriate PHP version and run
# php -i | grep extension_dir

# Download the blackfire probes to the proper extension directories as 'blackfire.so'
cd /usr/local/lib/php/pecl/20131226
wget -o blackfire.so https://packages.blackfire.io/binaries/blackfire-php/1.24.2/blackfire-php-darwin_amd64-php-56.so
cd /usr/local/lib/php/pecl/20151012
wget -o blackfire.so https://packages.blackfire.io/binaries/blackfire-php/1.24.2/blackfire-php-darwin_amd64-php-70.so
cd /usr/local/lib/php/pecl/20160303
wget -o blackfire.so https://packages.blackfire.io/binaries/blackfire-php/1.24.2/blackfire-php-darwin_amd64-php-71.so
cd /usr/local/lib/php/pecl/20170718
wget -o blackfire.so https://packages.blackfire.io/binaries/blackfire-php/1.24.2/blackfire-php-darwin_amd64-php-72.so
cd /usr/local/lib/php/pecl/20180731
wget -o blackfire.so https://packages.blackfire.io/binaries/blackfire-php/1.24.2/blackfire-php-darwin_amd64-php-73.so

Create file /usr/local/etc/php/5.6/conf.d/ext-blackfire.ini

[blackfire]
extension=blackfire.so

Copy the file to the other ini paths.

cd /usr/local/etc/php/5.6/conf.d
cp ext-imagick.ini ../../7.0/conf.d
cp ext-imagick.ini ../../7.1/conf.d
cp ext-imagick.ini ../../7.2/conf.d
cp ext-imagick.ini ../../7.3/conf.d

Read more at https://blackfire.io/docs/up-and-running/installation

Install IonCube

IonCube is used by some agencies to obfuscate their code, making it impossible to be read or changes by others. I am not a fan of IonCube. I get th idea behind code obfuscation, but I think it is highly customer unfriendly to ship obfuscated code, not to mention the fact that runtime de-obfuscation eats extra resources. I wished I didn't need this extension, but we have one or two legacy Magento shops that use third party code that is obfuscated with Ioncube.

I would recommend you only install Ioncube for the PHP version you actually need it. For PHP 5,6 that would mean the following:

  1. Download the Ioncube installer for your system.
  2. Unpack the installer and locate the Ioncube extension for PHP 5.6: ioncubeloaderdar_5.6.so
  3. Copy that file to /usr/local/Cellar/php@5.6/5.6.39/lib/php/20131226/ioncubeloaderdar_5.6.so
  4. Create /usr/local/etc/php/5.6/conf.d/ext-ioncube.ini:
    [ioncube]
    zend_extension="ioncube_loader_dar_5.6.so"
    
  5. Restart Apache sudo apachectl -k restart

Blogs in this series:

  1. Choosing a local PHP development setup on Mojave
  2. Install and configure Apache 2 on Mojave with Homebrew
  3. Install and configure multiple PHP versions on Mojave with Homebrew
  4. Installing PHP extensions and services with PECL and Homebrew

Many thanks to the guys at getgrav.org, who wrote a great blog series on this topic: https://getgrav.org/blog/macos-mojave-apache-multiple-php-versions.