I’ve had a couple customers in the past month or so run into a problem where they were trying to run a simple Perl script, but kept getting HTTP 500 errors (Internal Server Error) despite double checking through their code, and simplifying it down to almost nothing. One had spent half a day troubleshooting a seemingly simple error. Apache would log something like this:
[Sat Nov 03 22:46:57 2007] [error] [client 22.214.171.124] (2)No such file or directory: exec of '/var/www/cgi-bin/hello.pl' failed [Sat Nov 03 22:46:57 2007] [error] [client 126.96.36.199] Premature end of script headers: hello.pl
By time they contacted me, they were so frustrated, that it is hard to tell them how easy the fix is. In both cases, the users had created the files on a Windows machine and then uploaded them to a Linux server. Many windows applications happen to save text files in a slightly different format than Linux does. Specifically, windows uses the two characters CR and LF (Carriage Return, and Line Feed), where Linux simply uses just a LF.
Carriage Return is the ASCII character 13, and is also recognized as ‘\r’, or ‘^M’. The Line Feed character is ASCII code 10, and represented as ‘\n’, or ‘^J’.
An attempt to run this same ‘hello.pl’ script via a command line results in:
[root@host cgi-bin]# ./hello.pl : bad interpreter: No such file or directory
To fix, simply convert the file to a Unix text file format. Your text editor may have an option to save the file in a Unix format. If you are stuck with the editor you have, you can use the ‘dos2unix‘ command which is available on pretty much any Linux box.
[root@host cgi-bin]# dos2unix hello.pl dos2unix: converting file hello.pl to UNIX format ...
And now your script will magically work (You did remember to add the content-type header, right):
[root@flickerworks cgi-bin]# ./hello.pl Content-type: text/html Hello World