Installing PHP 5.4 (like a boss) with MacPorts

PHP 5.4.7 is the latest stable release of PHP. WordPress has a minimum required version of 5.2.6. Most developers aren’t using the PHP 5.4 branch. Actually, most aren’t even rocking PHP 5.3. This disgusts me.

PHP 5.3 added support for closures. If you come from the world of JavaScript, you know how useful they can be. If you have used PHP 5.3 and closures in classes, you will be happy to know that PHP 5.4 allows you to use $this in closures in class methods.

If you haven’t messed around with PHP 5.3, you can install these MacPorts to get started:

php5 +apache2+fastcgi+pear
php5-apc
php5-curl
php5-gd
php5-http
php5-iconv
php5-imagick
php5-mbstring
php5-mcrypt
php5-memcached
php5-mysql +mysqlnd
php5-openssl
php5-tidy

If you already have PHP 5.3 and want to upgrade to PHP 5.4, these are some tricks to get you on the right path:

sudo -s // use sudo mode throughout

port uninstall php5
// won't work if you have extensions installed,
// so uninstall everything that has PHP5 as a dependency first

port install php54
cd /opt/local/etc/php54 && sudo cp php.ini-development php.ini

Install a bunch of PHP extensions:

port install php54-apc php54-curl php54-gd php54-http php54-iconv php54-imagick php54-mbstring php54-mcrypt php54-memcached php54-mysql php54-openssl php54-tidy

To use mysqlnd with a local MySQL server, edit /opt/local/etc/php54/php.ini and set

mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket
to

/opt/local/var/run/mysql5/mysqld.sock

Make sure PHP 5.4.6 is the default PHP binary:

which php

If it’s something like /usr/bin/php:

cd /usr/bin && sudo rm -rf php
sudo ln -s /opt/local/bin/php54 php

You now have PHP 5.4.6 and your extensions, but you no longer have the apache variant.

port install php54-apache2handler

cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n php5 mod_php54.so

vi /opt/local/apache2/conf/httpd.conf (remove the old php5.so)

You now have PHP 5.4 and the apache handler, but you no longer have the PEAR variant. You can try to make this work:

port install pear-PEAR

Or you can do the following:

cd #
curl http://pear.php.net/go-pear.phar -o go-pear.phar
sudo php go-pear.phar

You will prompted to specify config vars, we want to change #1 and #4.

Press 1 – Installation base ($prefix) – and enter:

/opt/local/lib/php54

Press 4 – Binaries directory – and enter:

/opt/local/bin

More checks for PEAR:

pear info pear && rm go-pear.phar
pear config-set auto_discover 1

// make sure PEAR is in the PHP include path
pear config-get php_dir

// if you don't see "/opt/local/lib/php54/share/pear" in there
php54 -i|grep 'php.ini'
// you should see "/opt/local/etc/php54" - if you don't:
sudo vi php.ini
// change include_path to:
include_path = ".:/opt/local/lib/php54/share/pear"

PEAR is installed, let’s install some PEAR stuffs:

// Unit tests
pear install pear.phpunit.de/PHPUnit
// Documentation generator
pear install pear.apigen.org/apigen

Restart Apache:

sudo /opt/local/apache2/bin/apachectl restart

You can start Apache and Memcached, et al by using commands like:

sudo port load apache2
sudo port unload apache2

sudo port load memcached
sudo port unload memcached

// memcached debugging, start with:
memcached -vv

Intstalling MySQL 5.5 on OS X (Mountain) Lion

I have a dark and painful history of installing MySQL the wrong way, which has forced many late nights and ungodly amounts of frustration. It should be super easy, and sometimes it is, but sometimes it is not. And it can suck.

This post is as much a documentation of what I did so I can re-read it the next time I screw up something as it is a tutorial for you. Let’s begin.

Don’t use MacPorts, use the .dmg from the MySQL website

MacPorts will give you MySQL 5.1.48 or something like it when you install the mysql5 port. The mysql55 port doesn’t upgrade your mysql5 port, it runs side by side and can be super confusing if you want to run terminal commands or change permissions on directories your forgot about when you installed mysql5. mysql5-devel doesn’t build on Mountain Lion yet. Awesome.

The .dmg file will give you everything you need: installs beautifully in /usr/local/mysql, provides a startup item installer that will fire up the mysqld_safe launch daemon when you turn your computer on, and a Preferences pane item that will allow you to start and restart MySQL until you are blue in the face with the simple press of a button.

