Perl CGI

Perl CGI

Posted by: entorb
Posted on: 2008-07-17 06:47:00

Hi there,
I am new to dreamhost and to perl CGI too, but I want to give it a try.

Unfortunately I am getting only an "Internal Server Error". The error.log speaks about "Premature end of script headers".
Can anyone help me with this?

What I did in detail:
I created a folder cgi-bin in my domain-dir, placed a small test script in it
8<---- test.pl
#!/usr/bin/perl
print "<p>Hello</p>\n";
8<----
# chmod 755 -R cgi-bin

# whereis perl
tells me /usr/bin/perl
# ./test.pl
works fine

In this forum and the wiki I found many people having problems with access rights, so I did a chmod 777 on the files, but this didn't do the trick.

Greetings

Re: Perl CGI

Posted by: Atropos7
Posted on: 2008-07-17 21:42:00

In reply to:

I created a folder cgi-bin in my domain-dir, placed a small test script in it
8<---- test.pl
#!/usr/bin/perl
print "<p>Hello</p>\n";
8<----
# chmod 755 -R cgi-bin

# whereis perl
tells me /usr/bin/perl
# ./test.pl
works fine

In this forum and the wiki I found many people having problems with access rights, so I did a chmod 777 on the files, but this didn't do the trick.


Here is what happens when your run a CGI script:

1. The web server sets up a "CGI environment" and then executes the script.
2. The "perl" program runs your script
3. The web server expects the script output to start with headers followed by a blank line followed by the content to pass along to the web browser.

From the error message you can tell that the failure is with step #2 or step #3. From your code, it is apparent that it is at least step #3 - you did not output a set of headers.

At the minimum you need a Content-Type header. Observe.

$mime_type = 'text/html';
$header_content_type = "Content-Type: $mime_type\n";
@headers = ($header_content_type);
$blank_line = "\n";
$content = "<p>Hello</p>\n";

print
@headers,
$blank_line,
$content;

The reason you get "premature end of headers" messages is because the output the web server sees is not a set of headers. Other than skipping the headers like your script does, you can also get this message if the perl program has a problem interpreting your code (syntax errors, etc) or crashing.


cool openvein.org -//-

Re: Perl CGI

Posted by: entorb
Posted on: 2008-07-18 03:26:00

Thanks for your quick reply, but your script did not work either. The output is the same. The logfile says:
8<--- error.log
suexec policy violation: see suexec log for more details
Premature end of script headers: test3.pl
8<---

Here is your script I used:
8<---test3.pl
#!/usr/bin/perl
$mime_type = 'text/html';
$header_content_type = "Content-Type: $mime_type\n";
@headers = ($header_content_type);
$blank_line = "\n";
$content = "<p>Hello</p>\n";
print
@headers,
$blank_line,
$content;
8<---

I did a chmod 775 test3.pl before running it.

Running it in a terminal works fine:
8<---
Content-Type: text/html

<p>Hello</p>
8<---

Any further ideas?


Re: Perl CGI - Solved

Posted by: entorb
Posted on: 2008-07-19 08:13:00

I found the solution, it was a stupid problem:

First thing is that Atropos7 was right about the header:
Content-Type: text/html
is necessaray.

The second problem was that the permissions have to be set to 755 instead of 775, as can be found in the wiki:
http://wiki.dreamhost.com/Perl#Perl_debugging
> suexec requires that all cgi scripts and the directories
> in which they reside _not_ be writable by anyone but the owner

Thanks to Craig@DH for making me checking the wiki again!



Tags: internal server errortest script