Why does my CGI/Perl script fail with an error 500 (Internal Server Error)?

These errors are nearly always related to a coding error within the script, or bad variables being passed into the script. If you encounter an error “500 Internal Server Error” whilst running your CGI and Perl scripts, try the following in order:

– Ensure you have uploaded the script through FTP using ASCII mode (not Binary), or that it has Unix-style carriage returns.

– Check the interpreter path on the first line of the script. For Perl it should always be “#!/usr/bin/perl”.

– Make sure the script filename ends in either “.pl” or “.cgi”.

– Make sure the script can execute. It should have permissions of “755”. Most FTP clients support this.

– Put “use CGI::Carp qw(fatalsToBrowser);” into the second line of your script. This will make Perl print the error instead of the “500 Internal Server Error” page, and will allow you to find out where your script is breaking.

Other potential problems include bad syntax in an .htaccess file (if you’ve created/edited one). Check your error log for details (there are articles on how to get to your error logs).

Why does my script produce a ‘file permissions error’?

CGI scripts need certain file permissions to be set if they are to work properly. In general, CGI scripts need to have permissions set at 755 (rwxr-xr-x), although PHP files only need 644 (rw-r–r–).

The folders containing CGI scripts also need certain permissions. In particular, if a folder has very lax permissions, such as 777 (rwxrwxrwx), the web servers will not execute the scripts contained in it. Folders containing CGI scripts should ideally have permissions of 711 (rwx–x–x). This also applies to the folders that contain the first folder, if you have a multi-level folder structure, although 755 for folders is also fine.

How do I set permissions on files and scripts?

Doing a CHMOD (changing a file’s permissions) is the setting of access privileges for a file. This tutorial shows the process in one of the most common FTP applications called WS_FTP but the process is very similar with other FTP clients.

First connect to your hosting via FTP using the settings provided in your setup email.

Next click on the directory you have uploaded the cgi script into and right-click on the file you would like to set permissions for. The right-click menu is then displayed, and by clicking the “CHMOD” (or “Properties” with some FTP software) option we get to the main part of doing a CHMOD.

The new menu that appears has three columns – Owner, Group, and Other. Each column has three checkboxes below them – Read, Write, and Execute. eg for cgi scripts needing 755 permission – In the “Owner” column, check all three boxes. In the “Group” column check the first and third checkboxes. In the “Other” column, check the first and third boxes.

Finally, click “OK” to complete the CHMOD. Test your script or file again by calling it from your web browser. Everything should work correctly, but if you are still seeing an Error 500 Server Error then it is not because of your permissions.

What is Perl?

Short for Practical Extraction and Report Language, Perl is a programming language developed by Larry Wall, especially designed for processing text. Because of its strong text processing abilities, Perl has become one of the most popular languages for writing CGI scripts. Perl is an interpretive language, which makes it easy to build and test simple programs.