how to get CGIs to work

When I was working as a system administrator one of the frequent complaints we heard was that users couldn’t get their CGI scripts to work. We had a set of steps to follow to get them to work, but a few people still had trouble. One user in particular swore up and down he had followed each of the steps to the utmost, and yet his CGI didn’t work. I was doubtful that he followed all of the correctly, so I went to his home directory to take a look.

Sure enough, he hadn’t followed one of the steps completely and after I finished it for him, the CGI worked fine. He sheepishly apologized when I explained what I had done to fix it, but the fact remains that it is an elusive problem, especially for those unfamiliar with Unix systems. Here are the steps I suggest you follow to get a CGI script to work correctly.

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. If you can’t figure it out, comment here and I’ll see what I can do.

Comments are closed