Mac OS X Yosemite: HOWTO install (non-Apple) PHP5 and Apache2 HTTP Server for Drupal with MacPorts, including GD graphics (and phpMyAdmin) support

HOT TIP: this content is flagged as highly recommended !
Keywords

Configuring Apple's default "out-of-the-box" PHP5, Apache2 HTTP Server (and MySQL) for Mac OS X Yosemite is easy, and has been explained frequently elsewhere. Some of the best explanations are these "coolest guides":

- Get Apache, MySQL, PHP and phpMyAdmin working on OSX 10.10 Yosemite (Oct 2014)

- How to set up Virtual Hosts in Apache on Mac OSX 10.10 Yosemite (Aug 2014)

The default versions of PHP5 and Apache2 for Mac OS X 10.10.1 are:

# /usr/bin/php  -v
PHP 5.5.14 (cli) (built: Sep  9 2014 19:09:25) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
# /usr/sbin/httpd -v
Server version: Apache/2.4.9 (Unix)
Server built:   Sep  9 2014 14:48:20
However, the Apple version of PHP does NOT contain all of the libraries and extensions you may require for Drupal (such as the GD graphics manipulation library). And unfortunately, the Apple version of PHP is not so easy to extend.

Instead, the solution given here uses the MacPorts package manager (there are similar solutions online using Homebrew) to install a new separate Apache2 and PHP5 that play nicely together, as well as some additional extensions needed by Drupal.

Example of the kind of problem you may encounter: (parts of) GD library not found

On trying to change the Color set of the basic Bartik theme for a fresh drush-installed Drupal-7.34 instance, on a freshly installed Mac OS X Yosemite 10.10.1, configured according to this guide, I got the following error in the Apache logs at /private/var/log/apache2/error_log:

PHP Fatal error: Call to undefined function imagecreatefrompng() in /Library/WebServer/Documents/drupal-7.34/modules/color/color.module on line 581, referer: http://localhost/drupal-7.34/admin/appearance/settings/bartik

This was despite the fact that (at least part of) GD was, according to phpinfo, installed, although note that PNG support is not present:

gd

GD Support: enabled
GD Version: bundled (2.1.0 compatible)
GIF Read Support: enabled
GIF Create Support: enabled
JPEG Support: enabled
libJPEG Version: 9 compatible
WBMP Support: enabled
XBM Support: enabled

You can check this by creating a PHP file phpinfo.php at your web root containing this:

<?php
phpinfo();
?>

Googling for 'Call to undefined function imagecreatefrompng()' gives many answers along the lines of:

You must enable the library GD2. Find your (proper) php.ini file. Find the line:

;extension=php_gd2.dll

and remove the semicolon in the front.

The line should look like this:

extension=php_gd2.dll

Then restart apache and you should be good to go.

answered Nov 12 '12 at 4:27, Commander

But php_gd2.dll is only relevant for MS Windows ! And restarting the Apache server (with 'apachectl restart') on Mac OS X Yosemite gives this error:

HP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20121212/php_gd2.dll' - dlopen(/usr/lib/php/extensions/no-debug-non-zts-20121212/php_gd2.dll

It turns out that the correct version for Mac (and Linux) is called gd.so, which was likewise also not under:

/usr/lib/php/extensions/no-debug-non-zts-20121212/

I could not find online the missing gd.so for Apple's PHP for Mac OS X Yosemite, but as we'll see later one can easily install it using MacPorts.

CAUTION: it may be tempting to try to combine the MacPorts PHP5 with the default Yosemite Apache2, but I found (at least for gd.so) that this resulted in incompatibility issues !

Besides, if you want to install other extensions you'll still have to hunt around, so let's just install more convenient package-managed versions of PHP5 and Apache2.

IMPORTANT: before you proceed, do re-comment this line in the default /etc/php.ini so that the default Apple PHP5 does NOT try to load the unfound GD libraries again !
;extension=php_gd2.dll

Or even better, put in the correct gd.so for Mac OS X as a reminder, even if you are not using it here:

;extension=php_gd2.dll ;MS Windows only !
;extension=gd.so ;Mac/Linux

Installing Drupal7-friendlier, easily extendable PHP5 and Apache2 using MacPorts

Find out how to install MacPorts here: https://guide.macports.org/.

