Jump to navigation Jump to search

CGI: Introduction

5 bytes added, 18:26, 8 November 2011
Punctuation and grammatical changes; some clarification.
'''Coral-Mini Server'''
Apache , with the release of 2.2.18 , made a small change, extract . Quoting from the change log : "'''''Added shebang check for '! so that .vbs scripts work as CGI''''' hidden ." Hidden in that statement is the ability not only to run VBScripts as CGI but also JavaScripts. A more profound implication is the ability to create dynamic web sites using Apache alone , removing the bloat of PHP and MySQL. This tutorial revisits the mini-server series and updates the basic Apache server to include the new Apache 2.2.21 core , and the control architecture from The Uniform Server Coral-8 series.
This new mini-server, Coral-Mini , is used for running examples for this tutorial examples. These examples provide an introduction to CGI scripting, they . They are written in VBScript and JavaScript. Before looking we look at CGI scripting , we will explore the mini-server architecture is first covered.
* The server logs all web requests and errors.
* Includes only the minimum modules to meet specification.
'''''[[#top | Top]]'''''
Download the Coral-Mini server from SourceForge [ Project Page] and save the file '''coral_mini_server.exe''' to any folder of your choice.
===Extract the files===Double click on coral_mini_server.exe, which starts the extraction process. No need to change the folder destination. Click extract; this creates a new folder '''MiniServer''' containing which contains four files and four folders.
# Start_Coral_Mini.exe - Double click to start the server
# Stop_Coral_Mini.exe - Double click to stop the server# tutorial.bat - Displays this tutorial in the default browser
# READ_ME.txt - Provides information for changing ports.
# apache2 - Apache sever server files and configuration
# control - File to start and stop server
# docs - Tutorial and working scripts from tutorial. Licenses for server.
# www - Root folder. Apache servers serves files from this folder.
Testing is straight forward.
# Start the server by double clicking on Start_Coral_Mini.exe (This automatically configures server paths.)
# Start a web browser.
# Type '''<nowiki>http://localhost:8081/</nowiki>''' into the browser address bar.
# The index default page index.html is displayed.
# Stop the server by double clicking on Stop_Coral_Mini.exe
==Apache modules==
Apache comes with a vast array of modules a . A full list and description can be found here in the [ Apache Docs] . Modules used in the Coral-mini Mini server are listed below, note . Note that the core module is part of the main binary (program) and loaded by default.
Highlighted Key modules are highlighted in bold are separate modules these . These are loaded using Apache's configuration file '''httpd.conf'''.
{| cellpadding="4" cellspacing="1" style="background:#000000;"
|core||Core Apache HTTP Server features that are always available ; included in the binary (program).
|mpm_winnt.c||WinNT MPM is part of the Windows Apache core. A Multi-Processing Module (MPM) , it is the default for Windows NT operating systems. Uses It uses a single control process which launches a single child process which in turn creates threads to handle requests. (This is the reason you will see two Apache processes in task manager).
|'''authz_host_module'''||Group authorizations based on host (name or IP address). Required to restrict access to folders , etc.
|'''mod_dir'''||Provides for "trailing slash" redirects and serving directory index files. (Optional , but nice to have otherwise . Otherwise a user needs to type index.html on entry to a folder or for initial web site access. It prevents this error message: The requested URL / was not found on this server.)
|'''mod_mime'''||Associates the requested filename's extensions file name extension with the file's behavior (handlers and filters) and content (mime-type, language, character set and encoding) Note that without this module , the file will be served as plain text. The DefaultType is text/plain
|'''mod_rewrite'''||Provides rewriting of requested URLs on the fly. Allows masking of file extensions ; for example , a request for index.htm results in page index.vbs being run on the server and the result returned as index.htm
|'''mod_cgi'''||Provides an interface for running CGI scripts. Scripts that require running are targeted using the AddHandler directive this . This directive associates file extensions with CGI scripts.
'''''[[#top | Top]]'''''
==Configuring Apache==
On starting When Apache is started, it is supplied with the location and name of a configuration file this . This overrides the default location compiled into the program. Its It's common practice to name the file '''httpd.conf''' and place it in a sub-folder named '''conf''' Coral-mini Mini uses this convention.
Coral-Mini Server uses the following this configuration file: MiniServer\apache2\conf\'''httpd.conf'''
'''Note:''' Apache’s example configuration file contains a lot of detailed information . I personally find this confusing and prefer to remove this detail.
{| cellpadding="4" cellspacing="1" style="background:#000000;"
'''Note:''' For some modules the order is important.<br />
'''Tip:''' Check Apache’s example files and list them in that order, which avoids any problems.
Order of priority is from bottom to top; hence if a module is dependent on another it should come first in the list.
'''DirectoryIndex''' index.html index.htm
These settings are common to the main server. Most settings in this section have defaults however . However I like to see what I am using , hence list them regardless.
* '''Listen:''' Server listening port, standard is port 80 change this to move the server to another port.
* '''ServerName:''' For reliability always specify a host name and port. Note: localhost is valid , however if you have a DNS entry , use your fully qualified domain name , eg . Alternatively you can leave this as localhost and use your fully qualified domain name in a Vhost section (not covered in this server example).
* '''ServerRoot:''' Path where the Apache program is located.
* '''DocumentRoot:''' Folder where your web-site will be served from.
* '''DirectoryIndex:''' When a user requests a page supplying only a folder name (example the index page is automatically returned by default. Note that you can have more than one index page in the same folder with a different file extension. Order of priority is left to right, ; first one found in the list is returned, all others are ignored.
MaxRequestsPerChild 0
Most settings in this section have defaults however . However again I like to see what I am using , hence list them regardless.
Each directory to which Apache has access can be configured with respect to which services and features are allowed and/or disabled in that directory (including its sub-directories).
Apache has access to all folders and files on the drive where it is installed on,
hence the '''first Directory''' directive is very restrictive:
* '''Options None:''' Turns off, directory browsing, server side includes, CGI execution, follow symbolic links.
* '''AllowOverride None:''' Turns off support for .htaccess files
* '''Deny from all:''' No one allowed access.
Only The only way to gain enable access is to target each folder in turn and open it up as required second . Second Directory directive targets folder www this which contains your web site.
* '''Options Indexes Includes:''' Turns on, directory browsing and server side includes.
* '''AllowOverride All:''' Turns on support for .htaccess files
* '''Allow from all:''' Everyone is allowed access to folder www and its sub-folders.
The '''AccessFileName''' directive defines the file name to use for secondary configuration files by . By convention the name is '''.htaccess'''Access to these files is retricted restricted using the '''Files''' directive. The regex also prevents '''.htpasswd''' files from being viewed by Web clients.
TypesConfig /usr/local/apache2/conf/mime.types
In conjunction with the mime_module , the directive TypesConfig is used to specify a file which maps extensions onto MIME types. Note that without the module and this file , served pages will be in plain text.<br />Coral-Mini uses a greatly reduced mime.types file ; it contains only commonly used types.
'''''[[#top | Top]]'''''
==Configuring Apache CGI - Overview==
The following provides a general overview of how to configuring Apache to run CGI scripts. In order to run CGI programs , Apache requires configuration directives to permit CGI execution. You can use one of the following methods to permit CGI execution.
'''''Note'':''' All three methods require the mod_cgi module to be enabled as shown below:
===Method 1 ScriptAlias===
The '''ScriptAlias''' directive tells Apache that a particular directory is set aside for CGI programs. Apache now assumes every file in this directory is a CGI program. When a page from this folder is requested , Apache will attempt to execute it using the appropriate program.
The ScriptAlias directive looks like:
ScriptAlias /cgi-bin "C:/UniServer/cgi-bin/"
The directive defines a URL prefix (cgi-bin) that is mapped to a particular folder (directory). ScriptAlias informs Apache that everything under that URL prefix will be considered a CGI program.
For example, if the URL <nowiki>http://localhost/cgi-bin/</nowiki> is requested, Apache will attempt to execute the perl file C:/UniServer/cgi-bin/ and return the output.
===Method 2 Folders===
On hosted sites , for security reasons CGI programs are restricted to ScriptAlias'ed folders. However you can run CGI programs from any folder. As an alternative to ScriptAlias you can use the Options directive, (inside your main server configuration file, ) to specify that CGI execution is permitted in a particular directory.
<Directory "C:/UniServer/www/somedir/">
===Method 3 .htaccess===
Using '''.htaccess''' files allow you to set configuration directives on a per-directory basis. Apache looks in a folder from which it is serving for this file and applies the directives found. To enable the use of .htaccess files , edit the Apache configuration files as follows:
Change or add the AllowOverride directive in a specific folder directive ; for example , www as shown below:
If not already included , add the following block of code to prevent .htaccess and .htpasswd files from being viewed by Web clients:
<Files ~ "^\.ht">
Which tells Apache that execution of CGI programs is permitted in this folder.
'''''Note'':''' All of The Uniform Servers have been pre-configured to allow the use of .htaccess files.
'''''[[#top | Top]]'''''
Using either of the above methods Apache will attempt to execute every file as a CGI script this . This includes files such as css and images. Clearly these are not CGI scripts and would produce errors when executed. To resolve avoid this you need to inform the server what files are genuine CGI scripts by using the AddHandler directive.
AddHandler cgi-script .cgi .pl .vbs
The above AddHandler directive informs the server to treat all files with a cgi, pl or vbs extension as CGI programs:. 
'''''[[#top | Top]]'''''
==Test folder and .htaccess==
Tutorial The tutorial examples require a test folder and .htaccess file.*In folder MiniServer\www create a new folder '''vbs_test''' this in which you will contain put your tutorial scripts.
*Inside this folder create a new '''.htaccess''' file with the following content:
|'''Note:'''||'''.htaccess''' content
|If you have problems creating the .htaccess file , copy the one from folder www.
Edit the file: Delete all lines and add the three lines shown on right and save.
'''''[[#top | Top]]'''''
The above has shown how to configure Apache to run CGI scripts.
Options +FollowSymlinks
Although tutorial examples specifically refer to the Coral-Mini Server they can be run on any of The Uniform Server Servers that contains contain Apache 2.2.18 or above. Just create the test folder and add the .htaccess file as described above.
'''''[[#top | Top]]'''''
==Where to next==

Navigation menu