Power User Monday Tip of the Week

Web Serving Made Easy Part 3

6010 reads Printer Friendly Page

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:

  1. Open a terminal window
  2. type "cd /etc/httpd"
  3. type "sudo apxs -e -a -n php4 libexec/httpd/libphp4.so"
  4. On OS X prior to 10.2, type
    sudo perl -p -i.bak -e 's%#(AddType S+-php[ -])%$1%i' httpd.conf
  5. On OS X 10.2, type
    echo 'echo "AddType application/x-httpd-php .php" >> /etc/httpd/httpd.conf' | sudo sh -s
  6. No matter what OS type "sudo apachectl graceful"

[via Entropy.ch]

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 it:

phpinfo();

?>

Name the file info.php and place it in your sites folder (it's right off your home folder). Open up http://localhost/~YOUR_USER_NAME/info.php in your browser of choice. This simple script will (hopefully) access the phpinfo() function 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: /Library/WebServer/CGI-Executables/ which is curious because to access the scripts you go to /cgi-bin/script name. 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 /Library/WebServer/CGI-Executables/.

Now, let's get that script working. Fire up the Terminal if it's closed and type:

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 by Apache. CGI scripts 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.

  


All personal comments should be sent to the author. All other discussion should be done in the Forums

[ Back to Power User Monday Tip of the Week | Sections Index ]