Friday, December 03, 2004

Troubleshooting CGI Scripts

Ever been working on a CGI script (I personally prefer Perl for my CGI needs), and you get stuck with a blasted 500 Internal Server Error or some other miscellaneous symptom of dysfunction? Those of you who use CGI frequently probably have troubleshooting a misbehaving script down to a science, but I found this nice little checklist of troubleshooting steps - perfect for the CGI n00b, or the methodical pro who enjoys checking things off a list.

1. Does the CGI work by itself?

Before you add to the complexity, try running the CGI by itself. If the CGI doesn’t work from the command line, there’s no way it can work through the web server. It needs to output a header (meaning the first thing that comes out of the script) containing Content-type: text/html followed by two newlines. (Remember, two newlines, not one, not three, two).

2. Is the server and directory configured correctly?

(Note: This is assuming you’re on an Apache web server)
The directory has to have ExecCGI set either in the .htaccess file or directly in httpd.conf for the directory containing the CGI. Explaining how to configure Apache is beyond the scope of this article, but if it’s not configured correctly, the CGI won’t work.

3. Are the permissions correct?

This is one of the most common reasons your script won’t work (and the step that eluded the guy who swore he had it configured correctly). It’s not just your script, or the directory containing your script, it’s the entire path that the webserver will have to traverse to get to the file.

The script needs to have execute permissions, and Apache is very specific about what permissions it and the directory containing it should be. If you give it full permissions Apache will not run it to avoid creating a security hole. You can use the chmod command to set the permissions (e.g. chmod 755 cgi-bin/script.cgi;chmod 755 cgi-bin).

If things still aren’t working, double and triple check that you’ve followed all of the steps. If it still doesn’t work, try looking at the Apache error_log to see what might be going wrong.

how to get cgis to work - Amidst a tangled web.

No comments: