natol44 has asked for the wisdom of the Perl Monks concerning the following question:

I didn't think I had to ask help about this, as it is a very very basic point, but after a few days trying, well... I just moved sites from a Centos 7 server to a Centos 8 server, Apache. I builded the server from scratch, through the virtualmin package.

And so, perl scripts do not work at all in the browser (they work in the console). Permissions are ok, all seems to be ok but... Directives of virtualhosts are ok

use CGI; $q = new CGI;
print $q->header(-charset=>'UTF-8');
print "hello";

Also tried
print "Content-type: text/html\n\n";
print "hello";

Well nothing works :(

Thank you :)
  • Comment on Infamous "Premature end of script headers"

Replies are listed 'Best First'.
Re: Infamous "Premature end of script headers"
by NetWallah (Canon) on Dec 14, 2021 at 17:49 UTC
    "perl scripts do not work" is a very poor problem description.

    Are you saying you see nothing output in the browser ?
    Have you checked the web-server logs for error message ?
    You do say " "Premature end of script headers" in he title - that indicates header problem - but your headers appear correct.

    There are plenty of pitfalls trying to get your first CGI script to work - you have the right approach by printing headers first - but you need to check - are the scripts getting loaded by the web server ? executable ? cgi framework loaded ?

    The answers are in the web server error logs.

    UPDATE: - Looks like you are missing the shebang line.

    Here is a working script:

    #!/usr/bin/perl print "Content-Type: text/html;\n\n"; print "<h1>Hello, cgi world</h1>"; print "<table>\n"; print qq|<tr><td><b>$_</b> </td><td> $ENV{$_}</td></tr>\n| for sort ke +ys %ENV; print "</table>\n";

                    "If you had better tools, you could more effectively demonstrate your total incompetence."

Re: Infamous "Premature end of script headers" (FAQ)
by Anonymous Monk on Dec 14, 2021 at 22:03 UTC
Re: Infamous "Premature end of script headers" -- -nph
by Discipulus (Canon) on Dec 15, 2021 at 08:07 UTC
    Hello nato144,

    welcome back to 2000 :)

    While I'd suggest to abandon CGIs and move to PSGI aware technologies, I remember there was a nph directive for CGIs as you can see at cgifaq.2.html#6: did you tried it too?

    See also USING-NPH-SCRIPTS

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Infamous "Premature end of script headers"
by natol44 (Sexton) on Dec 16, 2021 at 20:02 UTC
    Well finally I solved it by disabling suexec for the concerned virtualhost. It is a Centos8/Virtualmin problem.

    I will anyway follow your recommendations and have a look at mod_perl.

    Thank you to all!

Re: Infamous "Premature end of script headers"
by Anonymous Monk on Dec 16, 2021 at 13:43 UTC
    You need to read the server error logs. They should contain the STDERR from the script, including the reason for the failure.
Re: Infamous "Premature end of script headers"
by natol44 (Sexton) on Dec 15, 2021 at 19:37 UTC
    Thank you for all replies, recommending me to try other options. The point is that the same script WORKS when ran by root (in a cron task) and fails when ran in the browser. Permission is 755 to be run in the browser).

    I would like to understand what's going wrong with this :)
      problem is: "run in the browser" is usually "run on server, wrapped in requests and responses from and to the browser", i.e.
      1. browser sends request to server
      2. server runs script
      3. server sends response back
      4. Browser renders response
      and if it "fails/does not work", you have to find out which of these four steps is at fault, and decompose that further...

      Start here, reply if you have problems. If you've rebuilt a server/environment there's likely part of the config which is wrong. Reading the logs and the debugging tips posted previously will most likely track down the problem.

      OK root works fine, but what user the script serving the browser runs under? And with what @INC?

      I would like to understand what's going wrong with this :)

      ;) time to get started, the list is not long

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Infamous "Premature end of script headers"
by vincent_veyron (Beadle) on Dec 14, 2021 at 21:02 UTC

    Hi

    May I suggest you delve into mod_perl?

    Once you get the hang of it, you can do anything you want; I skipped the cgi-bin part altogether
    To install, it's easier to use the distribution packages (Debian in my case, so : apt install libapache2-request-perl libapache2-mod-perl2 libapache2-mod-apreq2 )

    https://compta.libremen.com

    Logiciel libre de comptabilité générale en partie double

      "I've upgraded and a CGI script no longer works" to "you should use mod_perl" is quite the jump.

        True. I was fortunate to find it 15 years ago, when the mod_perl list was very active, hence got a lot of guidance, and there are a few gotchas. Now that I'm replying to you, I recall that I did have to study this book before I could be operational.

        However I found the effort was worth it because you can do so much with mod_perl (I build web based applications for a living, the hooks to the Apache response phases are awesome); and you get to write perl code, not cgi code. Also, very efficient.
        Anyhow, you are right, not for someone who is building a hobby site. But if one wants to make a serious one, it's a great tool.

        https://compta.libremen.com

        Logiciel libre de comptabilité générale en partie double