The other method is to put scripts in among the HTML files. You should only do this if you trust the authors of the site to write safe scripts (or not write them at all) since security is much reduced. Generally speaking, it is safer to use a separate directory for scripts, as explained previously. First, it means that people writing HTML can’t accidentally or deliberately cause security breaches by including executable code in the web tree. Second, it makes life harder for the Bad Guys: often it is necessary to allow fairly wide access to the nonexecutable part of the tree, but more careful control can be exercised on the CGI directories.
We would not suggest you do this unless you absolutely have to. But regardless of these good intentions, we put mycgi.cgi in.../site.cgi/htdocs. The Config file, ... /site.cgi/conf/httpd2.conf, is now:
User webuser Group webgroup ServerName www.butterthlies.com DocumentRoot /usr/www/APACHE3/site.cgi/htdocs AddHandler cgi-script cgi Options ExecCGI
Use Addhandler
to set a handler type of
cgi-script
with the extension
.cgi. This means that any document Apache comes
across with the extension.cgi will be taken to be an
executable script.You put the CGI scripts,
called <name>.cgi in your document root. You also need to have
Options
ExecCGI
. To run this one, type the following:
./go 2
You would access this script by browsing to http://www.butterthlies.com/cgi-bin/mycgi.cgi.
To experiment, we have a simple test script, mycgi.cgi, in two locations: .../cgi-bin to test the first method and.../site.cgi/htdocs to test the second. When it works, we would write the script properly in C or Perl or whatever.
The script mycgi.cgi looks like this:
#!/bin/sh echo "Content-Type: text/plain" echo echo "Have a nice day"
Under Win32, providing you want to run your script under COMMAND.COM and call it mycgi.bat, the script can be a little simpler than the Unix version — it doesn’t need the line that specifies the shell:
@echo off echo "Content-Type: text/plain" echo. echo "Have a nice day"
The @echo
off
command turns off command-line echoing, which
would otherwise completely destroy the output of the batch file. The
slightly weird-looking echo.
gives a blank line (a
plain echo
without a dot prints ECHO is
off
).
If you are running a more exotic shell, like bash or perl, you need the “shebang” line at the top of the script to invoke it. These must be the very first characters in the file:
#!shell path
...