Monday, September 6, 2010

Making PHP to work on Mac

Its such a pain that the most simplest of the things are the ones which people 'forget' to mention! (I have been often guilty of same to be truthful)

I was trying to get Apache, PHP and MySQL working on my Mac and couldn't get a simple 'HelloWorld' in PHP to run on the machine even after supposedly getting everything in place.

I am not going into installation/settings of the PHP / MySQL Or enabling of Apache as that is presented in most of the sites in a very lucid manner, I will just list the issues I ran into and which didn't have a very clear answer on any of the sites for installation and use of the 'AMP' technology.

The two things I stumbled over were:
1. Editing the httpd.conf file for enabling PHP
2. Getting the code to run

(Working on both issues assume you have administrator privileges on the machine and/or can provide credentials when asked for - I had almost made the same mistake of 'forgetting' to mention the simplest thing!)

Editing httpd.conf

Starting with the most basic info which is 'forgotten' while mentioning editing the conf file to enable PHP is that the file is hidden. Almost everybody will mention that the file is at: /private/etc/apache2/httpd.conf. But, if you are new, you can spend all day trying to search for it. And unless you are well versed with searching and editing the file through the terminal by using something like:
sudo vim /private/etc/apache2/httpd.conf

The other methods are:
a) Use an App e.g. Houdini to manage hidden files and folder via GUI
b) run the following command on your terminal :
defaults write com.apple.finder AppleShowAllFiles TRUE
and then restart the Finder on your machine by using killall Finder

Now, you should be able to see and access all the hidden files and folders through any text editor. The \etc folder being in the 'Macintosh HD'

Though, I read that BBEdit and similiar other such editors can show hidden files using their 'View' option, I wasn't able to do so till I did the step above (quite possible I wasn't looking properly)

Now, its much easier to see and uncomment the :

LoadModule php_module libexec/apache2/libphp5.so

Getting the code to run

In hindsight, I understand that there are two ways of writing PHP code: the old way - using just "<? Code ?>" and the new way - "<?php Code ?>"
The below section in the php.ini was set to 'On' and the old style worked with this setting - which I could successfully run after trying many other things and being pointed in the right direction here . And, on turning it 'Off', I am able to get the new style to work. Though as I am only starting out - I have just tried phpinfo() and echo till now.

; short_open_tag
; Default Value: Off