Pages

2015/12/07

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.


XDebug

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 http://xdebug.org/files/xdebug-2.4.0rc2.tgz
$ md5check xdebug-2.4.0rc2.tgz e00e92bb2e72f7c94e1300b2a980e79e
OK
$ 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 xdebug.so shared object we need to copy to our PHP7 installation extension directory.

$ sudo cp modules/xdebug.so /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.

[xdebug]
zend_extension=/opt/php7/lib/php/extensions/no-debug-non-zts-20151012/xdebug.so
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).

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
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/libphp7.so
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
 </IfModule>
</IfModule>
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 configure-php.sh script, all database settings were already configured. No further settings are required.

--with-mysqli \
--with-pdo-mysql \
--with-mysql-sock=/tmp/mysql.sock
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.

No comments:

Post a Comment