You first need to enable Apache in the Sharing prefpane. Check "Web sharing" and your web server is running.
Note that since Mountain Lion, Web Sharing was removed from the Sharing preference pane, but Apache is still included. Check this answer for controlling Apache in Mountain Lion.
To activate PHP you'll need to edit /etc/apache2/httpd.conf
in Terminal.app. This requires root credentials. nano
is a very accessible command-line editor if you are uncomfortable with vim
.
sudo nano /etc/apache2/httpd.conf
Find the line (you can press ctrl + W to start searching in nano
):
#LoadModule php5_module libexec/apache2/libphp5.so
and uncomment it. Next find the line
#Include /private/etc/apache2/extra/httpd-vhosts.conf
and uncomment that as well to enable virtual hosts support.
Save the file and exit nano
by pressing ctrl + X, then confirming the changes by pressing Y(es), then enter.
You can now edit your virtual hosts in the file /etc/apache2/extra/httpd-vhosts.conf
sudo nano /etc/apache2/extra/httpd-vhosts.conf
Important to note is that the first defined host will be the default host for unqualified host names. For resolving additional hostnames apart from localhost
I recommend hardcoding them in /etc/hosts
.
To install MySQL, download the installer from the MySQL website (64bit installer should be ok). Follow the instructions to install it.
Finally, to configure PHP for MySQL, copy the default php.ini
:
sudo cp /etc/php.ini.default /etc/php.ini
Now you can edit /etc/php.ini
(again root access required) and replace any reference to /var/mysql/mysql.sock
with /tmp/mysql.sock
(the default location of the MySQL socket after running the installer). There probably are about 3 references to that path.
Finally, restart Apache for the new configuration to take effect:
sudo apachectl restart
Alternatively you can restart Apache by toggling it off and on again in the Sharing prefpane.
Done.
I have an alias specified in OSX server pointing to a user directory.
I spent a long while chmodding and messing with _www user, adding executable permissions recursively, uninstalling macports and all sorts of stuff trying to get this to work. No idea why it wasn't working.
Eventually, I just checked the "shared folder" checkbox in the Finder for that folder, and it worked, on the specified domain, with php active, the way I wanted it to. :/
...so that was easy.
Best Answer
Okay, I ended up running
which returned
When I edited httpd.conf, it turned out that it was trying to load two different version of libphp5.so: a new version that had been freshly installed (I think) by Homebrew, and an old version that Homebrew had (I think) deleted once it installed the new version.
For some reason, when Apache tried to load the old version of libphp5.so and found it wasn't there, this prevented Apache running at all. As soon as I deleted the line that tried to load the missing libphp5.so and restarted Apache everything worked fine.