27.2. Sample CGI Using Bash Shell Scripting

This section demonstrates how to perform CGI tasks using standard Linux Bash shell scripts. The same methods outlined in this section can be applied to most other programming and scripting languages.

The examples in this section were created on a GNU/Debian Linux system and may need to be modified to run on other versions of Linux.

27.2.1. Configuring Apache to Deliver Bash Scripts

Configuring Apache to deliver other scripts is fairly straightforward, involving making it aware that a particular file type should be handled like a script.

It is possible to cheat and simply name your scripts using filenames representative of other scripting languages. For example, if the Web server is already configured to deliver .cgi files as scripts, you can simply name your Bash scripts using the .cgi extension. However, it is usually advisable to appropriately name your scripts following conventions for the language being used and to configure Apache appropriately for that language. For example, use .sh extensions for shell scripts, .py extensions for Python scripts, and so on.

The essential steps to configure Apache to deliver shell scripts are as follows:

  1. Ensure that the Apache CGI module is installed and active. The module should be compiled into Apache or appear in a LoadModule line within the Apache configuration file similar to the following:

    LoadModule cgi_module /usr/lib/apache/1.3/mod_cgi.so
  2. Enable CGI scripting in the directory(ies) where ...

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.