How to use PHP on Snow Leopard

by Roger Collins on December 20, 2009

in Internet,Programming

Here’s how to get a PHP site running on your Mac with minimal effort.  First, lets put a PHP file in the right directory so we’ll have something to see when we get it working.  Put the following code in Sites/phpinfo.php

<?php phpinfo(); ?>

It is an effective method to develop backwards, so lets start with what we want to see at the end of this procedure.  Open your browser and visit (replace roger with your username):

http://localhost/~roger/phpinfo.php

You should get an error like “Unable to connect.”  That’s because we haven’t enabled the web server nor enabled PHP in the web server.  Lets enable Apache web server.  Go to System Preferences -> Internet & Wireless -> Sharing, and check Web Sharing.

Now if you refresh your browser with the above URL you’ll just see the PHP file as you coded it.  Still not what we want, but at least the web server is running.

Edit the file /etc/apache2/httpd.conf, search for “php”, and uncomment (remove the #) on that line. Should look like this when you’re done:

LoadModule php5_module  libexec/apache2/libphp5.so

Now go back to Sharing, uncheck Web Sharing, wait till it says it stopped, then check it again.  This restarts the web server with the configuration change you just made.  Now refresh your browser and you have your first PHP page served on your Mac. Congratulations!

Leave a Comment

Previous post:

Next post: