Troubleshooting CGI Scripts

From OCS Support Wiki

Jump to: navigation, search

Contents

[edit] Introduction

This article will help you troubleshoot CGI scripts.

[edit] Common Problems

Here are the most common reasons CGI scripts fail to execute, giving a 500, or Internal Server Error, and their solutions.

[edit] Incorrect Path to Program

Your CGI script must pass on the first line of the file the correct path to the interpreter you wish to use. Here are the usual paths to the interpreters on our systems:

  • Perl - /usr/bin/perl
  • Ruby - /usr/bin/ruby
  • Python - /usr/bin/python or /opt/python/bin/python
  • Bash - /bin/bash or /bin/sh

Bash note: the /bin/sh interpreter doesn't have all of the functionality of /bin/bash, but runs faster and uses less RAM. Use it if you can, especially for simple scripts.

[edit] Using the cgi-bin Folder

Ironically, the cgi-bin folder is generally not the best place to run your script. Simply move the script to the public_html folder or a folder inside of it to resolve this issue.

[edit] Script Not Marked Executable

The script must have 755 permissions. You can set this via FTP or via SSH with the command:

chmod 0755 script.cgi

In the above example, replace script.cgi with the name of the script file.

[edit] Directory is World Writable

CGI scripts are not allowed to execute in directories that are world writable. You can remove this permission via FTP (remove the execute flag on global or world) or via SSH with the following command:

cd ~/public_html/path_to_script_folder
chmod 0751 .

In the above example, replace path_to_script_folder with the folder name that your CGI script is in. If it's in the public_html folder, then simply replace the first command above with cd ~/public_html.

[edit] File Corruption

The script file may have been uploaded in a non-ASCII mode via FTP, or contains invalid characters. Some editors (especially older Windows editors) will put unnecessary and invalid characters at the end of each line (often seen as ^M). If this is the case, switch to a different editor or consult your editors documentation on how to save files in UNIX or plain ASCII mode.

[edit] An Error in the Script

Sometimes the script generates an error during execution, producing a 500 error. To diagnose what's wrong, connect via SSH and attempt to run the script manually. Also, the script may have a log file that it creates, if so, the answer might lie there. Since all scripts are written differently, diagnosing this issue will vary.

[edit] No 500 Error, Contents of Script Display

If you can see the contents of the script, you may not be using the .cgi file extension. You can add your custom extension by creating an .htaccess file in your public_html with the contents (or adding the following lines to an existing one):

Options +ExecCGI
AddHandler cgi-script .pl

In the above example, we're telling Apache to parse .pl files as CGI scripts. You can change this extension to suit your needs.