#6                      Day #2     Serving Dynamic Content with CGI                    Wildan Maulana             wildan.m...
Overview●   How the CGI protocol works●   How to configure Apache to run CGI scripts, on    both Unix and Windows●   How t...
Common Gateway InterfaceSource : http://viu.eng.rpi.edu/
CGI ProtocolCERN HTTPd Server                                   NCSA HTTPd Server      Both provided mechanisms to invoke ...
CGI Operation●   Apache receives a request and determines that it    needs to be served by the CGI program●   Apache start...
CGI Environment VariablesVariable Name     Variable DescriptionSERVER_NAME       Hostname or IP address of the serverREQUE...
CGI Response●   Location: Instructs Apache that the CGI is not going    to answer the request and that the client should b...
Advantages and Disadvantages of             CGI Scripts●   Portability     ●   Performance●   Simplicity      ●   Code and...
Configuring Apache
Configuring Apache                         CGI ContentScriptAliasScriptAlias /usr/local/apache2/cgi-bin/ /cgi-bin/
Configuring Apache                           Fine-Grained ControlSetHandler Directive# Any files accessed thru the /cgi-bi...
Configuring Apache        Action and ScriptAction image/gif /cgi-bin/process.cgiScript PUT /cgi-bin/upload.cgi
Configuring Apache                          CGI Security●   Non Parse Headers (NPH) Scripts    ●   nph-example.cgi●   Debu...
Unix ConfigurationTesting Shell Script CGIs
Unix Configuration                          Perl Installation●   Preinstalled Perl    #which perl    perl -v●   Installing...
Unix ConfigurationTesting Perl CGI Scripts
Windows Configuration●   Testing Batch File CGIs●   Perl on Windows    ●   http://www.activestate.com●   Testing Perl Scri...
Enhancing Your CGI Performance●   mod_perl●   FastCGI
Common CGI Problems●   Forbidden Error    ●   Filesystem Permissions    ●   CGI Execute Permissions●   Internal Server Err...
Q&Ahttp://workshop.openthinklabs.com/resources
Reference●   Daniel Lopez Ridruezo; SAMS Teach Yourself    Apache2 in 24 Hours, SAMS Publishing, 2002    (Buy this book on...
Upcoming SlideShare
Loading in …5
×

Apache2 BootCamp : Serving Dynamic Content with CGI

38,000 views
37,899 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
38,000
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Apache2 BootCamp : Serving Dynamic Content with CGI

  1. 1. #6 Day #2 Serving Dynamic Content with CGI Wildan Maulana wildan.m@openthinklabs.com http://workshop.openthinklabs.com
  2. 2. Overview● How the CGI protocol works● How to configure Apache to run CGI scripts, on both Unix and Windows● How to troubleshoot common errors
  3. 3. Common Gateway InterfaceSource : http://viu.eng.rpi.edu/
  4. 4. CGI ProtocolCERN HTTPd Server NCSA HTTPd Server Both provided mechanisms to invoke external programs and scripts to create dynamic content but incompatible The Solution The CGI 1.1 specification
  5. 5. 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
  6. 6. CGI Environment VariablesVariable Name Variable DescriptionSERVER_NAME Hostname or IP address of the serverREQUEST_METHOD HTTP request method: HEAD, GET, POST, and so onREMOTE_ADDR Client IP addressCONTENT_TYPE MIME type of any client data being passed by a POST or PUT requestCONTENT_LENGTH Size of the client data
  7. 7. 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.
  8. 8. Advantages and Disadvantages of CGI Scripts● Portability ● Performance● Simplicity ● Code and● Existing Code Presentation● Source Hiding● Memory Leaks
  9. 9. Configuring Apache
  10. 10. Configuring Apache CGI ContentScriptAliasScriptAlias /usr/local/apache2/cgi-bin/ /cgi-bin/
  11. 11. Configuring Apache Fine-Grained ControlSetHandler 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 scripts AddHandler cgi-script .cgi <Directory “/usr/local/apache2/htdocs/scripts”> Options +ExecCGI </Directory>
  12. 12. Configuring Apache Action and ScriptAction image/gif /cgi-bin/process.cgiScript PUT /cgi-bin/upload.cgi
  13. 13. Configuring Apache CGI Security● Non Parse Headers (NPH) Scripts ● nph-example.cgi● Debugging CGI Execution ● ScriptLog ● ScriptLogLength ● ScriptLogBuffer
  14. 14. Unix ConfigurationTesting Shell Script CGIs
  15. 15. Unix Configuration Perl Installation● Preinstalled Perl #which perl perl -v● Installing Binaries ● Linux – Redhat : rpm -i perl*.rpm – Ubuntu : sudo apt-get install perl● Installing from Source http://www.perl.com/pub/a/language/info/software.html#sourcecode
  16. 16. Unix ConfigurationTesting Perl CGI Scripts
  17. 17. Windows Configuration● Testing Batch File CGIs● Perl on Windows ● http://www.activestate.com● Testing Perl Scripts
  18. 18. Enhancing Your CGI Performance● mod_perl● FastCGI
  19. 19. 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
  20. 20. Q&Ahttp://workshop.openthinklabs.com/resources
  21. 21. Reference● Daniel Lopez Ridruezo; SAMS Teach Yourself Apache2 in 24 Hours, SAMS Publishing, 2002 (Buy this book on Amazon)

×