Configure Apache and PHP on OS X 10.8 Mountain Lion

Apache/2.2.22 is pre-installed in Mountain Lion. To check the version installed:

httpd -v

To start | stop | restart Apache:

sudo apachectl start|stop|restart

After starting Apache you should see the “It Works!” text in the browser – http://localhost.

OS X has historically had 2 document roots, one at system level, and one at user level – the user level one allows multiple acounts to have their own web root, whilst the system one is global for all users.

The default system document root is found at /Library/WebServer/Documents/ – http://localhost/.

The user document root level should be found at the ‘~/Sites’ folder in the User account on a clean installation, but is missing in Mountain Lion,  so you need to make a “Sites” yourself:

cd /Users/username
mkdir Sites

Check that you have a “username.conf” filed under: /etc/apache2/users/. If you don’t (very likely), then create one:

cd /etc/apache2/users
sudo nano username.conf

Then add the content below:

<Directory "/Users/username/Sites/">
  Options Indexes MultiViews
  AllowOverride All
  Order allow,deny
  Allow from all

Permissions on the file should be:

-rw-r--r--   1 root  wheel  298 Jun 28 16:47 username.conf

Then this user level document root will be viewable at: http://localhost/~username/

PHP 5.3.13 is loaded in OSX 10.8 Mountain Lion and needs to be turned on by uncommenting a line in the httpd.conf file.

sudo nano /etc/apache2/httpd.conf

Uncomment the line (remove the #):

LoadModule php5_module libexec/apache2/

To configure php, create php.ini file:

sudo cp /etc/php.ini.default /etc/php.ini
sudo chmod ug+w php.ini
sudo nano /etc/php.ini

Re-load Apache.