How to turn your Mac into a dynamic, database-driven web server

Submitted by Richard Sheppard on Mon, 19/06/2006 - 19:13


In preparation for this presentation, I've done a fresh install of Mac OS X Tiger on an external drive and patched it up to the latest version. I've also installed a few programs that I use in web development and server configuration, including (but not limited to):

Can it get easier than this?

The quickest way to enable the web server on your Mac is by going to System Preferences>Sharing, and then ticking the Personal Web Sharing tick box, as shown below:

Sharing Prefs Pane

Note that this will allow you to access your server via these urls:

  Main Server User's Sub-sites
locally http://localhost http://localhost/~username
locally +
other bon jour
http://your-computer.local http://your-computer.lcoal/~username
via IP address
via domain
http://www.domain.dom http://www.domain.dom/~username

Your static web site files

At this stage, you can decide where you want to work - either using the main directory at

Personally, I tend to work in the Sites directory on my machine. When I get a new web project, I create a new folder with the project name and can then go to http://localhost/~ricardo/project_name/ to view the work.

Another useful tip - drag your Sites directory and/or the /Library/WebServer/Documents/ to the Sidebar in the Finder for easy access to your web page directories.

But I want a dynamic site!

After creating a few html files and linking them to each other, eventually you might get bored and want to create pages on the fly or have information displayed due to the time of day, season, whether the user is logged in, whether the user has a specific role on a site, or any innumerable sets of criteria you put your mind to. Think Amazon, Apple Store, forums, guestbooks, newsletter subscriptions, etc.

Once again, the process for setting up this ability on your Mac is very straight forward.

As seen from the static pages, the web server shipped with OS X is Apache, which is the most popular web server dishing out pages on the Internet. Unlike your standard desktop applications, to adjust the preferences for the Apache server, we will have to dig around under the hood and adjust a specific text file here: /private/etc/httpd/httpd.conf.

The Finder's "Go" menuThere are many ways to get to this file, but I'd like to show you the simplest way on the Mac. Go to the Finder's Go menu and choose Go to Folder... as seen on the left.

Go to a hidden folderYou can then enter the path to the /private/etc/httpd/ here. You will then be able to double click on the file to open it. I've noted on this installation I've done, that the default editor for .conf files is SubEthaEdit, of which I'm not extremely familiar with. I'm certain it will do the job, as will TextWrangler, but I'm going to use BBEdit to make my changes.

BBEdit iconI've been a user of BBEdit for years (read - yearly upgrades for features I'm fairly certain I never use). It has built-in tools to allow you to edit files owned by root. There are four lines of which we will uncomment (remove the # at the beginning of each line):
LoadModule perl_module libexec/httpd/
LoadModule php4_module libexec/httpd/
AddModule mod_perl.c
AddModule mod_php4.c

After saving the file, we will need to restart Apache, and we can do this by turning web sharing on and off in System Preferences>Sharing:

Sharing Prefs Pane

Is it dynamic then?

To see if we've got PHP running, create a new text file in our Sites folder with just this in it:

<?php phpinfo(); ?>

saving it as phpinfo.php and then navigating to http://localhost/~username/phpinfo.php, we should see a full listing of the features that this PHP installation provides, as well as other environmental variables Apache supplies.

Where do we go from here

Subscribe to this how-to at , as I will be adding the mySQL portion shortly.

stonehead28 (not verified)

Sun, 05/11/2006 - 12:31

thanks i have go the php al working

Anonymous (not verified)

Thu, 05/04/2007 - 02:52

is this for a local network or the world wide web? because i can only access it seems through my local network. When i asked my buddy to go to it, nothing came up.

Richard Sheppard

Thu, 05/04/2007 - 11:33

In reply to by Anonymous (not verified)

For your server to be accessible to the world, you'll need to do some other tasks, depending how your access to the internet is set up.
Are you connecting via a router of some sort? If so, many routers provide a way of forwarding web requests made to the router on it's public IP address to the Mac on your network.
I hope this helps.
Kind regards,

Richard Sheppard

Mon, 30/07/2007 - 03:46

Man you rock, but then how do you set up the database ?
Thank you! I guess I don't rock that hard though, as I've not updated this properly or made reference to the update on mySQL that I made a few days after my original posting.
I had some issues with different versions of mySQL, php and the mysqli extension. I've got a sort of kludgy way to work around it here. I hope this helps.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.