Apache2 BootCamp : Serving Dynamic Content with CGI

Preview:

DESCRIPTION

 

Citation preview

Day #2Serving Dynamic Content with CGI

Wildan Maulanawildan.m@openthinklabs.com

http://workshop.openthinklabs.com

#6

Overview

● How the CGI protocol works● How to configure Apache to run CGI scripts, on

both Unix and Windows● How to troubleshoot common errors

Common Gateway Interface

Source : http://viu.eng.rpi.edu/

CGI Protocol

CERN HTTPd Server NCSA HTTPd Server

Both provided mechanisms to invoke externalprograms and scripts to create dynamic content but incompatible

The Solution

The CGI 1.1 specification

CGI Operation

● Apache receives a request and determines that it needs to be served by the CGI program

● Apache starts an instance of the CGI program● Apache passes information about the request to the

CGI● Apache receives the response from the CGI, optionally

processes its headers and contents, and sends it to the client

● The CGI program finishes and all resources associated with it are recalled by the operating system

CGI Environment VariablesVariable Name Variable Description

SERVER_NAME Hostname or IP address of the server

REQUEST_METHOD HTTP request method: HEAD, GET, POST, and so on

REMOTE_ADDR Client IP address

CONTENT_TYPE MIME type of any client data being passed by a POST or PUT request

CONTENT_LENGTH Size of the client data

CGI Response

● Location: Instructs Apache that the CGI is not going to answer the request and that the client should be redirected to the specified URL.

● Status: This is not a valid HTTP header and it is not transmitted back to the client, but it indicates the HTTP status code for the request to Apache.

● Content-Type: Specifies the type of data returned in the request. For example, if you are returning a Web page, the header value should be text/html.

Advantages and Disadvantages of CGI Scripts

● Portability● Simplicity● Existing Code● Source Hiding● Memory Leaks

● Performance● Code and

Presentation

Configuring Apache

Configuring ApacheCGI Content

ScriptAlias

ScriptAlias /usr/local/apache2/cgi-bin/ /cgi-bin/

Configuring ApacheFine-Grained Control

SetHandler Directive

# Any files accessed thru the /cgi-bin/ url will execute as CGI scripts.<Location “/cgi-bin/”> Options +ExecCGI SetHandler cgi-script<Location>

Associating CGI Processing with File Extensions

# Any files ending in .pl will be executed as CGI scripts<Files *.pl> Options +ExecCGI SetHandler cgi-script</Files># Any files ending in .cgi in the /usr/local/apache2/htdocs/scripts # will be executed as CGI scriptsAddHandler cgi-script .cgi

<Directory “/usr/local/apache2/htdocs/scripts”> Options +ExecCGI</Directory>

Configuring ApacheAction and Script

Action image/gif /cgi-bin/process.cgiScript PUT /cgi-bin/upload.cgi

Configuring ApacheCGI Security

● Non Parse Headers (NPH) Scripts● nph-example.cgi

● Debugging CGI Execution● ScriptLog● ScriptLogLength● ScriptLogBuffer

Unix ConfigurationTesting Shell Script CGIs

Unix ConfigurationPerl Installation

● Preinstalled Perl#which perlperl -v

● Installing Binaries● Linux

– Redhat : rpm -i perl*.rpm– Ubuntu : sudo apt-get install perl

● Installing from Sourcehttp://www.perl.com/pub/a/language/info/software.html#sourcecode

Unix ConfigurationTesting Perl CGI Scripts

Windows Configuration

● Testing Batch File CGIs● Perl on Windows

● http://www.activestate.com

● Testing Perl Scripts

Enhancing Your CGI Performance

● mod_perl● FastCGI

Common CGI Problems● Forbidden Error

● Filesystem Permissions● CGI Execute Permissions

● Internal Server Error● Program Permissions● Interpreter Location● Malformed Headers

● Other Causes● Source Code in the Browser

Reference

● Daniel Lopez Ridruezo; SAMS Teach Yourself Apache2 in 24 Hours, SAMS Publishing, 2002 (Buy this book on Amazon)

Recommended