Working with multiple PHP versions on MAC OS-X

Today I had to update a project that was developed using WordPress and PHP 5.3. Today I have PHP 5.4 installed on my machine and this newer version abandoned some old features, and you have some Fatal errors like Call-time pass-by-reference has been removed. The solution was go back to PHP 5.3 and do the updates on my the project, because the production server is with PHP 5.3 too.

The cleaner solution is using Vagrant, that allow you install only what you want for a specific project, leaving your machine cleaner. But was not my case at the moment.

Macports

I’ve used macports to install PHP in my machine. I think macports is a very good way to organize all sources in the same place. More information you can find here.

You can install PHP 5.3 and 5.4 in the same machine and decide what you’ll use just updating your Apache httpd.conf file.

Updating Apache configuration file

You can install Apache by macports too. Just search for it and install. Now, I’m using the original Apache2 version that comes with the MAC OS-X Mountain Lion. So my httpd.conf(apache file configuration) is located inside /etc/apache2/httpd.conf.

Open this file and make some changes:

sudo nano /etc/apache2/httpd.conf

Now you have your Apache2 configuration file for editing. What you must do is change dephp5_module file to point to the version you want. For default macports installs everything under /opt/local directory. So you can find the php5_module file under /opt/local/apache2/modules/. There you have mod_php54 and mod_php53.

Changing PHP versions

If you want to use now PHP 5.3 you just have to change the httpd.conf file line to:

LoadModule php5_module /opt/local/apache2/modules/mod_php53.so

Or if you want PHP 5.4:

LoadModule php5_module /opt/local/apache2/modules/mod_php54.so

PHP.ini configuration file

By default macports store php.ini files under /opt/local/etc/php54/. There you can find 2.ini files:

cd /opt/local/etc/php54/ ls php.ini-development php.ini-production

Just rename the development file to php.ini to be your default PHP 5.4 ini file.

sudo mv php.ini-development php.ini

And restart Apache:

sudo apachectl restart

Now you can use the version you selected in httpd.conf file.

Using PHP on Command Line (CLI)

You can change the php command line version too. By default macports installs the binfiles under cd /opt/local/bin/. What you must do is create a symbolic link to the PHP version you want. So if you want to use php command line 5.3 you run: