Install and configure multiple PHP versions on Mojave with 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 3: Install and configure multiple PHP versions on Mojave with Homebrew

Uninstall any deprecated Homebrew PHP versions and extensions

Before March 2018, Homebrew installed PHP as a Homebrew package. As of March 2018, PHP is part of the homebrew core. If yu have any PHP versions installed from through homebrew from before March 2018, make sure to remove them.

brew update
brew upgrade
brew cleanup

# Check if you have old deprecated Homebrew PHP versions and extensions installed
brew list | grep php

# Uninstall deprecated Homebrew PHP
brew uninstall --force php56 php56-apcu php56-opcache php56-xdebug php56-yaml
# brew uninstall --force php70 php70-apcu php70-opcache php70-xdebug php70-yaml
# Etc.

# Check that no deprecated Homebrew PHP packages are left
brew cleanup
brew list | grep php

# Remove all old PHP configurations
rm -Rf /usr/local/etc/php/*

Install multiple PHP versions through Homebrew

Bu default, Homebrew only supports official non-deprecated PHP versions. If you need to install a PHP version that is already end of life, like PHP 7.0, make sure to run this tap first:

brew tap exolnet/homebrew-deprecated

Now, install all PHP versions you need. Later, we will install a command line tool to easily witch between versions.

brew install php@5.6
brew install php@7.0
brew install php@7.1
brew install php@7.2
brew install php@7.3

Now first close your terminal windows so your .bash_profile is reloaded.

Check the latest installed PHP version is actually running.

php -v
# Shoud return PHP 7.3.0 (cli)...

Now we need to load all PHP versions as Apache modules. Open file /usr/local/etc/httpd/httpd.conf and add a LoadModule statement for every PHP version you have installed under the existing LoadModule lines. Uncomment the currently running version and comment out the other versions.

#LoadModule php5_module /usr/local/opt/php@5.6/lib/httpd/modules/libphp5.so
#LoadModule php7_module /usr/local/opt/php@7.0/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.1/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so
LoadModule php7_module /usr/local/opt/php@7.3/lib/httpd/modules/libphp7.so

Now, add index.php as the default file to load in a directory. Search:

<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

And replace with:

<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

Now restart Apache.

sudo apachectl -k stop
sudo apachectl start

To quickly check your local PHP install, create an index.php file inside your localhost webroot (in my case Users/joostvanveen/htdoc) and make it echo your phpinfo:

<?php phpinfo();

Now, browse to http://localhost and you should see the PHP info page fo your current PHP install.

Switch between Homebrew PHP versions from the command line

Andy Miller has written a handy script that we'll use to easily switch between PHP versions from the command line.

# Download the switcher script to the proper destination
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw > /usr/local/bin/sphp

# Make the switcher script writeable
chmod +x /usr/local/bin/sphp

Check if the paths /usr/local/bin and /usr/local/sbin have been added to your bash profile file (~/.profile, ~/.bash_profile, or ~/.zshrc).

echo $PATH

If they have not, you need to add them manually.

Now, you can switch between PHP versions from the command line. You can check PHP info at http://localhost.

sphp 7.2
sphp 7.1
# Etc.

Upgrading PHP with Homebrew

When you want to update the currently active PHP version through Homebrew, do:

brew update
brew upgrade

To upgrade other PHP versions, first switch and then repeat the upgrade process.

In the next episode we will install the necessary and PHP extensions.

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