4.1 Install multiple PHP versions

Install PHP 5.5, PHP 5.6, PHP 7.0, and PHP 7.1

For each vesion of PHP you can get a full list of available options to include during the install by typing brew options php55

In this example we are just including Apache support via --with-apache to build the required PHP modules for Apache.

brew install php55 --with-apache
brew unlink php55
brew install php56 --with-apache
brew unlink php56
brew install php70 --with-apache
brew unlink php70
brew install php71 --with-apache

This may take some time as your computer is actually compiling PHP from source.

Note: You must reinstall each PHP version with reinstall command rather than install if you have previously installed that PHP version through Brew.

4.2 Configuration in php.ini

The php.ini files for each version of PHP are located in the following directories:

cd /usr/local/etc/php/5.5/php.ini
cd /usr/local/etc/php/5.6/php.ini
cd /usr/local/etc/php/7.0/php.ini
cd /usr/local/etc/php/7.1/php.ini

To make it easier to find and edit these files create a bash alias in .bashrc to open the folder containing them in your favorite editor

# Edit php in the "php.ini" file
alias config_php="sublime /usr/local/etc/php"

Open each file and check/edit the date.timezone configuration.

; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/Vancouver

and the memory setting

Edit the memory_limit parameter in the php.ini file (usually in a section called Resource Limits). Make sure you use M to specify the number of megabytes (not MB). memory_limit = 64M ; Maximum amount of memory a script may consume (64MB) If there is no section already for this, place the above line at the end of the file.

For Drupal 256M is a reasonable starting point

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 256M

Note: PHP 7.1 is currently in a Release Candidate state at the time of this writing, but it's expected to be released before the end of 2016. You can install as many or as few PHP versions as you like, it's nice to have options right?

4.3 Apache PHP Setup - Part 1

You have successfully installed your PHP versions, but we need to tell Apache to use them.

Open the /usr/local/etc/apache2/2.4/httpd.conf file and search for #LoadModule php5_module.

You will notice that each PHP version added a LoadModule entry, however these are all pointing to very specific versions. We can replace these with some more generic paths (exact versions may differ):

LoadModule php5module /usr/local/Cellar/php55/5.5.3811/libexec/apache2/libphp5.so
LoadModule php5module /usr/local/Cellar/php56/5.6.263/libexec/apache2/libphp5.so
LoadModule php7module /usr/local/Cellar/php70/7.0.115/libexec/apache2/libphp7.so
LoadModule php7module /usr/local/Cellar/php71/7.1.0-rc.38/libexec/apache2/libphp7.so

Modify the paths as follows:

#LoadModule php5_module    /usr/local/opt/php55/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
#LoadModule php7_module    /usr/local/opt/php71/libexec/apache2/libphp7.so

We can only have one module processing PHP at a time, so for now, comment out all but the php56 entry:

# LoadModule php5_module    /usr/local/opt/php55/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
# LoadModule php7_module    /usr/local/opt/php71/libexec/apache2/libphp7.so

This will tell Apache to use PHP 5.6 to handle PHP requests. (We will add the ability to switch PHP versions later).

Next, set the Directory Indexes for PHP explicitly, so search for this block:

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

and replace it with this:

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>
 
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Save the file and restart Apache again.

sudo apachectl -k restart

4.4 Validating PHP Installation

The best way to test if PHP is installed and running as expected is to make use of phpinfo(). This is not something you want to leave on a production machine, but it's invaluable in a development environment.

Simply create a file called info.php in your Sites/ folder you created earlier. In that file, just enter the line: <?php phpinfo(); >

Point your browser to http://localhost/info.php and you should see a shiny PHP information page.

You now have Apache and PHP running successfully.

You can test the other PHP versions by commenting the LoadModule ... php56 ... entry and uncommenting one of the other ones. Then simply restart apache and reload the same page.

4.5 PHP Switcher Script

Note: Skip this section. This script is not working correctly… not installed...

To switch between the installed versions of PHP we will install a PHP switcher script.

We will install the sphp script into brew's standard /usr/local/bin:

curl -L https://gist.github.com/w00fz/142b6b19750ea6979137b963df959d11/raw > /usr/local/bin/sphp

Set permissions

chmod +x /usr/local/bin/sphp

Check Your Path

Homebrew should have added its preferred /usr/local/bin and /usr/local/sbin to your path as part of its installation process. Quickly test this by typing:

echo $PATH

returns: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

If you don't see this, you might need to add these manually to your path.

Depending on your shell your using, you may need to add this line to ~/.profile, ~/.bash_profile, or ~/.zshrc.

We will assume you are using the default bash shell, so add this line to a your .profile (create it if it doesn't exist) file at the root of your user directory:

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

At this point, I strongly recommend closing ALL your terminal tabs and windows. This will mean opening a new terminal to continue with the next step. This is strongly recommended because some really strange path issues can arise with existing terminals (trust me, I have seen it!).

4.6 Apache PHP Setup - Part 2

Although we configured Apache to use PHP earlier, we now need to change this again to point the PHP module to a location used by the PHP switcher script. You will again need to edit the /usr/local/etc/apache2/2.4/httpd.conf file and search for the LoadModule php text that you edited earlier.

It's quite IMPORTANT at this stage to fully stop your Apache sever, and start it again. Do not just restart it!

sudo apachectl -k stop
sudo apachectl start

Then, you must replace the current block of LoadModule entries (including any commented out ones):

#LoadModule php5_module    /usr/local/opt/php55/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
#LoadModule php7_module    /usr/local/opt/php71/libexec/apache2/libphp7.so

with this:

Brew PHP LoadModule for sphp switcher
LoadModule php5_module /usr/local/lib/libphp5.so
#LoadModule php7_module /usr/local/lib/libphp7.so

The Commented out line for php7_module is important and required if you are installing PHP 7.0 or 7.1 as it uses a unique PHP module handler. The script will automatically handle uncommenting and commenting the appropriate PHP module.

4.7 Uninstalling PHP versions

First uninstall the extensions that were compiled against that version of php

ex.

brew uninstall php56-twig && brew uninstall --force php56-opcache && brew uninstall --force php56-mcrypt && brew uninstall --force php56-intl && brew uninstall --force php56-apcu

Remove the extension config files, pay attention to the version.

cd /usr/local/etc/php/5.6/conf.d
rm *.*

Uninstall PHP using Homebrew, make sure you target the right version.

brew uninstall --force php56