Dynamic Content


Active content on a web page can range from a simple form or opinion poll, to a search-engine results page, to a portal with personalized news content.

Apache offers three methods of generating active content: common gateway interface (CGI) scripts, server-side includes (SSI), and active content modules such as mod_perl.

Chapter 31, "Programming Dynamic Websites," covers the creative end of making active content. This section is a brief introduction into the nuts and bolts of delivering this content to users.

CGI Programs

CGI is a specification that allows a web server to execute programs and scripts that reside on the web server and return their output back to the client. CGI programming is the most common method of generating dynamic content for the web. Almost any language can be used, although Perl, Python, and C tend to be the most popular.

The easiest (and most secure) way to activate the CGI capabilities of Apache is to create a special directory that will contain all the scripts, and specify it using the ScriptAlias directive. Traditionally, the name of the directory is cgi-bin. Apache will try to run any file that resides in that directory ll file residing in the directory set with ScriptAlias.

Remember, these programs must be executable by the system Apache user. When you place your programs there, the permissions must be set to permit this:

chmod 755 program.cgi 

Another way to enable CGI is to add +ExecCGI to the directory's Options directive and then set a handler for files with certain extensions. These changes can be made either in the http.conf or .htaccess file:

Options +ExecCGI AddHandler cgi-script .cgi .pl 

In the preceding code, files in the directory with a .cgi or .pl extension will be handled as CGI scripts. Apache will execute the scripts and return the results to the requesting client.

Server-Side Includes

SSI are specially crafted comments called elements, which are embedded in a web page and interpreted by Apache. They can be used to include the contents of other files, display environment variables (such as the current date and time) or a file's timestamp, and produce program outputs.

To enable SSI, mod_include must be available to Apache, and you must add +Includes to the directory's Options directive either in http.conf or in .htaccess:

Options +Includes 

There are then two methods for identifying a file for processing as an SSI document. The first is to specify a file extension, traditionally .shtml, and associate a handler to process files that end with that extension:

AddType text/html  .shtml AddHandler server-parsed  .shtml 

The directives may be placed in either the http.conf or a .htaccess file. When set, Apache will process all files with the .shtml extension for SSI elements before returning the results to the requesting client.

The second method for identifying a file is by turning on the XBitHack directive and setting the execute permission bit on any SSI file. Again, they may be set either in .htaccess or httpd.conf.

XBitHack on 

With XBitHack turned on, Apache will parse all files that the user has permission to execute, regardless of the file's extension.

Active Content Modules

If you find yourself writing a lot of CGI scripts in a particular interpreted language, it might be beneficial to see if a module exists that integrates the language's interpreter with Apache. For example, you would probably do better in the long run using the mod_perl Apache module instead of processing your Perl-powered pages through CGI. When you call a script from Apache, the Perl interpreter process starts, does its work, and then eventually stops. You can just imagine what that does to your system's resources if you have 20 scripts running simultaneously, even if just for a few seconds. When you channel those scripts through a module, everything is handled by one native interpreter. This makes everything go faster and safer.

The downside is that mod_perl is much fussier about errors. Apache doesn't really mind if you don't release a resource programmatically, because it shuts down the script before that becomes a problem. In a module, errors accumulate and become real problems. The upside is that you will become a better programmer as a result of fixing all these problems.

Some languages are specifically designed to be run through an Apache module, such as PHP. PHP can be used to process scripts through CGI; the language's documentation encourages the use of an Apache module over the CGI implementation.

SUSE Linux provides Apache modules for four scripting languages: mod_perl, mod_python, mod_php4, and mod_ruby.

  • SEE Chapter 30, "Using Perl and Python," and Chapter 31, "Programming Dynamic Websites," for more information about using these languages.



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net