Maintaining two versions of PHP with MacPorts

As a mac user, I’m a huge fan of the great work the people at MacPorts do. If you haven’t used MacPorts before, its basically a mac version of apt-get or rpm and allows you to install all kinds of unix-y goodness from source code on OSX. I use MacPorts to maintain my web development tool suite, and recently I figured it was high time I installed PHP4 (for testing things out only), and wanted to use MacPorts to do so. I already had PHP5, Apache 2, and MySQL 5 installed, all I needed was PHP4. If you want to get started from scratch with MacPorts this tutorial covers most of my process and I based my php-switch shell script off of his as well.

Installing PHP4

Installing PHP4 is an easy matter, as long as you remember to shut Apache down, and deactivate your current PHP5 port. sudo apachectl stop and sudo port deactivate php5. Got me to the point where installing PHP4 didn’t cause any serious errors. I made sure to back up all my configuration files, as I’ve lost them in the past when Apache updated itself. Once all the configuration files were backed up, I ran sudo port install php4 +apache2 +mysql5 +postgres8 (you may want to install other variants, take a look at what is available). PHP4 created a /opt/local/etc/php4 dir to contain its php.ini and other configuration files. I then copied my httpd.conf to httpd.php4.conf and included the correct PHP module. I also copied my existing httpd.conf to httpd.php5.conf so using my php-switch script would be easier.

Getting XDebug setup.

I’m also a huge fan of Xdebug and owe Derick Rethans a beer should we ever meet. So I needed that setup too. The great people at ActiveState have an excellent selection of xDebug binaries and I find them quite handy, you can also install xDebug with MacPorts but I couldn’t figure out how to get PHP4 xDebug running. After downloading the tar from ActiveState I copied the xdebug.so into /opt/local/lib/php4/extensions. I also have a /opt/local/lib/php5/extensions that contains my PHP5.2 extensions. This lets me keep my xDebug binaries neat and tidy. A quick edit of the php.ini file, and a restart of Apache, and PHP4 was up and running.

Switching between PHP5 and PHP4

While there is no php-switch in MacPorts its not hard to make one. Taking what I found in the tutorial linked above, I was able to prune it down to the following.

Show Plain Text
  1.  
  2. #!/usr/bin/env bash
  3. /opt/local/apache2/bin/apachectl stop
  4.  
  5. case "$1" in
  6. 'php4')
  7. /opt/local/bin/port deactivate php5
  8. /opt/local/bin/port activate php4
  9. cp /opt/local/apache2/conf/httpd.php4.conf /opt/local/apache2/conf/httpd.conf
  10. ;;
  11. 'php5')
  12. /opt/local/bin/port deactivate php4
  13. /opt/local/bin/port activate php5
  14. cp /opt/local/apache2/conf/httpd.php5.conf /opt/local/apache2/conf/httpd.conf
  15. ;;
  16. 'php53')
  17. /opt/local/bin/port deactivate php5
  18. /opt/local/bin/port activate php5-devel
  19. cp /opt/local/apache2/conf/httpd.php5.conf /opt/local/apache2/conf/httpd.conf
  20. ;;
  21. esac
  22. /opt/local/apache2/bin/apachectl start
  23.  

Since PHP4 and PHP5 have different directories for their configuration files, you don’t need to move those around, and I have my extensions in separate directories as well so I don’t move those around either. You’ll also see that I have a PHP 5.3 package installed with the php-devel name, doing this is mostly a simple repeat of the install process used for PHP4. I put this bash script in /opt/local/bin/php-switch and made it world executable. I can now call php-switch php4 to switch to PHP4 and php-switch php5 to switch back to PHP 5.2. I’m pretty happy with the setup, switching to PHP 5.3 is a bit more tricky but I don’t do it often enough to be annoying, when I do I’ll make the script more friendly. In the mean time, enjoy.

Comments

Great stuff Mark…it’s good to see you got this working!

Sonny Scroggin on 14/7/09

thnx, mark! Just was trying to get a new version of php 5.2.x on my macbook.

Damn! Standard php5 macport is already 5.3
I don’t want that! :( Also trying to install apache2 with it but failing big time.
Trying to do it from source myself again…

Cheers!

primeminister on 27/7/09

You could also look into VirtualBox one time. You can have different instances running different webservers / php versions, make snapshots of them, hand them out to colleagues, and shut them off when you’re done coding. Hence shutting down all server related stuff and save you some CPU cycles / battery life.

more info in a writeup i once did
http://kevin.vanzonneveld.net/techblog/article/how_virtualization_will_improve_your_code/

Kevin van Zonneveld on 8/8/09

I’ve done this with FastCGI – I have one web server and multiple FastCGI proxies (for every PHP version one proxy), then I just connect the proxies with directories, files or extensions, so you don’t have to switch between the PHPs. I belive it can be done on mac too – check my tutorial: http://ondrejsimek.com/2009/08/running-multiple-php-versions-is-so-easy-with-fastcgi/

Ondrej Simek on 27/8/09

So now that Snow Leopard has upgraded my MacBook to PHP 5.3 I’m looking for a way to use MacPorts to install 5.3 & 5.2 side by side so I can still develop on CakePHP 1.2. MacPorts has created a php52 port, so I’m trying to figure out how to adapt this method to that situation. Any ideas?

Darren on 7/9/09

Just gave this method a try, however, MacPorts is updated to php 5.3 and I need 5.2. Anyone know how I can get 5.2 and do the technique mentioned above to switch between the two?

Sufian on 9/11/09

Sufian: There is a php52 port in macports. It has PHP5.2.11 in it :) Also you may want to look at my article about updating to PHP5.3 with macports

mark story on 13/11/09

Cool site http://www.turkishny.com/ – new york, turkishny, new york, news and othe.

invermera on 3/10/11

Seems like this works fine with bare bones installs of PHP4 and PHP5. But added complex factor of modules doesn’t work. I’ve tried to deactivate all php5 modules via sudo port deactivate php5 and some will not deactivate due to dependencies.

Eliot on 19/12/11

Eliot: A while after I wrote this tutorial, macports totally changed how they managed PHP extensions which made this far more difficult. My bash script to switch ended up being really gross and long. I gave up on that and ended up just compiling PHP from source. http://mark-story.com/posts/view/installing-multiple-versions-of-php-from-source I gave up on PHP4, as its just not worth the time for me anymore.

mark story on 21/12/11

Yep…

Keep getting the following error message

Error: port deactivate failed: Please uninstall the ports that depend on php5 first.

So, it seems that to run PHP4 and PHP5 with one Apache is through MAMP or MAMP Pro.

Eliot on 21/12/11

Hey! This is my 1st comment here so I just wanted to give a
quick shout out and say I genuinely enjoy reading through your posts.
Can you suggest any other blogs/websites/forums that cover
the same topics? Thank you so much!

Going In this article on 7/7/13

Have your say:

*
* You can use Textile markup, but be reasonable