Skip to main content

Installing PHP 7 with XDebug, Apache and MySQL on OS X Yosemite

In my previous blog post "Installing PHP 7 on OS X Yosemite" I gave some insights on how to get quicly started with PHP7 on your Mac, specifically on your commandline. But before I continue on how to make it work with your installed Apache and MySQL I would like to address a subject many people found worth mentioning: PHP Installation Managers.

PHP Installation Managers

In the feedback I received on my article, people would like to point out that the package managers like "PHP-OSX" binary installer, "phpenv" multiversion php management and installer, "phpbrew" for installing and running multiple versions of PHP and "HomeBrew" the missing package manger for OS X. There will be probably more tools out there, but these were the suggestions made by some of my readers.

Again, I have nothing against these tools and I value their functionality a lot. The only reason I posted my article was to show people how you could have PHP 7 run immediately on your system without being too depending of any availability in a package manager or other tool. If you already have one of these PIM's configured, by all means use them. Saves you a bunch of manual work.

But if you don't have interest to install an additional tool, you can still follow these guidelines to have the latest and greatest PHP7 running on OS X.

If you would like to keep your system clean, there's also a Docker installation for PHP7 provided by Zend Technologies, Inc.


When running PHP you also want to have XDebug compiled into it. Unfortunately there's not a stable release for XDebug 2.4, but I feel confident using the RC in the mean time as debugging is only something I will use locally.

Get the latest XDebug package (at this time of writing it's xdebug-4.2.0RC2), check the signature and unpack it.

$ cd /tmp/
$ wget
$ md5check xdebug-2.4.0rc2.tgz e00e92bb2e72f7c94e1300b2a980e79e
$ tar -xvzf xdebug-2.4.0rc2.tgz
$ cd xdebug-2.4.0RC2/
Once done, we need to phpize and configure XDebug using our installed PHP7 in /opt/php7.

$ /opt/php7/bin/phpize
$ ./configure --enable-xdebug --with-php-config=/opt/php7/bin/php-config
Now all we need to do is to run make and make test to complete the build. This will create a shared object we need to copy to our PHP7 installation extension directory.

$ sudo cp modules/ /opt/php7/lib/php/extensions/no-debug-non-zts-20151012/
Now we have the extension installed, we need to inform PHP7 we have the extension available. In the configuration of PHP7 in my previous article, I had stated that our configuration was going to be installed in /etc/php7 (see --with-config-file-path=/etc/${PHP} argument). This also means we need to ensure a php.ini file is there. Since this is a clean installation, we can easily copy the supplied php.ini-development into our configration path.

$ sudo cp /tmp/php-7.0.0/php.ini-development /etc/php7/php.ini
Now we need to add the xdebug configuration in it.

When we now run php -v on the commandline, we now see xdebug is installed correctly.

Further php.ini settings

In PHP7 if you omit to change the date.timezone, it will be set to UTC by default which might cause some conflicts if you have time and date operations. Best is you set it to the timezone you prefer (e.g. Europe/Brussels).

; Defines the default timezone used by the date functions
date.timezone = Europe/Brussels

Apache Module for PHP7

OS X Yosemite comes with Apache 2.4 installed, so all we need to do is ensure PHP7 is compiled with flag --with-apxs2 enabled, which we already provided in my configure script mentioned in my previous article. This will modify our Apache /etc/apache2/httpd.conf and will add the following line:

LoadModule php7_module        libexec/apache2/
So now we have a PHP5 and a PHP7 module in our configuration. Prefer to uncoment the line for loading the PHP5 module so we only have the PHP7 module to take care of.

Now you just have to make the configuration for php7_module available. Therefor I added a new file /etc/apache2/other/php7.conf where I have specific PHP7 directives (comparable to the PHP5 directives).

<IfModule php7_module>
 AddType application/x-httpd-php .php
 AddType application/x-httpd-php-source .phps

 <IfModule dir_module>
  DirectoryIndex index.html index.php
Now we can restart Apache and we should be able to see a nice phpinfo() page for PHP7 (I always have one available in the webroot).
Success! We have now PHP7 for our web applications too.

MySQL setup

The final step is to ensure we can connect with the installed MySQL database. Since we already took care of that in our script, all database settings were already configured. No further settings are required.

--with-mysqli \
--with-pdo-mysql \
Done. That's it. Again, if you can work with a package manager of binary installer tool, go ahead use them. If you would like to have a bit more control, feel free to use this guide.


Popular posts from this blog

PHP 7 and Apache on macOS Sierra

I posted several talks about compiling PHP from source, but everyone was trying to convince me that a package manager like Homebrew was a more convenient way to install. The purpose of Homebrew is simple: a package manager for macOS that will allow you to set up and install common packages easily and allows you to update frequently using simple commands. I used a clean installation of macOS Sierra to ensure all steps could be recorded and tested. In most cases you already have done work on your Mac, so chances are you can skip a few steps in this tutorial. APACHE AND PHP WITH HOMEBREW I’ve made this according to the installation instructions given on GetGrav. The installation procedures These installation procedures will set up your macOS Sierra with PHP 7.1 and Apache 2.4. Install Xcode command line tools (if not done yet)xcode-select --install Install Homebrew/usr/bin/ruby -e "$(curl -fsSL" Set up for in…

Sessions in PHP 7.1 and Redis

In case you have missed it, PHP 7.1.0 has been released recently. Now you can’t wait to upgrade your servers to the latest and greatest PHP version ever. But hold that thought a second… With PHP 7 lots of things have changed underneath the hood. But these changed features can also put unexpected challenges on your path. Our challenge One of these challenges that we faced was getting PHP 7.1 to play nice storing sessions in our Redis storage. In order to store sessions in Redis, we needed to install the Redis PHP extension that not only provides PHP functions for Redis, but also installs the PHP session handler for Redis. Because we upgraded our servers to PHP 7.1, we were looking to use the latest provided version for this Redis extension: redis-3.1.0. Once installed, we bumped against a nasty problem. Warning: session_start(): Failed to read session data: redis (path: tcp:// Searching the internet for this error, we didn’t got many hits that could point us into a dire…

Speeding up database calls with PDO and iterators

When you review lots of code, you often wonder why things were written the way they were. Especially when making expensive calls to a database, I still see things that could and should be improved.
No framework development When working with a framework, mostly these database calls are optimized for the developer and abstract the complex logic to improve and optimize the retrieval and usage of data. But then developers need to build something without a framework and end up using the basics of PHP in a sub-optimal way.

$pdo = new \PDO( $config['db']['dsn'], $config['db']['username'], $config['db']['password'] ); $sql = 'SELECT * FROM `gen_contact` ORDER BY `contact_modified` DESC'; $stmt = $pdo->prepare($sql); $stmt->execute(); $data = $stmt->fetchAll(\PDO::FETCH_OBJ); echo 'Getting the contacts that changed the last 3 months' . PHP_EOL; foreach ($data as $row) { $dt = new \DateTime('2015-04-…