Uli's Web Site
Activating PHP on MacOS X
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:
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.
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.
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:
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".
Created: 2004-03-26 @852 Last change: 2021-09-21 @139 | Home | Admin | Edit|
© Copyright 2003-2021 by M. Uli Kusterer, all rights reserved.