These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

UNIX01/Scripting For Apache

Classnotes | UNIX01 | RecentChanges | Preferences

When Apache runs a server-side script, it generally takes the STDOUT from that script and outputs it to the client. Because of this, we can once again apply the old PIPE/FILE-ABSTRACTION standby's with respect to scripting for web-sites.

When a script begins, the script must first output the content type for the data it is about to send to the web-server. This can be any one of the W3C compliant MIME types (see http://www.w3.org/TR/REC-html40/types.html#h-6.7). If the script is simply sending text or html data through the server, then the following will suffice:

 Content-type: text/html

Often, depending upon which scripting language is being used, this initial content type declaration will be sent automatically. Additionally, some languages such as Perl offer module libraries which will handle things like this (and producing compliant W3C code) for you. Always consult the documentation for your web-scripting language of choice before assuming one way or ther other.

Recalling our discussion of Perl in day three, we know that the print command will send data to STDOUT. Thus, the following simple script will print "Hello World!" to the web-browser:

 #!/usr/bin/perl
 print "Content-type: text/html\n\n";
 print "<html ><body>\n";
 print "<center>Hello World!</center>\n";
 print "</body></html>\n";

Go ahead and enter this into a script called "helloworld.cgi". Make it executable, and run it from the command-line to verify it works.

So now we have a Perl scrpt suitable for very simple CGI implimentation. Where do we place it?

The file /etc/httpd/conf/httpd.conf is the configuration file for Apache. We will examine it in grotesque detail in "UNIX02/Apache Configuration", but for now we are looking for the "ScriptAlias" line which defines where we install our scripts to. Chances are, it will be this:

 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

So by moving this helloworld.cgi script into this directory (/var/www/cgi-bin/) we can then browse to it on our web-server using:

 http://localhost/cgi-bin/helloworld.cgi

The book goes into gratuitous detail about various web-scripting options available under Linux, but this is all we will cover in this course (afterall, it really isn't a programming course in spite of the fact that there is so much scripting involved with UNIX ;-)

If you would like some great books on CGI and web programming under Linux, I highly recommend the following:



Classnotes | UNIX01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited August 9, 2003 4:42 am (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.