Apr 27 2009
By: Jon Gales
Last week we learned the finer points of IP forwarding. Hopefully you’ve gotten
that under control. This week we’ll learn how to do some fun stuff with PHP
and CGI. Next week will be the last lesson before the first application (setting
up a weblog).
PHP is the world’s most popular web scripting language. It’s GREAT. It’s also
installed on your computer already, you just have to activate it. Here’s how:
- Open a terminal window
- type “
- type “
sudo apxs -e -a -n php4 libexec/httpd/libphp4.so”
- On OS X
prior to 10.2, type
sudo perl -p -i.bak -e 's%#(AddType S+-php[ -])%$1%i' httpd.conf
- On OS X 10.2,
echo "AddType application/x-httpd-php .php" >> /etc/httpd/httpd.conf'
| sudo sh -s
- No matter what OS type “sudo apachectl graceful”
Now fire up your plain text editor of choice (BBedit or TextEdit set to plaintext
mode or anything else that writes plain text) and drop the following code into
Name the file info.php and place it in your sites folder (it’s right off your
home folder). Open up
in your browser of choice. This simple script will (hopefully) access the
which will output the specifics of your install to your browser. If PHP has
been successfully activated, you’ll
see a nice long page full of info you probably won’t care about. If it doesn’t
work, you’ll most likely see the text of the script. Go back and try again.
Make sure you did the right commands for the right version of OS X.
Once you’ve got PHP up and running, try your hand at CGI (it’s already active
so it’s quite easy). Point your browser to http://localhost/cgi-bin/test-cgi and you’ll note that there is an error. It’s not the common 404 not found error,
it’s the Forbidden error. From a web developer point of view, this is a much
worse error. In this case it’s not hard to solve.
The set up that OS X ships with puts CGI scripts in the directory:
is curious because to access the scripts you go to
Most people would thing that there would be a directory called cgi-bin inside
/Library/WebServer/Documents. Get over it. If you need the techie
explanation head over to O’Reilly’s
Mac Dev Center. For the rest of you, just remember
CGI goes in
Now, let’s get that script working. Fire up the Terminal if it’s closed and
sudo chmod 755 /Library/WebServer/CGI-Executables/test-cgi
This runs the chmod program as super user. You’ll be prompted for a password,
just give it the administrator password. The 755 changes the permissions for
the file test-cgi to a level that can be executed
are actually programs, and for programs to be run the user has to have the
correct permissions. Now, go back to http://localhost/cgi-bin/test-cgi and
note the output. You’ve now just run your first CGI script!
These two skills will be very important in the near future, even if you aren’t
a programmer. There are thousands of free programs that can be run with these
techniques which can speed things along when you’re making a site. Two weeks
from now we’ll meet a few of the more well known ones.