Installing PHP 5.4 with MacPorts

I had a little bit of a run around trying to figure this out. So here’s the full process, all in one place for your convenience. This assumes that you have MacPorts installed, but if you don’t, there are several ways of doing so.

Before we get started, be sure to disable OS X’s default Apache install if you haven’t done so already. ┬áThis can be done by opening System Preferences, going to Sharing and disabling Web Sharing.

If you don’t care about the explanation, skip to the bottom.

Let’s get started. Open Terminal, and paste the following:

This will install Apache2 and PHP 5.4, and setup Apache to start on boot.

Next, configure Apache to use PHP 5.4. Create the file /opt/local/apache2/conf/extra/php.conf,

And paste the following into it:

This will load the PHP 5.4 module, make index.php the default directory index file and cause files with .php and .phtml extensions to be interrupted by said PHP module.

Next, tell Apache to include your new PHP config file. Open the httpd.conf,

And add this line to the bottom:

Lastly, restart Apache.

You should now have a functional PHP 5.4 environment. Also, don’t forget that you’ll need to reinstall all of your favorite PHP modules. Here’s what I normally roll with. Don’t forget to restart Apache again.

TL;DR: If you have su access, you can just copy and paste the whole thing all at once: