21
Day #2 Serving Dynamic Content with CGI Wildan Maulana [email protected] http://workshop.openthinklabs.com #6

Apache2 BootCamp : Serving Dynamic Content with CGI

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Apache2 BootCamp : Serving Dynamic Content with CGI

Day #2Serving Dynamic Content with CGI

Wildan [email protected]

http://workshop.openthinklabs.com

#6

Page 2: Apache2 BootCamp : Serving Dynamic Content with CGI

Overview

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

both Unix and Windows● How to troubleshoot common errors

Page 3: Apache2 BootCamp : Serving Dynamic Content with CGI

Common Gateway Interface

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

Page 4: Apache2 BootCamp : Serving Dynamic Content with CGI

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

Page 5: Apache2 BootCamp : Serving Dynamic Content with CGI

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

Page 6: Apache2 BootCamp : Serving Dynamic Content with CGI

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

Page 7: Apache2 BootCamp : Serving Dynamic Content with CGI

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.

Page 8: Apache2 BootCamp : Serving Dynamic Content with CGI

Advantages and Disadvantages of CGI Scripts

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

● Performance● Code and

Presentation

Page 9: Apache2 BootCamp : Serving Dynamic Content with CGI

Configuring Apache

Page 10: Apache2 BootCamp : Serving Dynamic Content with CGI

Configuring ApacheCGI Content

ScriptAlias

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

Page 11: Apache2 BootCamp : Serving Dynamic Content with CGI

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>

Page 12: Apache2 BootCamp : Serving Dynamic Content with CGI

Configuring ApacheAction and Script

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

Page 13: Apache2 BootCamp : Serving Dynamic Content with CGI

Configuring ApacheCGI Security

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

● Debugging CGI Execution● ScriptLog● ScriptLogLength● ScriptLogBuffer

Page 14: Apache2 BootCamp : Serving Dynamic Content with CGI

Unix ConfigurationTesting Shell Script CGIs

Page 15: Apache2 BootCamp : Serving Dynamic Content with CGI

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

Page 16: Apache2 BootCamp : Serving Dynamic Content with CGI

Unix ConfigurationTesting Perl CGI Scripts

Page 17: Apache2 BootCamp : Serving Dynamic Content with CGI

Windows Configuration

● Testing Batch File CGIs● Perl on Windows

● http://www.activestate.com

● Testing Perl Scripts

Page 18: Apache2 BootCamp : Serving Dynamic Content with CGI

Enhancing Your CGI Performance

● mod_perl● FastCGI

Page 19: Apache2 BootCamp : Serving Dynamic Content with CGI

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

Page 21: Apache2 BootCamp : Serving Dynamic Content with CGI

Reference

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