Apache Web Serving with Jaguar, Part 2
Pages: 1, 2
In a default installation of Apache on Mac OS X, CGI scripts are only allowed within /Library/Webserver/CGI-Executables/. Uncommenting the above line (removing the
# character) allows CGI scripts to be executed from any user directory, such as /Users/morbus/Sites. In our case, because we aren't using the User directories (because they create ugly URLs for GatesMcFarlaneCo's intranet), we're going to leave the line commented.
If CGI access is turned on already (as per the
LoadModule lines at the beginning of our article), we should be able to reach one of the pre-installed scripts and see a happy result, right? Try
test.cgi). You were probably greeted by a not-so-joyous response: "FORBIDDEN," Apache screams. "You don't have permission to access!"
Huh? Why didn't this work? Now is a perfect time to prove how useful the Apache web server logs can be. If you recall our discussion about
httpd -V above, you'll remember that Apache's access log is located at /var/log/httpd/access_log. Let's look at the very last line of that file, easily reached with
You'll see that the last line looks similar to:
127.0.0.1 - - [19/Feb/2003:20:26:22 -0500] "GET /cgi-bin/test-cgi HTTP/1.1" 403 292
Quickly, this line shows where this particular request came from (127.0.0.1), the time the file was requested, the protocol used (HTTP/1.1), the response code (403), and the size of the response (292 bytes). This is all fine and dandy, but doesn't tell us what went wrong. For this, we'll dip into our error log (pinpointed by the
httpd -V command):
tail /var/log/httpd/error_log . . [Wed Feb 19 20:26:22 2003] [error] [client 127.0.0.1] file permissions deny server execution: /Library/WebServer/CGI-Executables/test-cgi
Bingo! This tells us exactly what went wrong--the file permissions were incorrect. For Apache to run a CGI script, the script in question needs to have "execute" permissions. To give the test-cgi file the correct permissions, do the following in the Terminal:
cd /Library/WebServer/CGI-Executables sudo chmod 755 test-cgi
After running the above (you'll be asked for an administrator's password), load the URL again, and you should be happily greeted with gobs of environment information. (To learn more about permissions and the
sudo, consult your favorite search engine, friendly geek, or O'Reilly-stocked library.)
With the basics of CGI out of the way, you can now install CGI-based applications to complement your intranet. Need a content management system for the GatesMcFarlaneCo developers to keep everyone up to date on their coding progress? Try the ever popular Movable Type.
Turning On Server-Side Includes
Server-side includes, better known as SSIs, allow you to include content from other files or scripts into the page currently being served. This is done by Apache before the page is actually shown to the user--a visitor will never know what you've included or where.
Commonly, SSIs are used to include things such as headers, footers, and "what's new" features across an entire site. When you need to change your site logo's height and width, for instance, you can change the header include only, and the changes will be reflected immediately wherever you've included that file.
SSIs, by default, are turned off. To turn them on, we're going to use the same "search for the feature" trick we did above. Open your Apache configuration file, and search for "server side." Our only match is near the previously seen
AddHandler for CGI scripts:
# To use server-parsed HTML files # # AddType text/html .shtml # AddHandler server-parsed .shtml
Happily, this is exactly what we're looking for. Those simple
Add lines tell us a lot. They establish a pattern based on what we already know about CGI. If you recall, we could have turned on the CGI feature for files ending in .cgi--in other words, any file you created with the .cgi extension (whether it was a CGI program or not), would be treated as an executable script.
Likewise, these lines are telling us that we can turn on the server-side include feature for files ending in .shtml. Whether we actually use the SSI feature in these files doesn't matter--they'd still be treated and processed as if they did.
This is important. You may be thinking "If SSIs are so great, why not enable them for .html filenames?" Ultimately, it's a matter of speed. If you have 3,000 .html files, and only 1,000 of them actually use SSI, Apache will still look for SSI instructions in the other 2,000. That's a waste of resources. Granted, processing SSI incurs very little overhead, but if you're being hit 50,000 times a second, it can certainly add up. This isn't too worrisome for our GatesMcFarlaneCo intranet, but is good to know for your future Apache projects.
For now, uncomment the
AddHandler lines. This will turn on the SSI mojo power. But where? When we were learning about CGI, we saw a configuration setting that said our CGIs lived in /Library/Webserver/CGI-Executables/--we now have to tell Apache where to enable our server-side includes.
Because we've building an intranet that's going to live in /Library/Webserver/Documents (Apache's
DocumentRoot), that's where we want our SSI capability to be active. Go to the top of your Apache configuration file and search for "/Library/Webserver/Documents/". The second result looks like the following (we've removed the commented lines from this example):
<Directory "/Library/WebServer/Documents"> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny Allow from all </Directory>
You'll notice that this looks similar to the
<Directory> entry we saw when we were looking into CGI. As before, we're going to skip the bulk of it (we'll pay attention to our ignored lines a little bit later in our series). For now, add the word
Includes to the
Options line, like so:
Options Indexes FollowSymLinks MultiViews Includes
Options is an Apache directive that can turn on or off different features for the indicated
<Directory> and all subdirectories beneath it. Subdirectories can override their parent configuration. In future articles of our series, we'll talk a bit more about the different configurations available to
Because we've made changes to Apache's configuration file, we now need to restart Apache (so that it reloads /etc/httpd/httpd.conf). The easiest way to do this is via our Sharing preference panel. Much like we started the web server in Part 1, we now need to stop and then start it to enable our changes. Do this now. Chuckle once or twice, if you must.
Note: If, after you've clicked Start, the status continually shows "Web Sharing starting up..." for more than a few seconds, you may have an error in your Apache configuration file, caused by a mishap in your editing. The Sharing preference assumes a valid configuration file, and will stall until it receives a positive response from Apache (which it'll never get). To check your configuration for correctness, enter
httpd -t in a Terminal--if there's an error, you'll be told the line number to investigate.
To test that SSIs are working properly, rename the index.html we created in part one to index.shtml (since .shtml is the only extension we've enabled SSIs for), and edit to match the snippet below:
<html> <body> <h1>Gleefully Served By Mac OS X</h1> <pre> <!--#include virtual="/cgi-bin/test-cgi"--> </pre> </body> </html>
Here, we're including the test CGI script into the contents of our main index page. When you load
http://127.0.0.1/index.shtml into your browser, you'll see our "Gleefully Served" message, as well as the output of the CGI script itself. We could have easily created a static file (navigation.html) and included that within index.shtml instead. Note: Changing the file extension to .shtml forces us to use a slightly longer URL--
http://127.0.0.1/index.shtml and not just
http://127.0.0.1/. We'll examine why (and how to fix it using
DirectoryIndex) in a future installment of our series.
SSI is configured and working, but what can you do with it? What if your marketing department wants to create an image gallery of the newest ads they've planned? For an advanced look at the possibilities of SSI, check out this SSI Image Gallery article, also written by yours truly.
In our next installment, I'll walk you through how to turn on PHP and test that it's working correctly. Until then, good luck working with CGI and server-side includes, and be sure to Talkback if you've got questions I've yet to answer.
Kevin Hemenway is the coauthor of Mac OS X Hacks, author of Spidering Hacks, and the alter ego of the pervasively strange Morbus Iff, creator of disobey.com, which bills itself as "content for the discontented."
Return to the Mac DevCenter.
2006-01-20 09:48:27 jwmci [View]
2005-09-19 20:59:38 DickG [View]
2005-04-21 15:43:05 Gus_Dueñas [View]
2004-06-22 22:19:15 UrbanDerelict [View]
Hopefully this will help
2006-07-15 19:54:55 jahuda [View]
2004-06-03 06:41:24 mactosh [View]
RE: missing SSI output!
2006-07-15 19:59:12 jahuda [View]
2004-03-01 11:51:29 dstankalis [View]
2004-01-28 20:59:16 mentalstasis [View]
2004-01-26 09:43:46 russguy [View]
test cgi files
2004-08-23 20:17:26 Tiamot [View]
2004-01-11 06:44:03 anonymous2 [View]
2004-01-10 23:53:44 anonymous2 [View]
2003-09-25 07:31:28 musicarr [View]
2003-09-23 14:25:54 anonymous2 [View]
2003-09-07 12:46:33 anonymous2 [View]
2003-09-23 14:24:05 anonymous2 [View]
2003-07-10 05:49:24 anonymous2 [View]
2003-07-09 02:03:05 anonymous2 [View]
2004-02-07 12:51:07 ecleo [View]
2003-09-18 09:48:22 anonymous2 [View]
2003-09-16 13:34:16 ecleo [View]
2003-07-19 07:48:34 anonymous2 [View]
2003-07-25 20:38:25 anonymous2 [View]
2003-06-17 11:20:53 anonymous2 [View]
unable to :w to httpd.conf
2003-07-09 01:46:11 anonymous2 [View]
unable to :w to httpd.conf
2003-06-20 14:49:15 anonymous2 [View]
2003-06-16 08:18:40 anonymous2 [View]
SSI not recognized
2003-06-27 08:19:37 anonymous2 [View]
2003-06-16 08:18:22 anonymous2 [View]
2003-05-20 18:51:50 anonymous2 [View]
CGI for 'ugly' user directories?
2003-05-20 18:58:38 anonymous2 [View]
2003-05-20 19:36:29 anonymous2 [View]
2003-05-10 17:48:26 anonymous2 [View]
Np o Permission for shtml
2003-06-11 16:42:30 anonymous2 [View]
2003-05-02 11:29:35 anonymous2 [View]
Proxy Server problem? - Possible Answer.
2003-05-06 07:24:05 anonymous2 [View]
Proxy Server problem?
2003-05-05 16:31:25 Kevin Hemenway | [View]
2003-04-19 06:43:05 anonymous2 [View]
2003-04-14 12:22:19 anonymous2 [View]
Re: Please help
2003-04-14 18:57:36 anonymous2 [View]
2003-04-12 04:29:56 anonymous2 [View]
2003-04-11 08:16:34 anonymous2 [View]