There are lots of good guides out there to installing PHP5 and Apache2 with MacPorts, but they are mostly not specific to using PHP with Drupal7 on Mac OS X Yosemite, and many of them are now out-of-date:

- Installing Apache and PHP with MacPorts (Oct 2011)

- Installing PHP and Apache on Mac OSX – that was (pretty) easy (Mar 2009)

WARNING: in particular many guides still refer to libphp5.so ! In fact with the latest MacPorts PHP5 and MacPorts Apache2 you will instead just load the module as mod_php55.so or mod_php56.so (depending on which PHP version you choose).

If you already have MacPorts installed, it is important before you attempt this that you have up-to-date versions of the port registry and all required ports, therefore perform first:

$ sudo port selfupdate

It's easier if you just perform the entire rest of this with sudo bash as root/admin user. I'll do all of the following as sudo bash, otherwise just prepend every command with sudo.

I also recommend that you install the components one at a time (although the port command accepts multiple package arguments).

Install the Apache2 port

Before you install apache2 you should unload the default (Apple Yosemite Mac OS X) version of Apache2 from the launch daemon (and remember to reverse this if you ever need the default server again):

# launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Then we install the MacPorts version of the Apache2 HTTP Server:

# port install apache2

Assuming all went well, you will be invited to set the new version to load automatically:

# launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

Before installing PHP, perform some minor configuration adjustments to the HTTP Apache configuration using an editor like nano/pico, texteditor, or vi:

nano /opt/local/apache2/conf/httpd.conf

You will probably want to change from the default MacPorts Apache document root:

DocumentRoot "/opt/local/apache2/htdocs"

To Apple's usual web root:

DocumentRoot "/Library/WebServer/Documents"

If you changed the DocumentRoot, you MUST also change the Directory directive from:

<Directory "/opt/local/apache2/htdocs">

To:

<Directory "/Library/WebServer/Documents">

And within that section you need to open up the allowed overrides for the .htaccess files used by Drupal from:

AllowOverride None

To:

AllowOverride All

For use with Drupal you will almost certainly also want to prevent directory listings, so change:

    Options Indexes FollowSymLinks

To:

    Options FollowSymLinks

And for Drupal clean URLs check that this is enabled (uncommented):

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

Install the PHP5 port and GD

First list all the php related packages and examine them:

# port list | grep -i php | more
 
swig-php                       @3.0.2          devel/swig
swig-php5                      @3.0.2          devel/swig
php                            @5.6            lang/php
php56                          @5.6.4          lang/php
php56-apache2handler           @5.6.4          lang/php
php56-cgi                      @5.6.4          lang/php
php56-fpm                      @5.6.4          lang/php
php56-calendar                 @5.6.4          lang/php
php56-curl                     @5.6.4          lang/php
php56-dba                      @5.6.4          lang/php
php56-enchant                  @5.6.4          lang/php
php56-exif                     @5.6.4          lang/php
php56-ftp                      @5.6.4          lang/php
php56-gd                       @5.6.4          lang/php
...
php55                          @5.5.20         lang/php
php55-apache2handler           @5.5.20         lang/php
php55-cgi                      @5.5.20         lang/php
php55-fpm                      @5.5.20         lang/php
php55-calendar                 @5.5.20         lang/php
php55-curl                     @5.5.20         lang/php
php55-dba                      @5.5.20         lang/php
php55-enchant                  @5.5.20         lang/php
php55-exif                     @5.5.20         lang/php
php55-ftp                      @5.5.20         lang/php
php55-gd                       @5.5.20         lang/php
...

(You could also use port search --name --glob 'php*' here.)

Notice how the sub packages are prefixed by the full php version number. Choose between php55 or php56 (or an older php version if needed), for demonstration's sake we'll use php55:

# port install php55

If any of your ports are out of date, you may see errors like this:

---> Installing ncurses @5.9_2
Error: org.macports.install for port ncurses returned: no destroot found at: /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_devel_ncurses/ncurses/work/destroot

The example above is for package nurses, but you may hit the same problem for other packages too. The cause and workaround solution are known; from https://trac.macports.org/ticket/36012:

This is a duplicate of #29223, which is a MacPorts base problem which will be fixed in a future version of MacPorts. For now, the solution is to "sudo port clean ncurses" and try again. Cleaning and trying again is the first thing you should try anytime any port fails to install.

