As with Apache HTTP server, PHP comes preloaded on a Mac. We simply need to go through a few steps to enable it and integrate it with the Apache HTTP server environment.
First, we need to modify the HTTP server configuration file. The default location of the server should be /private/etc/apache2. We’ll head to that location and then edit the httpd.conf file using the following command:
cd /private/etc/apache2 sudo vi httpd.conf
When you’ve opened the httpd.conf file, search for the following line:
#LoadModule php5_module libexec/apache2/libphp5.so
This is the line that allows PHP 5 to run within the HTTP server. To enable it, remove the initial pound sign (#), giving you:
LoadModule php5_module libexec/apache2/libphp5.so
Save and exit the httpd.conf file. Now we’re going to move on to enabling PHP by setting up our php.ini configuration file. We’ll go to our /private/etc folder and rename the default configuration file as follows:
cd /private/etc sudo cp php.ini.default php.ini
Now we simply need to restart the Apache HTTP server, which we can do by going to Web Sharing within System Preferences and disabling and reenabling the server.
Once we’ve done this, we should be able to use PHP files by navigating to them in a browser at http://localhost/~username/file.php.