CGI Scripts

Scripts?


Yes... scripts... don't leave home without them. A necessary thing from the mid-evil times of programming when every one sat a terminals and the concept of walking around with a processor in your hand had not made it to the drawing board... Scripts!

Scripts are great for performing simple tasks that don't require back end processing like keeping hackers at bay. Scripting, especially shell scripting was designed for that very thing... simple tasks.

Scripting comes in many flavors and styles. Fortunately plugging them into apache is pretty straight forward and simple.

CREATED 2020-04-19 09:28:16.0

010-00-00-E1

UPDATED 2020-04-19 11:14:55.0

The Alias


The fist thing we need to do is tell Apache the path we want to use and where that path maps to i.e. the directory the scripts are in. We do this using an alias diective, namely the SciptAlias directive.

SciptAlias takes two arguments. The Path and the Path. The first is the path you want to use in the url like /bin or www.example.com/bin/somefile.script. The second is the path it maps to i.e. the path in the file system or the directory the files (scipts) are in.

ScriptAlias /bin /path/to/bin

CREATED 2020-04-19 09:29:16.0

010-00-00-E3

UPDATED 2020-04-19 11:15:04.0

The Directory Directive


The Second thing we need to do is tell Apache what we are doing and where we are doing it. For that we use the Directory Directive (say that three times fast). The Directory directive encompasses all the rules for the directory we put our scripts in.

<Directory /path/to/directory>
   more directives...
</Directory>

Inside the Directory stanza we want to identify 3 things.

  • The Options
  • The Handler
  • The Files

CREATED 2020-04-19 09:28:51.0

010-00-00-E2

UPDATED 2020-04-19 11:15:05.0

Options


We need to use two of the many options Apache gives. First is ExecCGI which tells Apache to treat every file in this directory as a script that should be executed. The script itself identifies what kind of script it is. The second thing is FollowSymLinks if we are using a link to our script.

<Directory /path/to/directory>
   Options -Indexes +ExecCGI + FollowSymLinks
</Directory>

CREATED 2020-04-19 10:50:46.0

010-00-00-E4

UPDATED 2020-04-19 10:57:33.0

The Handler


The next thing we need to do is add a handler that will associate the type of script that we want to execute. This is not rocket science it simply looks at the extensions.

<Directory /path/to/directory>
   Options -Indexes +ExecCGI + FollowSymLinks
   AddHandler cgi-script .sh .php
</Directory>

CREATED 2020-04-19 10:51:30.0

010-00-00-E5

UPDATED 2020-04-19 10:57:34.0

Files...


The Last thing we need to identify is the files we want to be executed and assign permissions to those files. This is done with the Files directive.

<Directory /path/to/directory>
   Options -Indexes +ExecCGI + FollowSymLinks
   AddHandler cgi-script .sh .php
   <Files /path/to/file >
      permissions
   </Files>
</Directory>

The permissions are different depending on which version of Apache you are running. Up to Apache 2.2 the permissions were set with the Order directive. Since 2.4 you would use a Require directive.

Since Apache 2.2:

<Directory /path/to/directory>
   Options -Indexes +ExecCGI + FollowSymLinks
   AddHandler cgi-script .sh .php
   <Files /path/to/file >
      Order Deny/Allow
      Allow from all
   </Files>
</Directory>

Since Apache 2.4:

<Directory /path/to/directory>
   Options -Indexes +ExecCGI + FollowSymLinks
   AddHandler cgi-script .sh .php
   <Files /path/to/file >
      Require all granted
   </Files>
</Directory>

CREATED 2020-04-19 10:51:34.0

010-00-00-E6

UPDATED 2020-04-19 10:57:36.0

When it Doesn't Work...


Thare can be many reasons the script doesn't fire. Here are some common ones...

Directory permissions - Apache needs to have read and execute permissions for:

  • the Script,
  • the Directory the script is in and
  • the directory above that (the gotcha).

The Script is not returning the right stuff. If the script is suppose to return a page, it may not be doing it right. The page needs to have headers with Status and Content-type. Content-type needs to be in the proper format e.g. text/html not html/text There also needs to be a blank line between the headers and the body of the page.

The Browser tries to download the script - This means the directory is not setup correctly or not at all. Apache doesn't know it needs to execute this file so it does what it's suppose to do, offer it up!

CREATED 2020-04-19 11:15:46.0

010-00-00-E7

UPDATED 2020-04-19 11:16:13.0

Knowledge

L
I
N
K
S

DBID: db.wam

Page Server: Ithica

©2012 Leistware Data Systems

      Hello anonymous