Apache Web-Serving with Mac OS X: Part 2
Pages: 1, 2
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 -- they'll 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 the background color of your site, for instance, you can change the header file only, and the color 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 grants us the following:
# 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 above, 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 AddType and 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 we
want our SSI capability to take place.
Because we've building an
intranet, that's going to live in /Library/Webserver/Documents, 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 brunt 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. Options is an Apache
directive that can turn on or off different features for the
<Directory> and all subdirectories beneath it. Subdirectories can
override their parent configuration.
Because we've made changes to
Apache's configuration file, we now need to restart Apache. The easiest way to do
this is via our Sharing preference panel. Much like we started the sharing preference in part one of this series, we can stop and start to enable our changes. Do this now. Chuckle once or twice, if you must.
To test that our SSIs are working properly, rename your index.html file to index.shtml (because .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/ into your browser, you'll see our
"Gleefully Served" message, as well as the output of the CGI script itself.
We could just have easily created a static web page (say,
"navigation.html"), and included that within our page instead.
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? That's quite easy using this SSI Image Gallery article, also written by yours truly.
In my next installment, I'll show you how to turn on PHP. Until then, good luck working with CGI. And if you have questions ... comment below and the author will get back to you.
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.
-
real helpful
2010-08-30 10:17:32 sha69 [View]
-
Creating Your own test.cgi file and cgi-bin
2009-12-28 11:36:11 stumpygreg [View]
-
Creating Your own test.cgi file and cgi-bin
2010-06-30 06:25:57 peter55 [View]
-
httpd -v with different results
2008-06-29 04:31:14 NickBadAtMac [View]
-
httpd -v with different results
2008-08-02 21:47:44 wwwalker [View]
-
no test-cgi
2008-06-29 01:01:33 NickBadAtMac [View]
-
location change for apache config files
2008-06-11 18:24:58 robbel [View]
-
location change for apache config files
2008-11-03 15:52:40 mcteapot [View]
-
NO TEST.CGI
2007-12-11 11:40:41 Tymierb [View]
-
Out of date and need a hand
2007-08-15 08:09:05 Michael_Janzen [View]
-
Out of date and need a hand
2007-08-15 09:59:10 Michael_Janzen [View]
-
Cannot get perl script to work using cgi
2007-05-30 10:21:35 AstroNerd [View]
-
Cannot get perl script to work using cgi
2007-09-05 10:32:22 abajay [View]
-
No permission to save httpd.conf
2007-05-23 05:30:45 aquilai [View]
-
No permission to save httpd.conf
2008-08-28 13:50:00 Roger LaPrelle [View]
-
No permission to save httpd.conf
2007-10-06 16:00:22 soundmagic1943 [View]
-
i dont understand how to find the httpd.conf file HELP
2007-04-17 01:44:09 jarodvonhoff [View]
-
editing user.conf versus httpd.conf file
2007-02-28 14:47:53 zimboden [View]
-
editing user.conf versus httpd.conf file
2008-01-28 09:07:27 Alabamian [View]
-
editing user.conf versus httpd.conf file
2007-11-25 11:43:06 0really [View]
-
editing user.conf versus httpd.conf file
2007-02-28 14:53:35 zimboden [View]
-
errata in this file
2007-01-21 10:16:08 imagedoc [View]
-
Panther issue
2005-01-08 22:49:17 jxpx777 [View]
-
Panther issue
2005-02-27 15:11:54 turbo-gi [View]
-
Error Message : test-cgi not found
2004-11-09 23:49:08 ZeeshanMaqbool [View]
-
Error Message : test-cgi not found
2004-11-24 20:19:41 idenrosenthal [View]
-
Error Message
2004-09-23 08:58:54 MikeBroderick [View]
-
Uncommenting Apache config file
2004-08-05 16:54:50 mstumpy [View]
-
Use sudo
2004-08-06 13:39:56 mstumpy [View]
-
test-cgi, printenv missing
2004-03-31 19:31:32 monica1 [View]
-
cgi not working with external programs.
2004-03-03 16:57:03 juferban [View]
-
questionable log entries
2003-10-02 09:55:53 jmedley [View]
-
questionable log entries
2003-12-14 14:41:20 anonymous2 [View]
-
questionable log entries
2003-11-01 07:04:10 anonymous2 [View]
-
Directory directives not working
2003-09-04 10:17:58 anonymous2 [View]
-
Still not getting .shtml to work right.
2003-08-30 09:53:49 anonymous2 [View]
-
Re-Reading httpd.conf without restarting apache
2003-07-01 05:58:19 anonymous2 [View]
-
Finally got SSI to work! Here's what I ran into...
2003-05-19 17:41:01 anonymous2 [View]
-
THANK YOU
2004-10-17 20:59:56 LostBurner [View]
-
CGI write to file
2003-05-15 13:33:13 mmcleod1 [View]
-
CGI write to file
2003-12-17 17:34:38 anonymous2 [View]
-
Permissions Trouble
2003-05-11 21:10:33 anonymous2 [View]
-
Found the Answer
2003-05-12 12:09:48 anonymous2 [View]
-
Permissions Trouble
2003-05-11 23:25:44 anonymous2 [View]
-
where is httpd.conf
2003-03-29 16:33:03 anonymous2 [View]
-
where is httpd.conf
2003-03-29 17:14:26 anonymous2 [View]
-
Premature end of script headers
2003-03-21 06:41:28 anonymous2 [View]
-
Premature end of script headers
2003-06-02 12:44:28 anonymous2 [View]
-
Premature end of script headers
2003-12-15 17:09:44 anonymous2 [View]
-
Premature end of script headers
2003-04-30 04:32:55 anonymous2 [View]
-
httpd.conf
2003-03-17 11:45:04 anonymous2 [View]
-
still don't have permission to access /cgi-bin/ after using "chmod"
2003-01-12 01:04:56 anonymous2 [View]
-
still don't have permission to access /cgi-bin/ after using "chmod"
2003-04-19 12:00:45 anonymous2 [View]
-
still don't have permission to access /cgi-bin/ after using "chmod"
2003-05-11 23:19:50 anonymous2 [View]
-
LoadModule command is not working...
2002-12-31 10:13:50 anonymous2 [View]
-
http-conf problems
2002-12-23 12:11:35 sweyhrich [View]
-
http-conf problems
2003-03-28 01:31:13 anonymous2 [View]
-
http-conf problems (I Stuffed Up Big Time)
2005-04-13 05:03:36 Lindz [View]
-
odd access file????
2002-11-01 21:23:17 anonymous2 [View]
-
no luck at all with SSI
2002-10-23 14:30:30 anonymous2 [View]
-
no luck at all with SSI
2003-02-20 14:21:46 jkurson [View]
-
^M Problem Has A Simple Solution or Not?
2002-10-11 06:30:48 anonymous2 [View]
-
^M Problem Has A Simple Solution or Not?
2002-11-28 07:09:24 yyyyy [View]
-
Macintosh line returns in scripts (^M)
2003-02-17 11:00:04 kenv [View]
-
Messing with Python cgi...
2002-10-07 17:14:24 elmlish [View]
-
cgi's in sites dir
2002-10-03 07:28:02 dragonfly1 [View]
-
Custom cgi's not working
2002-10-01 21:28:43 ccalfee [View]
-
apache on os x
2002-09-28 09:49:43 anonymous2 [View]
-
Apache won't start after Include added
2002-09-24 12:15:52 anonymous2 [View]
-
Apache won't start after Include added
2002-09-25 15:10:35 philocon [View]
-
To Morbus
2002-09-12 03:16:55 epvb [View]
-
gfdg
2003-11-10 00:25:22 anonymous2 [View]
-
Still not getting .shtml to work right.
2002-08-26 09:17:21 oliverspapa [View]
-
Still not getting .shtml to work right.
2002-09-25 16:45:04 philocon [View]
-
Again cgi-test
2002-08-15 08:02:03 pathell [View]
-
http://127.0.0.1/...plus index.shtml...needed?
2002-08-04 19:03:44 bobmoore [View]
-
ScriptAlias: Command not found.
2002-07-28 07:31:54 ferrit [View]
-
Re: ScriptAlias: Command not found. - Don't worry, got it now !!
2002-07-28 08:52:18 ferrit [View]
-
perl location
2002-06-21 14:26:15 bluesy [View]
-
Can't see index.shtml page
2002-06-20 14:09:59 ltwiebe [View]
-
Can't see index.shtml page
2002-06-20 14:19:12 ltwiebe [View]
-
bbedit /etc/httpd/httpd.conf
2002-06-01 22:48:22 cmgarofalo [View]
-
bbedit /etc/httpd/httpd.conf
2002-06-20 14:17:44 ltwiebe [View]
-
bbedit /etc/httpd/httpd.conf
2002-07-07 17:58:31 thegws [View]
-
bbedit /etc/httpd/httpd.conf
2002-06-11 15:44:53 macette [View]
-
bbedit /etc/httpd/httpd.conf
2002-06-11 15:44:50 macette [View]
-
Problems getting scripts running.
2002-05-01 17:09:33 cwdegidio [View]
-
Problems getting scripts running.
2002-05-07 05:03:25 david.parrish@buentec.com [View]
-
problems with httpd.conf
2002-04-28 18:57:47 beefyt [View]
-
ahh, fixed it
2002-04-28 19:13:46 beefyt [View]
-
ahh, fixed it
2002-04-29 04:21:26 Kevin Hemenway |
[View]
-
restarting server
2006-06-22 05:39:40 carlco2 [View]
-
acgi
2002-04-24 03:40:29 kkniel [View]
-
acgi
2002-04-24 19:58:00 Kevin Hemenway |
[View]
-
Kill Apache???
2002-04-22 07:58:47 jblarsen [View]
-
Kill Apache???
2002-04-23 15:40:11 Kevin Hemenway |
[View]
-
Includes problem
2002-04-12 05:07:06 bernad [View]
-
Includes problem
2002-04-12 05:15:25 bernad [View]
-
Help Includes
2002-04-06 01:59:01 edo [View]
-
Help Includes
2002-04-16 17:02:05 Kevin Hemenway |
[View]
-
Help Includes
2002-04-25 03:31:58 edo [View]
-
PATH Environment Variable from printenv
2002-04-03 20:48:46 yaruki [View]
-
PATH Environment Variable from printenv
2002-04-16 17:00:20 Kevin Hemenway |
[View]
-
The CGI Basics....Please help
2002-03-28 16:11:48 beanie [View]
-
The CGI Basics....Please help
2003-03-29 16:30:37 anonymous2 [View]
-
The CGI Basics....Please help
2003-12-27 16:19:06 anonymous2 [View]
-
The CGI Basics....Please help
2002-03-28 17:39:04 beanie [View]
-
The CGI Basics....Please help
2002-04-16 16:55:07 Kevin Hemenway |
[View]
-
test-cgi causes Internal Server Error
2002-03-27 22:17:29 tcolin [View]
-
test-cgi causes Internal Server Error
2002-04-03 15:43:33 danastasi [View]
-
Why is chmod not permtted?
2002-03-25 15:55:38 beanie [View]
-
Why is chmod not permtted?
2002-03-28 08:40:03 indigo [View]
-
Why is chmod not permtted?
2002-03-27 16:40:43 beanie [View]
-
retarting Apache...
2002-03-24 22:40:54 senecasax [View]
-
retarting Apache...
2002-03-25 08:24:23 senecasax [View]
-
surely there's a better way?
2002-06-11 06:29:36 macette [View]
-
thanks all
2002-03-23 12:42:08 greenfruit [View]
-
CGI - ScriptAliasMatch
2002-03-11 13:00:22 kenyatta [View]
-
CGI - ScriptAliasMatch
2002-03-11 17:05:32 Kevin Hemenway |
[View]
-
CGI - ScriptAliasMatch (THANKS!!)
2002-03-11 17:24:41 kenyatta [View]
-
CGI script error
2002-02-28 11:37:05 fritz [View]
-
Domain Name
2002-03-01 11:11:16 fritz [View]
-
CHMOD from remote clients WSftp and FETCH
2002-02-25 20:00:21 loutti [View]
-
CHMOD from remote clients WSftp and FETCH
2003-08-30 04:10:00 anonymous2 [View]
-
not parsing in include
2002-02-15 11:23:49 bradrice [View]
-
with shtml I get an index of the contents of directory
2002-02-05 13:45:37 pablo2001 [View]
-
with shtml I get an index of the contents of directory
2002-02-06 07:42:53 Kevin Hemenway |
[View]
-
with shtml I get an index of the contents of directory
2002-02-08 04:53:09 pablo2001 [View]
-
how to enable .html
2002-02-05 12:35:04 storme [View]
-
how to enable .html
2002-02-05 13:19:10 Kevin Hemenway |
[View]
-
how to enable .html
2002-02-05 14:08:01 storme [View]
-
how to enable .html
2002-02-06 07:49:36 Kevin Hemenway |
[View]
-
how to enable .html
2002-02-06 08:49:23 storme [View]
-
how to enable .html
2002-02-06 13:13:40 Kevin Hemenway |
[View]
-
how to enable .html
2002-02-06 21:31:57 storme [View]
-
how to enable .html
2002-02-05 14:09:28 storme [View]
-
Internal Server Error
2002-02-02 11:45:00 rtevans [View]
-
Internal Server Error
2002-02-03 13:36:58 Kevin Hemenway |
[View]
-
cgi outside of cgi-bin
2002-02-01 20:36:16 griffitts [View]
-
cgi outside of cgi-bin
2002-02-03 13:30:45 Kevin Hemenway |
[View]
-
Server Admin
2002-02-01 11:21:55 galt606 [View]
-
Server Admin
2002-02-03 13:23:46 Kevin Hemenway |
[View]
-
Changing cgi permissions...
2002-01-31 22:18:51 razzlemfdazzle [View]
-
Changing cgi permissions...
2002-02-01 07:18:25 Kevin Hemenway |
[View]
-
My cgi's don't work!
2002-01-31 11:55:19 haymaker [View]
-
My cgi's don't work!
2002-01-31 14:37:30 Kevin Hemenway |
[View]
-
My cgi's don't work!
2002-01-31 17:20:02 haymaker [View]
-
My cgi's don't work!
2002-05-29 13:04:11 emc^2 [View]
-
My cgi's don't work!
2002-02-01 07:08:58 Kevin Hemenway |
[View]
-
My cgi's don't work!
2002-02-01 09:12:24 haymaker [View]
-
Question!
2002-01-30 18:10:04 ganesha [View]
-
It's all there...
2002-01-30 18:16:14 ganesha [View]
-
httpd.conf owned by root
2002-01-28 23:08:06 tanderson4 [View]
-
httpd.conf owned by root
2002-01-29 10:31:49 Kevin Hemenway |
[View]
-
missing lines in httpd.conf
2002-01-28 13:56:18 gvanmete [View]
-
missing lines in httpd.conf
2002-01-28 18:08:57 Kevin Hemenway |
[View]
-
index.shtml not showing results of cgi script
2002-01-27 09:50:55 pattyb777 [View]
-
I found another resolve
2002-02-15 19:54:28 orleck [View]
-
had same problem
2002-01-31 04:30:38 kevinmclean1 [View]
-
had same problem
2002-02-02 12:52:04 pattyb777 [View]
-
index.shtml not showing results of cgi script
2002-01-28 11:12:36 Kevin Hemenway |
[View]
-
After editing html file
2002-01-20 14:47:18 panderson [View]
-
After editing html file
2002-01-21 07:49:16 Kevin Hemenway |
[View]
-
Error document
2002-01-16 06:45:36 martino [View]
-
Error document
2002-01-16 13:59:23 Kevin Hemenway |
[View]
-
Error document
2002-01-18 04:23:31 martino [View]
-
Error document
2002-01-18 09:59:19 Kevin Hemenway |
[View]
-
Error document
2002-01-25 12:14:06 haymaker [View]
-
Adding another SSI Directory
2002-01-13 16:34:33 wmartino [View]
-
Adding another SSI Directory
2002-01-14 13:02:42 Kevin Hemenway |
[View]
-
Adding another SSI Directory
2002-01-14 20:06:41 wmartino [View]
-
Directory listing, not .shtml file
2002-01-13 09:37:09 mab152 [View]
-
Directory listing, not .shtml file
2002-01-14 12:59:57 Kevin Hemenway |
[View]
-
Problem enabling all .cgi or .pl files
2002-01-11 13:58:00 johnkoetsier [View]
-
Problem enabling all .cgi or .pl files
2002-01-12 08:55:00 Kevin Hemenway |
[View]
-
Includes for shtml
2002-01-10 14:45:24 walsh1 [View]
-
Includes for shtml
2002-01-11 04:24:39 Kevin Hemenway |
[View]
-
Still cannot access http://127.0.0.1/cgi-bin/test-cgi problems accessing
2002-01-08 11:20:41 eyelet [View]
-
Still cannot access http://127.0.0.1/cgi-bin/test-cgi problems accessing
2002-01-08 14:35:59 Kevin Hemenway |
[View]
-
Still cannot access http://127.0.0.1/cgi-bin/test-cgi problems accessing
2002-01-08 19:29:00 eyelet [View]
-
How do you open the config file with bbedit
2002-01-06 05:34:15 eyelet [View]
-
How do you open the config file with bbedit
2002-01-06 08:13:24 Kevin Hemenway |
[View]
-
Could we go a little deeper?
2002-01-02 14:08:30 beowulfrex [View]
-
Could we go a little deeper?
2002-01-03 08:21:54 Kevin Hemenway |
[View]
-
This is great!
2001-12-28 13:02:18 jays [View]
-
Adding another directory. . . ?
2001-12-27 11:31:58 kenyatta [View]
-
Adding another directory. . . ?
2001-12-27 12:17:35 Kevin Hemenway |
[View]
-
Adding another directory. . . ?
2002-01-25 12:07:12 haymaker [View]
-
restarting Apache
2001-12-24 05:09:53 philocon [View]
-
restarting Apache
2003-06-17 23:24:50 anonymous2 [View]
-
restarting Apache
2001-12-24 08:29:48 Kevin Hemenway |
[View]
-
restarting Apache
2001-12-25 13:10:56 philocon [View]
-
restarting Apache
2001-12-25 16:06:09 Kevin Hemenway |
[View]
-
restarting Apache
2001-12-26 04:39:05 philocon [View]
-
Lovin this too!
2001-12-20 19:27:36 ryentzer [View]
-
/etc/httpd/httpd.conf "dosn't relate to your example - please help!"
2001-12-20 09:50:28 bob30 [View]
-
/etc/httpd/httpd.conf "dosn't relate to your example - please help!"
2001-12-20 10:12:27 Kevin Hemenway |
[View]
-
/etc/httpd/httpd.conf "dosn't relate to your example - please help!"
2001-12-20 10:24:42 bob30 [View]
-
/etc/httpd/httpd.conf "dosn't relate to your example - please help!"
2001-12-20 14:05:00 Kevin Hemenway |
[View]
-
/etc/httpd/httpd.conf "dosn't relate to your example - please help!"
2001-12-21 01:34:11 bob30 [View]
-
still a bug
2001-12-18 19:59:30 chet [View]
-
still a bug
2001-12-19 07:47:26 Kevin Hemenway |
[View]
-
Not being processed
2001-12-18 13:29:50 radionerd [View]
-
Not being processed
2001-12-19 07:44:43 Kevin Hemenway |
[View]
-
Good stuff!
2001-12-18 01:41:59 chet [View]
-
very helpful but...
2001-12-17 11:02:36 swatson [View]
-
very helpful but...
2001-12-17 13:42:39 Kevin Hemenway |
[View]
-
something's missing
2001-12-16 22:56:43 ngb [View]
-
something's missing
2001-12-17 05:32:38 Kevin Hemenway |
[View]
-
something's missing
2001-12-17 11:25:33 ngb [View]
-
I'm loving this!
2001-12-15 08:05:47 hansv [View]

