Running PHP on the macOS / Mac OS X Apache Web Server

PHP 5 is installed in macOS / OS X by default, but the built-in Apache web server may not be configured to automatically run PHP. To enable PHP, the apache configuration file needs to be modified. This file is located in the following location:

/etc/apache2/httpd.conf

In order to edit this file, the permissions on the file must be changed. This can be accomplished by doing the following:

1. Open a terminal windows (Applications -> Utilities -> Terminal)

2. Change to the correct directory (cd /etc/apache2)

3. Change the permissions on the file (sudo chmod 755 httpd.conf)

Once the file permissions are changed, you can use a tool like EditRocket to open the httpd.conf file and modify it. After opening the httpd.conf file, we need to look for the following line:

#LoadModule php5_module libexec/apache2/libphp5.so

This line needs to be uncommented by removing the # in front of the line. Remove the # and then save the file.

PHP is now enabled, but we need to restart the apache web server for the changes to take effect. To restart the web server, do the following:

1. Go to System Preferences -> Sharing View

2. Uncheck the Web Sharing box. Then check the Web Sharing box again. This will start and stop the apache web server.

To test PHP, create a PHP file called test.php in the following directory

/Library/WebServer/Documents/

Enter the following URL to visit the page:

http://localhost/test.php