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.
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.
Installing PHP versions
First let's install PHP 5.4 using macports:
sudo port install php54
You can search for packages. Let's search for PHP 5.4 Mysql driver:
port search php54-mysql*
And you'll get:
php54-mysql @5.4.9 (lang, php, www, databases) a PHP interface to MySQL databases, including the mysql, mysqli and pdo_mysql extensions
So now you can install PHP 5.4 MySQL driver:
sudo port install php54-mysql
Well, this way you can install what you want, like extensions and drivers, like pdo, etc. For example, you must install the apache2 handler too. So:
port search php54-apache* php54-apache2handler @5.4.9 (lang, php, www) php54 Apache 2 Handler SAPI sudo port install php54-apache2handler
Now you have PHP 5.4 installed. The same way you will install PHP 5.3, for example, like:
sudo port install php53 sudo port install php53-mysql sudo port install php53-apache2handler // and more
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 de php5_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 bin files 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:
sudo rm /usr/bin/php // remove /usr/bin/php first sudo ln -s /opt/local/bin/php53 /usr/bin/php // pointing to php53 php -v // get version PHP 5.3.19 (cli) (built: Nov 26 2012 12:44:33) Copyright (c) 1997-2012 The PHP Group
Or using PHP 5.4:
sudo rm /usr/bin/php // remove /usr/bin/php first sudo ln -s /opt/local/bin/php54 /usr/bin/php // pointing to php54 php -v // get version PHP 5.4.9 (cli) (built: Nov 26 2012 12:40:37) Copyright (c) 1997-2012 The PHP Group
That it! I hope help you! Thanks for reading.