Just repeat these until the php port installs:

# port clean [package_with_ destroot_error]
# port install php55

And then let Apache2 know about PHP5:

# port install php55-apache2handler

Which provides:

# port contents php55-apache2handler
Port php55-apache2handler contains:
  /opt/local/apache2/conf/extra/mod_php55.conf
  /opt/local/apache2/modules/mod_php55.so

Next you have to "activate" php55 in the Apache configuration at:

/opt/local/apache2/conf/httpd.conf

(It's a good idea to make a backup copy first, so you can diff them later to see what changes were made.)

You can do this in an editor, or you can use the 'apxs - APache eXtenSion tool' command

# /opt/local/apache2/bin/apxs -a -e -n “php5″ mod_php55.so
IMPORTANT: note that I did NOT use libphp5.so, which is no longer generated by this MacPort as you can confirm with:
# ls /opt/local/apache2/modules/

The result in /opt/local/apache2/conf/httpd.conf is:

LoadModule php5_module modules/mod_php55.so

You should also now add (in an editor) the following MIME type declaration to the same httpd.conf, so that .php files are recognised for processing by the PHP module (you can place it under the LoadModule line above):

AddType application/x-httpd-php .php

And add index.php to the dir_module directive:

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

The PHP configuration file will live at:

/opt/local/etc/php55/php.ini

or (depending on which version you chose):

/opt/local/etc/php56/php.ini

You can either use one of the MacPorts php.ini templates from scratch:

# ls -1 /opt/local/etc/php55/
php.ini-development
php.ini-production

Or you can copy across a previous /etc/php.ini (assuming it's from a recent PHP version).

Time to install the GD graphics library extension using MacPorts, our original aim:

# port install php55-gd

(Adapt according to the PHP version you chose.)

This will install:

# port contents php55-gd
Port php55-gd contains:
  /opt/local/include/php55/php/ext/gd/config.h
  /opt/local/include/php55/php/ext/gd/gd_compat.h
  /opt/local/include/php55/php/ext/gd/gdcache.h
  /opt/local/include/php55/php/ext/gd/libgd/gd.h
  /opt/local/include/php55/php/ext/gd/libgd/gd_intern.h
  /opt/local/include/php55/php/ext/gd/libgd/gd_io.h
  /opt/local/include/php55/php/ext/gd/libgd/gdcache.h
  /opt/local/include/php55/php/ext/gd/libgd/gdfontg.h
  /opt/local/include/php55/php/ext/gd/libgd/gdfontl.h
  /opt/local/include/php55/php/ext/gd/libgd/gdfontmb.h
  /opt/local/include/php55/php/ext/gd/libgd/gdfonts.h
  /opt/local/include/php55/php/ext/gd/libgd/gdfontt.h
  /opt/local/include/php55/php/ext/gd/libgd/gdhelpers.h
  /opt/local/include/php55/php/ext/gd/libgd/jisx0208.h
  /opt/local/include/php55/php/ext/gd/libgd/wbmp.h
  /opt/local/include/php55/php/ext/gd/libgd/webpimg.h
  /opt/local/include/php55/php/ext/gd/php_gd.h
  /opt/local/lib/php55/extensions/no-debug-non-zts-20121212/gd.so
  /opt/local/var/db/php55/gd.ini

Note the last two !

MacPorts GOTCHA: in the /opt/local/etc/php55/php.ini file, you do not need to uncomment either of these:
; extension=php_gd2.dll
extension=gd.so

If you do uncomment the extension=gd.so you will get a harmless warning about duplicates in the Apache error logs:

PHP Warning: Module 'gd' already loaded in Unknown on line 0

This is because under MacPorts control it will be loaded by:

# cat /opt/local/var/db/php55/gd.ini 
; Do not edit this file; it is automatically generated by MacPorts. Any changes
; you make will be lost if you upgrade, uninstall or deactivate php55-gd.
; To configure php55, edit /opt/local/etc/php55/php.ini.
extension=gd.so

It is perhaps a good idea to comment on this under ;extension=gd.so in php.ini as a reminder:

;extension=gd.so
; MacPorts: already loaded by /opt/local/var/db/php55/gd.ini
Again, resist the temptation of simply copying the MacPorts version:
/opt/local/lib/php55/extensions/no-debug-non-zts-20121212/gd.so

into the default Mac OS X Yosemite extensions:

/usr/lib/php/extensions/no-debug-non-zts-20121212/

They are not compatible !

Configuring PHP to work with MySQL

Then restart your new web server USING THE PATH TO THE NEW SERVER not /usr/sbin/apachectl:

/opt/local/apache2/bin/apachectl -k restart

(Likewise, if you are running php in command line mode, you need to be mindful of whether you are executing /usr/bin/php or /opt/local/bin/php -> /opt/local/bin/php55. You may with to play with your $PATH order in your ~/.bash_profile.)

On restarting the web server (with /opt/local/apache2/bin/apachectl) you might get this error (explained at https://www.drupal.org/node/1016622), which you can find in the logs at /opt/local/apache2/logs/error_log:

PHP fatal error 'Undefined class constant MYSQL_ATTR_USE_BUFFERED_QUERY' when trying to install on MySQL

You have to let the PHP module know how to talk to MySQL (which I am assuming you already have installed, otherwise just try the MacPorts version) and the extension for PHP Data Objects (PDO). According to your installed PHP version:

# port install php/php55-myql

You can check what it provides with:

# port contents php55-mysql
Port php55-mysql contains:
  /opt/local/include/php55/php/ext/mysqli/mysqli_mysqlnd.h
  /opt/local/include/php55/php/ext/mysqli/php_mysqli_structs.h
  /opt/local/lib/php55/extensions/no-debug-non-zts-20121212/mysql.so
  /opt/local/lib/php55/extensions/no-debug-non-zts-20121212/mysqli.so
  /opt/local/lib/php55/extensions/no-debug-non-zts-20121212/pdo_mysql.so
  /opt/local/var/db/php55/mysql.ini

Note again the last two !

# cat /opt/local/var/db/php55/mysql.ini 
; Do not edit this file; it is automatically generated by MacPorts. Any changes
; you make will be lost if you upgrade, uninstall or deactivate php55-mysql.
; To configure php55, edit /opt/local/etc/php55/php.ini.
extension=mysql.so
extension=mysqli.so
extension=pdo_mysql.so

So under MacPorts control you DO NOT have to activate it in the php.ini, but it's a good idea to comment on it like this:

;extension=php_pdo_mysql.dll ; NO ! Windows only.
;extension=pdo_mysql.so
; MacPorts: loaded in /opt/local/var/db/php55/mysql.ini

If you are using phpMyAdmin you will also need to install this (via 'sudo bash'):

# port install php55-mbstring

And then you'll find there is also now a matching config file /opt/local/var/db/php55/mbstring.ini, and again, under MacPorts control, you don't have to uncomment it in the php.ini, but leave a remark if you wish:

;extension=php_mbstring.dll
; Under MacPorts control, see /opt/local/var/db/php55/mbstring.ini.

And you will also need:

# port install php55-mcrypt

Which provides:

# port contents php55-mcrypt
Port php55-mcrypt contains:
  /opt/local/lib/php55/extensions/no-debug-non-zts-20121212/mcrypt.so
  /opt/local/var/db/php55/mcrypt.ini

[BTW, there is also a port for phpMyAdmin called phpmyadmin if you need to install it.]

Then restart the web server again (with /opt/local/apache2/bin/apachectl). Use your phpinfo.php file to check that PHP is running OK with GD installed, along with the PNG extensions:

PHP Version 5.5.20

...

GD

GD Support enabled
GD Version bundled (2.1.0 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.5.4
GIF Read Support enabled
GIF Create Support enabled
JPEG Support enabled
libJPEG Version 9 compatible
PNG Support enabled
libPNG Version 1.6.15
WBMP Support enabled
XBM Support enabled

And that's it ! Find (or install with Drush) a Drupal7 instance with Bartik theme and see whether you can change the Appearance settings' Color Set for a chosen Color Scheme and Save configuration, which should now work fine without getting a White Screen of Death (WSOD) ! It may seem like a lot of work just for the GD library, but you now have an easily extended Apache2 and PHP5 under MacPorts packaging control, which is much easier to maintain than the Apple Mac OS X Yosemite versions.