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

Prerequisites

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 http://127.0.0.1 http://127.0.0.1/~username
locally +
other bon jour
computers
http://your-computer.local http://your-computer.lcoal/~username
via IP address http://xxx.xxx.xxx.xxx http://xxx.xxx.xxx.xxx/~username
via domain
name
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
/Library/WebServer/Documents/
or
/Users/username/Sites/

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/libperl.so
LoadModule php4_module libexec/httpd/libphp4.so
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 http://www.siliconmeadow.net/mac-as-web-server , as I will be adding the mySQL portion shortly.

Comments

thanks man!

Thanks... I did it!

Well done!

Soon you won't be needing my services!

 

thanks i have go the php al working

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.

Hi,

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

Man you rock, but then how do you set up the database ?

Man you rock, but then how do you set up the database ?

-anonymous

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.

great post thanks for share www.fb.com login

awesome i really appreciate
www.fb.com

In order to convert static website into dynamic website the SQL server is widely used for this purpose and it is indeed one of the most wonderful and excited process but only for those who know it well. Visit the link that is www.intimeessay.com as this is useful for in time creation of essays for the clients.

Thank you for a good guide and detailed explanations!
Always yours ukessays team!

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><p><img><blockquote><h1><h2><h3><h4><h5><h6>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.
 


 The Right Way