How to Enable PHP in Apache for Mac OS X Mountain Lion
OS X Mountain Lion ships with PHP 5.3.13 preinstalled, though if you start the built-in Apache server, you will find PHP isn’t enabled by default. Changing this is easy, and if you’re a web developer and want PHP running on your local Mac with OS X 10.8 or later, follow along to have it working in no time.
Launch Terminal and enter the following command, using the administrator password when requested:
sudo nano /etc/apache2/httpd.conf
Now hit Control+W to use the search feature of nano, and type in “php”
Locate the following line and remove the comment (#) from the beginning:
LoadModule php5_module libexec/apache2/libphp5.so
Now hit Control+O to save the changes, followed by Control+X to quit out of nano.
Back at the command prompt, you’ll want to restart the Apache server for the php module to load. This is done with the following command, or you can toggle the on/off switch in the 3rd party WebSharing panel:
sudo apachectl restart
Apache quickly restarts and PHP will be enabled.
You can verify this by throwing any php file into the ~/Sites/ directory and loading localhost/~user/file.php into a web browser, or you use phpinfo() to check the existing php configuration by placing the following into any file with a php extension:
<?php phpinfo(); ?>
Save that file into the user ~/Sites/ directory and load it into a web browser.
If you want to make any changes to PHP configuration, use the following command to make a copy of the default php.ini file:
cp /private/etc/php.ini.default /private/etc/php.ini
Make adjustments to the copied php.ini file in /etc/ or /private/etc/ as necessary, leaving the original .default file intact. As usual, any major changes to php.ini will need to be followed by another Apache restart to take affect.