Uli's Web Site
[ Zathras.de - Uli's Web Site ]
Other Sites: Stories
Pix
Abi 2000
Stargate: Resurgence
Lost? Site Map!
 
 
     home | blog | moose | programming | articles >> blog

 Blog Topics
 
 Archive
 

15 Most Recent [RSS]

 Less work through Xcode and shell scripts
2011-12-16 @600
 
 iTunesCantComplain released
2011-10-28 @954
 
 Dennis Ritchie deceased
2011-10-13 @359
 
 Thank you, Steve.
2011-10-06 @374
 
 Cocoa Text System everywhere...
2011-03-27 @788
 
 Blog migration
2011-01-29 @520
 
 All you need to know about the Mac keyboard
2010-08-09 @488
 
 Review: Sherlock
2010-07-31 @978
 
 Playing with Objective C on Debian
2010-05-08 @456
 
 Fruit vs. Obst
2010-05-08 @439
 
 Mixed-language ambiguity
2010-04-15 @994
 
 Uli's 12:07 AM Law
2010-04-12 @881
 
 Uli's 1:24 AM Law
2010-04-12 @874
 
 Uli's 6:28 AM Law
2010-04-12 @869
 
 Uli's 3:57 PM Law
2010-04-12 @867
 

More...

Activating PHP on MacOS X

Starting Apache

[The Sharing icon in the Internet and Network section of System Prefs]

You may not know it, but every copy of MacOS X comes with its own copy of Apache, the internet server most used on the 'net. It is fairly simple to activate it:

  1. Choose "System Preferences..." from the Apple menu.
  2. Click the little folder icon named "Sharing" to get to the preference panel of that name.
  3. If you're running MacOS 10.2 or later, check the check box next to "Personal Web Sharing" on the first tab. If you're running an older MacOS X version, click the "Start" button next to "Personal Web Sharing".

[The Web Sharing checkbox]

If you're a 10.2 or later user, remember the "Computer Name" ("Rendezvous name" in earlier Mac OS X releases) at the top of the window. You can use that to try out your web server while you're offline or from a local network.

[The Computer Name edit field]

Ready to try it out? OK, start up the web browser of your choice, and enter the Bonjour Name of your Mac as the URL. In my case, that would be slab.local. If you're running an older MacOS X without Bonjour, enter http://127.0.0.1 instead, which is the loopback address that tells your computer to connect to itself. You'll see a sample page put there by Apple. This page can be found in /Library/WebServer/Documents/

There is also a page for each user. Simply append a slash, a tilde (the little wave-thingy on your keyboard) and your short user name to the address. E.g. http://macrovigor.local/~witness and you'll get the default page for that user. This second page can be found in this user's "Sites" folder. Go ahead and replace it with the HTML files for your own web site. Cool, isn't it? If you go to the "Network" preferences panel and note down the IP-Address mentioned there, you can even have someone else use it to access your server. Simply use that IP address instead of your Bonjour name.

Warning:If you're no internet guru, I strongly suggest you turn off Personal Web Sharing after you've played with it. Personal Web Sharing lets other people access files on your computer. While this is by default restricted only to files in your "Sites" folder, it is very easy to accidentally grant access to other files on your hard disk if you make a simple small mistake. Better safe than sorry.

Activating PHP

Those of you who have already used PHP may have tried to copy their PHP files into the Sites folder to notice that they don't work. Very likely, your browser offered them for download. Not quite what we want, eh? So, what do we do?

First of all, get comfortable with the Terminal, which you can find in /Applications/Utilities/. Start it up, and type

sudo pico /etc/apache2/httpd.conf

On versions of Mac OS X prior to 10.5, write sudo pico /etc/httpd/httpd.conf.

sudo means that you want root access, which in turn means that the operating system will let you do lots of dangerous things. In English, this means that if you screw up beyond this point, you'll very likely find yourself reinstalling your system. Be sure to make a backup of any important files, I'm not responsible if you screw up. You have been warned.

pico is a text editor. I've chosen it for one simple reason: It is easy to learn how to use it, because it lists all important commands at the bottom. While it may not be as powerful as vi or emacs (which are also available), at least I can never forget how one quits the application. Yeah, my memory is that bad.

The final part of what you typed is the path to Apache's configuration file, "httpd.conf". It contains a long text list of settings for Apache.

OK, now hit return, and you'll get a nice text screen with a list of things at the bottom. Note that the little triangle-thingie in the black boxes is used to represent the "ctrl" key. I.e. to quit pico, you would use the shortcut Ctrl-X (instead of the Apple-Q we all know and love). What we want to do now, is look for all lines related to PHP. Type the shortcut for "Where is", which means "Find" and then type "PHP"and hit return.

You should arrive at a line that reads

#LoadModule php5_module          libexec/httpd/libphp5.so

Remove the hash sign ("#") at the start of the line to activate this option. Depending on your version of Mac OS X, you may get a different number than 5, because it comes with a different version of PHP.

If you are running running Mac OS X 10.4.11 or earlier, do another Ctrl-W and just hit return again, which means "Find Again", until you end up at the following line:

#AddModule mod_php4.c

Remove the hash sign there as well. If you're running an older (pre-10.2) version of MacOS X, you can hit Ctrl-W and return a couple more times to end up at the line we're adding to the file "mime.types" below. It used to be in httpd.conf as well, but the Apple folks decided to clean things up a bit and move it. If your httpd.conf contains that line, simply remove the hash from that line as well and don't mess with mime.types.

Now search for "DirectoryIndex". Change the line to read something like the following:

DirectoryIndex index.html index.htm index.php

As the comment in the file says, order is important. What it says the way I wrote it is that Apache will first look for index.html, if that doesn't exist it'll look for index.htm, and if that isn't there it will look for index.php before just showing a directory listing. Change the order if you want it to be different.

Now that this is finished, use WriteOut to save your changes to the file, then quit pico. If you're using Mac OS X 10.4.11 or earlier, you need to make sure PHP files are recognized as belonging to the PHP module we activated above. To do that, type:

sudo pico /etc/httpd/mime.types

You'll get a huge list of MIME type names, followed (sometimes) by the file name suffix that belongs to them. Scroll down to "application/x-hdf hdf" and enter the following lines after it:

application/x-httpd-php         php
application/x-httpd-php-source  phps

This makes sure that when Apache sees a file name ending in PHP, it will know to hand it to the libphp module that we just turned on using LoadModule and AddModule in httpd.conf. Save your changes to mime.types.

Once the settings have been changed to allow PHP, you only need to tell Apache to reload its settings by going back to the "Sharing" Prefs panel and turning off and on Personal Web Sharing. This will restart Apache and it will re-read its httpd.conf (and mime.types) and pick up the changes we made.

Voila, you have just turned on PHP on your MacOS X box. Everything you do from here on happens at your own risk.

Update: Changed to be current for Mac OS X 10.5 "Leopard".

Reader Comments: (RSS Feed)
Neil Nguyen writes:
Note that for Mac OS X 10.5 (Leopard), the httpd.conf file used by Apache is in a different location: /private/etc/apache2/httpd.conf. There still exists a /etc/httpd/httpd.conf file, but editing that file will have no effect whatsoever. You must edit the one in the /private/etc/apache2 folder.
Uli Kusterer replies:
@Neil: /etc/apache2 is a symlink to /private/etc/apache2, so editing both should work just fine.
Steve writes:
Excellent job... well written and clear. It was a great help, thanks!
jonah writes:
Thanks Uli, I love my MacBook even more now.
larisa writes:
excellent, clear instructions. i was trying to install php onto my mac and all i had to do was enable it. thanks for your help!
Steve writes:
Thanks for the instructions! Where do I go to clean out the failed installations of PHP? Unfortunately the installer does not reveal the installation location. Thanks, Steve
Steve writes:
... in other words, is there anything that needs to be cleared out besides the contents of the /usr/local/php5 directory? I figured I should have clarified. Thanks!
Uli Kusterer replies:
I can't tell. Every installer is different. But if you have an installer that was built using PackageMaker (standard Mac installer), you can get it to display its file list. There's a menu item when you open the installer, that lists all the paths.
Peter writes:
Hi Uli, It was all going very well until the search command could not find any instance of PHP. Do I need to go and find and install it first? Is my Mac the only one that came without it? Is there any chance you can help me with this please? Best, Peter...
Uli Kusterer replies:
Peter, no, it's on every Mac. Did you get no "php" found in both files? I'd have to look again, but it might be unnecessary to do some of these steps on newer Mac OS X versions.
Matt Burns writes:
i followed the instructions, but it won't let me re-enable personal web sharing. i let it sit for over an hour, and nothing. i've gone through the instructions again, and everything that was supposed to be changed was changed. do you have any suggestions? is there a way i can reset the file and repeat the process in case i altered something else by mistake?
Claudia writes:
sudo pico /etc/httpd/mime.types After I entered that, there was no MIME type lists showing up, it was just blank..
cfarm writes:
Hi, great read and very helpful. I was wondering, my apache config file didn't have the #AddModule mod_php4.c library. I skipped over the line and it seems to compile php just fine but am wondering how important that is. Thanks
Uli Kusterer replies:
cfarm, as the article says, theAddModule line is only needed on 10.4 and earlier
simone writes:
sorry for the bad english but i'm italian. when i activate php following your instruction automatically i activate apache or i need a differen way to do that? my mac is 10.4.11 version.
daniel farmer writes:
note : for 10.4.11 users. Apache Web root is : /Library/Webserver/Documents/
Or E-Mail Uli privately.

 
Created: 2004-03-26 @852 Last change: 2024-04-25 @358 | Home | Admin | Edit
© Copyright 2003-2024 by M. Uli Kusterer, all rights reserved.