CGI Programming With Apache and Perl on Mac OS X
Since Mac OS X already has Apache and Perl installed, all you need to do is configure the Apache server to allow CGI programs.
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, a powerful text editor from Bare Bones Software. (This is a commercial product, but they do have a free 30-day demo you can use to try it out.) BBEdit offers syntax-coloring, Perl syntax checking, a "run" command (allowing you to run your programs), and lots more. It's primarily an HTML editor, so you also get the full suite of HTML tools as well.
- 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://126.96.36.199/)*. 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/httpd/httpd.conf. If you have BBEdit, use "Open File By Name" from the File menu to specify the path to the file to edit:
BBEdit "open file by name"
(If you don't have BBEdit, you'll have to edit the file the hard way - in the Unix shell. Open the Terminal application, then type
sudo pico /etc/httpd/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, scroll down (or use Find) until you get to the line that says "# Control access to UserDir directories". Scroll down just past that and you'll come to a commented section for Directory:
#<Directory /home/*/Sites> # AllowOverride FileInfo AuthConfig Limit # Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec # <Limit GET POST OPTIONS PROPFIND> # Order allow,deny # Allow from all # </Limit> # <LimitExcept GET POST OPTIONS PROPFIND> # Order deny,allow # Deny from all # </LimitExcept> #</Directory>
Uncomment this entire section (by removing the pound signs at the beginning of each line), and change the
Optionsline to this:
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
Now scroll down several pages (or use Find) to the AddHandler section. Uncomment the CGI line:
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 the AddHandler line for server-parsed, and change the extension from .shtml to .html:AddHandler server-parsed .html
This causes all .html files to be searched for server-side include tags.
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.)
To restart Apache, go to the System Preferences panel and select the "Sharing" icon:
Check "Personal 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, look at http://localhost/ (or http://10.0.1.2) in your browser to ensure that the server restarted successfully.
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.
The site-wide test page
A better solution is 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.
Your new personal web page
Writing Your CGI ProgramsNow 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.
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.
- Apache Web-Serving with Mac OS X
- Apache Web Serving With Jaguar, part 1. How to start and stop Apache on your mac; where to put your HTML pages.
- Apache Web Serving With Jaguar, part 2. Editing the config file to enable CGI programs. (For the impatient, the config file is typically located in