Getting MySQL, PHP and Apache To Work (Again) On OS X Mountain Lion

Apple's gone and done it again, messing up our local Web Server setup. In Lion you used to be able to put a website in ~/Sites and it'd work when browsing to http://127.0.0.1/~username.

What they've done:

  • Took out the GUI option of starting/stopping Apache in System Preferences' Sharing section.
  • Updated Apache, which comes with a new httpd.conf that disables the PHP module.
  • Removed username.conf from /etc/apache/users, thereby throwing out our local user's DocumentRoot.
  • Updated PHP from 5.2 to 5.3, moving our /etc/php.ini to /etc/php.ini-5.2-previous.

How to fix it:

PHP

Enable PHP by opening /etc/apache2/httpd.conf and uncomment the following line:
LoadModule php5_module libexec/apache2/libphp5.so

MySQL

When installing MySQL on Mac it will put the mysql.sock file in the "tmp" folder. This is not the place where the version of PHP that ships with OS X will look for it. We need to tell PHP where to find the mysql.sock file.
Copy /etc/php.ini.default to /etc/php.ini, open /etc/php.ini and change all occurrences of "/var/mysql/mysql.sock" to "/tmp/mysql.sock" (without the quotes)

Apache

Make Apache recognize /Users/<username>/Sites as a valid DocumentRoot again.
Create a text file /etc/apache2/users/<username>.conf and add, swapping out username with your actual username:
<Directory "/Users/username/Sites/">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Starting Apache

Now that our settings are back to normal we can start Apache, but with the item missing from System Preferences' Sharing menu we'll need to do it from the command line. Open up Terminal and type: sudo apachectl start
This controls Apache and tells it to start, you obviously need to be administrator so "sudo" is needed here as well. It will ask you for the password and then, when you browse to:

http://127.0.0.1/~username

You should see your old website again.

Apache can be started, restarted and stopped with the following commands:
sudo apachectl start
sudo apachectl restart
sudo apachectl stop