OSX: Getting Console / Terminal to Use correct PHP5 Build

If you are running an OSX-based web server for your testing / staging machine as I am, you have probably needed to update your PHP library to a php5.3.x build.  I’m using Entropy for this and things seemed fine on the webserver.  Most tutorials have the same information out there about getting you running with PHP5.x in Apache.  But what about when you need to execute a php script form a cron job or from a shell script, or just right from the console?   I noticed that I was getting a strange report that my date-setting was generating a fatal error whenever php tried to run from the shell:

$ php
PHP: syntax error, unexpected '(' in /private/etc/php.ini on line 998

Upon inspection, the line in question was the default date-time setting:

date_default_timezone_set('America/Los_Angeles');

Upon inpecting this further I was baffled. This runs fine in apache and the code is 100% correct php5 initialization. After a while it dawned on me: What if apache is running the right php version but my shell is running the wrong php version?

It took some research and I was eventually able to learn to fix this by editing the following file and updating the path to php5.

$ mate ~/.bash_login

Then changing this:

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

To this:

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/local/php5/bin:$PATH"

I saved the file and re-initialized it:

$ source ~/.bash_login

Now when I run php from console, I get the correct version of php running. Enjoy!

Categories

Leave a Reply

Translate »