Apache and PHP on OSX Mountain Lion

From banana_wiki
Jump to: navigation, search

In previous versions of OSX, this was as simple as going into System Preferences and turning on Web Sharing. Well recently Apple thought it was best to remove this feature, but Apache is still there and can be turned on this way.

First lets open Terminal.app, that’s in /Applications/Utilities/

And enter, where USERNAME is your username on the mac:

nano /etc/apache2/users/USERNAME.conf 

This will bring up an editor, and then paste in the following, again where USERNAME is your username on the mac:

<Directory "/Users/USERNAME/Sites/">  
Options Indexes Multiviews  
AllowOverride AuthConfig Limit  
Order allow,deny  
Allow from all  

Next we need to restart aapche for the changes to take affect:

sudo apachectl start

Now you can go to http://localhost in your browser and see the “It Works” Apache message. To view your Sites folder, open http://localhost/~USERNAME

Adding PHP

Now you may want PHP to work as well with your new Apache setup.

First we need to open our Terminal.app again, and enter:

sudo nano /etc/apache2/httpd.conf

Next we need to go to line #114 and uncomment the line (removing the #)

LoadModule php5_module libexec/apache2/libphp5.so

Next we need to create a php.ini, by using the included default:

cd /private/etc
sudo cp php.ini.default php.ini

Now we need to restart apache and PHP should be working:

sudo apachectl restart

To test php we can create a php info file:

nano ~/Sites/info.php

and paste in:

<?php phpinfo() ?>

Now open in your browser http://localhost/~USERNAME/info.php