CGI: Introduction: Difference between revisions

m
More cleanup.
(Punctuation and grammatical changes; some clarification.)
m (More cleanup.)
Line 6: Line 6:
Apache, with the release of 2.2.18, made a small change. Quoting from the change log: "'''''Added shebang check for '! so that .vbs scripts work as CGI'''''." 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.
Apache, with the release of 2.2.18, made a small change. Quoting from the change log: "'''''Added shebang check for '! so that .vbs scripts work as CGI'''''." 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. These examples provide an introduction to CGI scripting. They are written in VBScript and JavaScript. Before we look at CGI scripting, we will explore the mini-server architecture.
This new mini-server, Coral-Mini, is used for running the examples for this tutorial. These examples provide an introduction to CGI scripting. They are written in VBScript and JavaScript. Before we look at CGI scripting, we will explore the mini-server architecture.


__TOC__
__TOC__
Line 47: Line 47:
Testing is straight forward.
Testing is straight forward.


# Start the server by double clicking on Start_Coral_Mini.exe This automatically configures server paths.
# Start the server by double clicking on Start_Coral_Mini.exe This automatically configures the server paths.
# Start a web browser.
# Start a web browser.
# Type '''<nowiki>http://localhost:8081/</nowiki>''' into the browser address bar.
# Type '''<nowiki>http://localhost:8081/</nowiki>''' into the browser address bar.
Line 90: Line 90:


==Configuring Apache==
==Configuring Apache==
When Apache is started, it is supplied with the location and name of a configuration file. This overrides the default location compiled into the program. It's common practice to name the file '''httpd.conf''' and place it in a sub-folder named '''conf''' Coral-Mini uses this convention.
When Apache is started, it is supplied with the location and name of a configuration file. This overrides the default location compiled into the program. It's common practice to name the file '''httpd.conf''' and place it in a sub-folder named '''conf'''. Coral-Mini uses this convention.


Coral-Mini Server uses this configuration file: MiniServer\apache2\conf\'''httpd.conf'''
Coral-Mini server uses 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.
'''Note:''' Apache’s example configuration file contains a lot of detailed information. I personally find this confusing and have removed this detail.


{| cellpadding="4" cellspacing="1" style="background:#000000;"
{| cellpadding="4" cellspacing="1" style="background:#000000;"
Line 110: Line 110:
<nowiki>#</nowiki> V 1.0 20-9-2011  
<nowiki>#</nowiki> V 1.0 20-9-2011  
|
|
General information a reminder what the configuration is for.  
General information. A reminder what the configuration is for.  


|-style="background:#f5f5f5;"
|-style="background:#f5f5f5;"
Line 127: Line 127:


'''Note:''' For some modules the order is important.<br />
'''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.
'''Tip:''' Check Apache’s example files and list them in that order to avoid 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.  
Order of priority is from bottom to top; hence if a module is dependent on another it should come first in the list.  
Line 150: Line 150:
These settings are common to the main server. Most settings in this section have defaults. However I like to see what I am using, hence list them regardless.
These settings are common to the main server. Most settings in this section have defaults. 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.
* '''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 www.fred.com. Alternatively you can leave this as localhost and use your fully qualified domain name in a Vhost section (not covered in this server example).
* '''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 www.fred.com. 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.
* '''ServerRoot:''' Path where the Apache program is located.
* '''DocumentRoot:''' Folder where your web-site will be served from.
* '''DocumentRoot:''' Folder where your web-site will be served from.
* '''DirectoryIndex:''' When a user requests a page supplying only a folder name (example fred.com) 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.
* '''DirectoryIndex:''' When a user requests a page supplying only a folder name (example: fred.com), 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.


|-style="background:#f5f5f5;"
|-style="background:#f5f5f5;"
Line 251: Line 251:
LoadModule cgi_module modules/mod_cgi.so
LoadModule cgi_module modules/mod_cgi.so
</pre>
</pre>
===Method 1 ScriptAlias===
===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 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.


Line 264: Line 264:
Similarly, if the <nowiki>URL http://localhost/cgi-bin/test.vbs</nowiki> is requested, Apache will attempt to execute the VBScript file C:/UniServer/cgi-bin/test.vbs and return the output.
Similarly, if the <nowiki>URL http://localhost/cgi-bin/test.vbs</nowiki> is requested, Apache will attempt to execute the VBScript file C:/UniServer/cgi-bin/test.vbs and return the output.


===Method 2 Folders===
===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.
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.
<pre>
<pre>
<Directory "C:/UniServer/www/somedir/">
<Directory "C:/UniServer/www/somedir/">
Line 273: Line 273:
The Options ExecCGI directive informs Apache to permit the execution of CGI files.
The Options ExecCGI directive informs Apache to permit the execution of CGI files.


===Method 3 .htaccess===
===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:
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: