CGI Programming With Apache and Perl on Mac OS X
These instructions are for Apache 2.2, which comes already installed on new Macs. If for some reason you're using an older mac with Apache 1.3, click here for the old configuration instructions.
You're going to need a text editor, both for editing the config files and for writing your CGI programs. You can get by with using TextEdit, Apple's free text editor. Or you may want to check out BBEdit or TextMate.
- Who can see your website?
- Programming Locally, then Uploading to the ISP
- Configuring Apache on Mac OS X
- Viewing Your Site
- Writing Your CGI Programs
- Accessing the Unix Shell
Who can see your website?
If you have a permanent, fixed IP address for your computer (e.g. your computer is in an office, or you have your own T1 line), your Apache server will be able to serve pages to anyone in the world*. If you have a transient IP address (e.g. you use a dialup modem, DSL modem or cable modem to connect to the internet), you can give people your temporary IP address and they can access your page using the IP address instead of a host name (e.g, http://18.104.22.168/)*. But when you logout, your server will obviously not be connected, and when you dial in again you'll probably have a different IP address.
Obviously for permanent web hosting, you should either get a fixed IP address (and your own domain name), or sign up with an ISP that can host your pages for you.* Unless you're behind a firewall, and the firewall is not configured to allow web traffic through.
Programming Locally, then Uploading to the ISP
You may want to develop and debug your programs on your own computer, then upload the final working versions to your ISP for permanent hosting. Since OS X is Unix, all of the programs shown in CGI Programming 101 should work seamlessly both on your Mac and on a remote Unix host/ISP.
Configuring Apache on Mac OS X
You need to modify the Apache configuration file to tell it where your pages are, and enable CGI programs. The config file is located in
/etc/apache2/httpd.conf. If you have BBEdit, use "Open File By Name" from the File menu to specify the path to the file to edit. If you're using TextEdit or TextMate, use File->Open and then type Command-Shift-G to show the "Go to Folder" window, then enter
/etc/apache2in the input box.
You can also use the Terminal application, then type
sudo pico /etc/apache2/httpd.confto edit the file. You'll be moved to a text editor inside of Terminal. Use the arrow keys to move around. Help and instructions on the pico editor will appear along the bottom of the window. When you're done editing, use control-X to quit out of pico (you'll be asked if you want to save the file or not).)
Once you have the httpd.conf file in your editor, use Find (⌘-F) to find "AddHandler". Uncomment the
cgi-scriptline (remove the "#" that appears before this text):
AddHandler cgi-script .cgi
This causes any file with a .cgi extension to be processed as a CGI program. If you want to also have files with a .pl extension be processed as CGI programs, add the .pl extension on that same line:
AddHandler cgi-script .cgi .pl
Also uncomment these two lines to allow for server-side includes:AddType text/html .shtml AddOutputFilter INCLUDES .shtml
Now save the configuration file. (If you're using BBEdit, you'll be prompted for your system administrator password; this is required since the config file is owned by the root user.)
Edit the User File
You also need to modify the configuration file for your userid. Follow the same process as before: File->open then Command-Shift-G to openFirst you'll need to change the
/etc/apache2/users. Look for the conf file for your userid (e.g. if your username is "ted" then the file is named "ted.conf".) Open the file.
Optionsline to add some more parameters:
Options MultiViews Indexes SymLinksIfOwnerMatch Includes ExecCGI
Options specifies what options are available in this directory. The important ones here are Indexes, which enables server-side includes, and ExecCGI, which enables CGI programs in this directory.
After the Options line, add the following DirectoryIndex line:
DirectoryIndex index.html index.cgi
Save the file.
To restart Apache, go to the System Preferences panel and select the "Sharing" icon:
Check "Web Sharing" to start Apache. (You may have to click the lock in the lower left-hand corner to unlock the sharing panel and allow you to make changes.)
Once web sharing is on, the right side of the sharing panel will show you links to your mac's main web page (in the above example it's http://192.168.1.100/), as well as to your personal web area (your Sites folder). You can click on those links to confirm that Apache is running.
Viewing Your Site
http://localhost/ is the homepage for your site; it shows the index.html (or more specifically index.html.en) page located in the /Library/WebServer/Documents folder. You can change the location for the server-wide files by changing the DocumentRoot directive in the httpd.conf file.
For developing your own pages and CGI programs, you'll want to use the Sites folder in your home directory. You can view pages in the Sites folder by looking at http://localhost/~yourusername/ in your browser. For example, my short username is "kira", so the URL to my pages is http://localhost/~kira/. This displays the index.html file located in /Users/yourusername/Sites/. The "Sites" folder in your home directory is where you can put all of your HTML and CGI programs.
Writing Your CGI Programs
Now you're ready to write some CGI programs! Here's a simple one you can use to get started. You can write this in your choice of text or Perl editor:#!/usr/bin/perl -wT print "Content-type: text/html\n\n"; print "<h2>Hello, World!</h2>\n";
Save the file in your Sites folder as "first.cgi", then go to http://localhost/~yourusername/first.cgi to view it.
If you get an "Internal Server Error", that probably means you need to fix the permissions on the file. Launch the Terminal app (its in Applications->Utilities), type
cd Sites, then
chmod 755 first.cgi. We'll look at this (and other debugging techniques) in Chapter 1 of the book.
Now you're ready to go to Chapter 1 and start learning CGI programming.
Accessing the Unix Shell
Your Mac is built on Unix, so you can access the Unix Shell by launching the Terminal application (in Applications/Utilities). A new terminal window will appear and you'll be connected in your home directory. Type
ls -lto see the contents of your home directory as it looks in Unix.
Consult the Unix tutorial to learn more about working in the shell.