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.
9 thoughts on “Working with multiple PHP versions on MAC OS-X”
Please stop advising people to delete files in `/usr/bin`. That’s Apple land, and Apple should be the only ones modifying it, especially since they will happily overwrite your changes with the next security or OS update *and* script may actually rely on `/usr/bin/php` being php 5.3 on OS X $whateveryourversion.
The correct way to make your preferred version of PHP the one being run when you execute “php” on the command line is adjusting $PATH (which you already have since you’re using MacPorts) and making sure there’s a php binary on that path somewhere. The correct way to do this for PHP from MacPorts is by using the port select mechanism by running `sudo port select –set php php54`, which will create the symlinks /opt/local/bin/php, php-config, phpize and the appropriate manpages.
Thanks for the comment and for the advice ;-). Regards.
Clemens and Junior Grossi Hello! I will be grateful for your help on this issue. Please, see my questions below. I truly need help. I have an obsolete Mac OS X 10.7.5, which has the preinstalled 5.3.28 version of php; I have installed php 5.5.37. I am a newbie, and after a week of installing/reinstalling I have managed to get php -v reveal this new 5.5.37 version by doing the following:
sudo mv /usr/bin/php /usr/bin/php5328
sudo ln -s /usr/local/php5/bin/php /usr/bin/php
– hide quoted text –
PHP 5.5.37 (cli) (built: Jun 26 2016 13:53:04)
But when I load http://localhost/~user/phpinfo.php or http://localhost/~user/phpmyadmin/ I get the information that I have 5.3.28 version of php. My /usr/local/ folder had two php directories: php5 and php5-5.5.37-20160626-135747. So I decided to: [code]sudo mv php5 php5_old[/code] and [code]sudo mv php5-5.5.37-20160626-135747 php5[/code]. php -v still show the correct 5.5.37 version, but both http://localhost/~user/phpinfo.php or http://localhost/~user/phpmyadmin/ tell me I use the old one.
As to the $PATH, here is what I have done:
– went to:
cd ; nano .bash_profile
– and then:
That file already contains export PATH=”/usr/local/mysql/bin:$PATH”, which I have put there while installing mysql.
I will be very grateful for your help!
EDIT: I have also tried to see if I have an /opt directory to add Loadmodule line to httpd.conf file, but I don’t have that directory.
-bash: cd: /opt/local/etc: No such file or directory
-bash: cd: /opt/: No such file or directory
Hey! Welcome! About your question, using just “localhost” in the URL you’re using the default Apache installation that comes with MAC OS. I suggest you start a PHP server, going to the path and “php -S localhost:8000” for example. So after that go to the url “http://localhost:8000” and you’ll see your page. Using this approach you’re not using Apache server, but PHP one, and the PHP is installed using version 5.5 as you told before, so no problem. Best regards.
@juniorgrossi:disqus Thank you very much for your reply. Being a newbie, I have to use the usual methods for now, i.e. AMP. I have followed usual instructions on how to set up AMP on Macs. So, I truly need to solve the issue that I face with php version. I seem to be very close, but I need help )
Hey again 🙂 I suggest you to use a VM machine, using Vagrant to that, like Homestead. If you want to install inside the MAC OSX I suggest to do that with Homebrew. Install homebrew and after install php5, something like “brew install php55”, easy like that. So take a look on the link command. After installed php55 or another version just do “brew link php55”, or “brew unlink php53 && brew link php55”. And you’ll have php55 🙂
If you need more help ping me on Slack. I’m on the official WordPress slack channel. Or just let me know where’s the slack team you are 🙂
Thank you very much! Yes, I already have VM because the free cs course I am taking now required to do some tasks on VM. But because of my obsolete OS, and, as I have found out because all apple devices seem to sort of dislike third party applications, my computer ‘switches into turbo mode” with all its fans on as soon as I use some additional apps and the like; even Firefox vexes the poor thing ) I will do my best to find the way out ) Thank you for your help!
I followed the instructions here, but I’m not able to run PHP53. Connection refused to localhost, when I swap PHP modules in httpd.conf:
LoadModule php5_module /usr/local/php5/libphp5.so
#LoadModule php5_module /opt/local/apache2/modules/mod_php53.so
With the above it’s ok with 5.6, but on the other hand,
#LoadModule php5_module /usr/local/php5/libphp5.so
LoadModule php5_module /opt/local/apache2/modules/mod_php53.so
it’s not working. If I use this mod_php53.so it should automatically now that my php.ini is in the above listed place (/opt/local/etc/php53/)
What can be the problem. Thanks for help.
Hi @Dániel! Each version has its own php.ini file. You can try to run PHP on the command line to check if it’s installed. You can check if the .so file is present in the directory, and don’t forget to restart the webserver after any changes.
Thanks for the comment.