Your install should start like this:

  • Download the .dmg
  • port uninstall mysql5 and mysql5-devel or whatever else you have installed
  • Add this to your ~/.bash_profileexport PATH=$PATH:/usr/local/mysql/bin
  • Restart your computer to make sure only your new MySQL 5.5’s daemon is running (when you uninstall the MacPorts, their daemons are still running until you kill them or restart) by typing:
    ps aux | grep mysql

If you see anything running from /opt, your MacPort(s) aren’t uninstalled.

After you have restarted your computer:

  • Fire up Terminal and type: mysql -u root
  • Run this query:  GRANT ALL ON *.* TO 'root'@'localhost' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; (replacing ‘mypassword’ with whatever you want your root password to be)

At this point, you should have a database that works. If not, read about symlink-ing .sock files, etc here: How to install MySQL 5.5 on Mac OS X 10.7 Lion

OS X 10.8 Mountain Lion Preview + MacPorts

I love having access to software early, so I bought a Mac Developer Program membership last year for 100 bones and subsequently never really used it. I got an invite last week to download the Developer Preview of Mountain Lion, the forthcoming OS X upgrade, so I did.

Note: as soon as you upgrade to Mountain Lion, you need to install the preview of Xcode 4.4. After doing that, you have to install Xcode command line tools, which doesn’t happen automatically. If you don’t follow this extra step, you won’t have anything at your disposal in Terminal – make, svn, etc.

Installing pre-release software is dangerous and will break things all over the place. I’m going to tell you what breaks (that I know of so far) and how to fix it (if i know).

Chrome

I’m not going to pretend like I know why, but Chrome runs like shit so far in Mountain Lion. Might have something to do with Darwin 11 vs 12, might not. However, Safari is NOT broken and runs faster and smoother than I have ever seen it. I have a strong affinity for Chrome so I may not switch back yet, but man, it is fast.

Little Snitch

If you have pirated software and you want to block all attempts at activation pings in a managed way, you probably have a program like Little Snitch helping you since it allows you to block all internet access to selected programs. I use it because I have Adobe Master Collection installed and I, not surprisingly, didn’t pay the thousands of dollars that it costs. Little Snitch doesn’t work because it sniffs the OS version (10.8) and deems itself incompatible.

MacPorts – specific ports

This one’s a doozy. If you run port upgrade outdated, every one of your MacPorts will be up for upgrade because they were previously compiled on Darwin 11, not Darwin 12 which ships with OS X 10.8. Annoying, and time-consuming, but not a deal-breaker… until you get to any ports which require libxml2, which is “most.”

Because some of your ports will bail on error, you need to upgrade ports individually (get the list using port outdated) using port upgrade fontforge or whatever.

Once you have upgraded every port that doesn’t die on error, you will realize you have a ton of ports that are in limbo, namely, php5 and all php5-* extensions, X11 and all libraries, and postgresql90. They all have libxml2 as a dependency which no worky. The error is due to locale in reinplace which is not fixed in the distributed version of MacPorts but is fixed in SVN trunk of MacPorts project: http://trac.macports.org/browser/trunk/base/src/port1.0/portutil.tcl?rev=89839.

So, if we want to fix our ports, we need to run trunk of MacPorts while running the dev version of Mountain Lion.

mkdir -p /opt/mports
cd /opt/mports
svn checkout https://svn.macports.org/repository/macports/trunk
cd /opt/mports/trunk/base
./configure --enable-readline
make
sudo make install
make distclean

port edit libxml2
--- and change "reinplace" to "reinplace -locale C"

At this point, you need to go back to individually upgrading ports, but you’ll find that most of them get upgraded as dependencies of other ports, so it won’t take as long as before.

Ports that are still broken after this for me: ffmpeg – due to failure of libvpx port (VP8 codec), postgresql90, and the PHP PostgreSQL extension (php5-postgresql). I’m sure there are many others.

Mountain Lion is pretty cool and worth upgrading to if you have the option. Once you upgrade, you can’t go back to Lion without having previously cloned your hard drive, etc, so be careful and be willing to get your hands dirty when things don’t work.

I installed Mountain Lion on my laptop, which I have been using less and less for programming lately, but I often need to run local PHP, so I had no choice but to figure all of this out.