CGI: Introduction: Difference between revisions
Punctuation and grammatical changes; some clarification. |
m Text replace - "projects/uniformserver" to "projects/miniserver" |
||
(3 intermediate revisions by 2 users not shown) | |||
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 | 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 | '''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 | 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 | '''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 | * '''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 245: | Line 245: | ||
==Configuring Apache CGI - Overview== | ==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 | 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. | ||
'''''Note'':''' All three methods require the mod_cgi module to be enabled as shown below: | '''''Note'':''' All three methods require the mod_cgi module to be enabled as shown below: | ||
Line 251: | Line 251: | ||
LoadModule cgi_module modules/mod_cgi.so | LoadModule cgi_module modules/mod_cgi.so | ||
</pre> | </pre> | ||
===Method 1 ScriptAlias=== | |||
You can select one of the following methods to permit CGI execution. | |||
===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 267: | ||
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 276: | ||
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 | Using '''.htaccess''' files allow you to set configuration directives on a per-directory basis. Apache looks for this file in a folder from which it is serving 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: | Change or add the AllowOverride directive in a specific folder directive; for example, www as shown below: | ||
Line 297: | Line 300: | ||
Options +FollowSymlinks | Options +FollowSymlinks | ||
</pre> | </pre> | ||
This 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. | '''''Note'':''' All of The Uniform Servers have been pre-configured to allow the use of .htaccess files. | ||
===AddHandler=== | ===AddHandler=== | ||
Using either of the above methods Apache will attempt to execute every file as a CGI script. This includes files such as css and images. Clearly these are not CGI scripts and would produce errors when executed. To avoid this you need to inform the server what files are genuine CGI scripts by using the AddHandler directive. | Using either of the above methods, Apache will attempt to execute every file as a CGI script. This includes files such as css and images. Clearly these are not CGI scripts and would produce errors when executed. To avoid this you need to inform the server what files are genuine CGI scripts by using the AddHandler directive. | ||
<pre> | <pre> | ||
AddHandler cgi-script .cgi .pl .vbs | AddHandler cgi-script .cgi .pl .vbs | ||
Line 340: | Line 343: | ||
==Where to next== | ==Where to next== | ||
The [[CGI: VBScript CGI|next page]] details how to format a VBScript for running as a CGI script. | |||
[[CGI: VBScript CGI| | |||
---- | ---- |