Debugging CGI

Once you have a basic CGI configuration working, there are some additional Apache directives which can assist you in debugging your CGI scripts. Warning! You should only use these directives during development. They are not optimised for a production machine and can cause a significant overhead on you server.

Getting more Information

Adding the directive ScriptAliases in your srm.comf will enable logging of CGI error information.
 
# ScriptAlias: This controls which directories contain server scripts.
# Format: ScriptAlias fakename realname

ScriptAlias /cgi-bin/ c:/apache/cgi-bin/

ScriptLog c:/apache/logs/cgi_errors_logs
  Remember! to re-start Apache after any changes you make to the configuration file.

So we can show the difference between a good and bad script response, we are going to "modify" the test-cgi script we used to in Testing CGI to give a bad error response.

Change the script so as to remove the header information.

 
@echo off

rem echo Content-type: text/plain
rem echo argv is %0
rem echo.

echo SERVER_SOFTWARE = %SERVER_SOFTWARE%
      .
      .
      .
  And then try out the script file again. Your ErrorLog should product something like....  
[Thu Apr 09 10:33:00 1998] [error] malformed header from script. 
Bad header=SERVER_SOFTWARE = Apache/1.3b3: c:/apache/cgi-bin/test-cgi.bat
  Which in this simple case, is sufficient to determine what is causing the error. The ScriltLog produces considerable more information.

We can see that Apache returned error code 500: and Internal Server Error We know from our modifications, that this is caused by the lack of header information.

After the %stdout we can see actually what the script was attempting to send back to the browser. As per the error code, there is no

Content-type: text/plain

header information.

 
%% [Thu Apr 09 10:33:00 1998] GET /cgi-bin/test-cgi.bat HTTP/1.0
%% 500 c:/apache/cgi-bin/test-cgi.bat
%request
Connection: Keep-Alive
User-Agent: Mozilla/4.0 [en] (WinNT; I)
Pragma: no-cache
Host: jaunty
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
%response
SERVER_SOFTWARE = Apache/1.3b3
%stdout
SERVER_NAME = jaunty
GATEWAY_INTERFACE = CGI/1.1
SERVER_PROTOCOL = HTTP/1.0
SERVER_PORT = 80
REQUEST_METHOD = GET
HTTP_ACCEPT = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"
PATH_INFO = ""
PATH_TRANSLATED = ""
SCRIPT_NAME = "/cgi-bin/test-cgi.bat"
QUERY_STRING = ""
REMOTE_HOST = 158.76.200.102
REMOTE_ADDR = 158.76.200.102
REMOTE_USER = 
AUTH_TYPE = 
CONTENT_TYPE = 
CONTENT_LENGTH = 
  Here is a Unix example using Perl. A simple script which prints the now famous "hello, world". There is a minor syntax error on the last line of the script. The semi-colon ";" is a missing.  
#!/usr/local/bin/perl

print "Content-type: text/html\n\n";
print "<html><head><title>hello, world</title></head>";
print "<body><h1>hello, world</body></html>"
  When we try to access the script, the ErrorLog shows us that their was and error in running the script.  
[Thu Apr  9 11:53:22 1998] access to /var/apache/cgi-bin/hello.pl 
failed for 158.76.200.102, reason: Premature end of script headers 
  But the ScriptLog shows us why it went wrong. Perl wrote on stderr that there was a syntax error near line 5.  
%% [Thu Apr  9 11:53:22 1998] GET /cgi-bin/hello.pl HTTP/1.0
%% 500 /var/apache/cgi-bin/hello.pl
%request
Connection: Keep-Alive
User-Agent: Mozilla/4.0 [en] (WinNT; I)
Pragma: no-cache
Host: www.jlk.net
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
%response
%stderr
syntax error at /var/apache/cgi-bin/hello.pl line 5, near "print"
Execution of /var/apache/cgi-bin/hello.pl aborted due to compilation errors.
  This trivial error could have been found by running hello.pl from the command line but is sufficient to demonstrate the additional script debugging features of Apache.

There are a few other directives that you should be aware of when debugging CGIs scripts with Apache.

Other Debug Directives

ScriptLog file logs grow very quickly due the the amount of data generated. You can limit the size of of ScriptLog file with the ScriptLogLength to a certain number of bytes.
ScriptLogLength 5000000
  Additionally the number of bytes logged during a Post request to a CGI script can be limited with the ScriptLogBuffer
ScriptLogBuffer 512
 

References

Apache Directives

 


Testing CGI   RTFM Index   Server Side Includes
Tell us what you think!
Copyright © 2006 Open Source Migrations ltd. All Rights Reserved.
Apache RTFM last updated Saturday October 21 2006