CGI problem

Discussion in 'PSA 2.0 Troubleshooting and Questions' started by sumsar, Nov 26, 2001.

  1. sumsar

    sumsar Guest

    I have tried everything!!

    I get an 500 internal server-error when trying to run a cgi script.
    Both mod_perl and cgi is checked in Plesk.
    In my error_log it says:

    Premature end of script headers

    I have made a test script:
    test.cgi

    #!/usr/bin/perl
    print "hello world\n";

    When I try to run the script via ssh on the server:
    ./test.cgi

    It prints hello world. So the script works FINE!
    The owner is right and the group is right.
    It has 755 permissions.
    The folder /var/log/httpd exists on the system.

    I AM GOING OUT OF MY MIND!!! :( :(
     
  2. kark

    kark Guest

    Have you checked that the script doesn't have a broken line somewhere?

    Kark
     
  3. sumsar

    sumsar Guest

    There is no broken lines in the script.
    It is a 2 line script, it works fine on everything else then my plesk box.
     
  4. sumsar

    sumsar Guest

    Just to be 100% sure I logged in as root on my box.
    And did the following:
    touch test.cgi
    vi test.cgi
    and typed:
    #!/usr/bin/perl
    print "hello world\n";

    in the file.
    I saved it and chmod'ed it to 755
    Then I chown'ed it like this:
    chown red:psacln test.cgi

    red is the plesk user.
    I the tried to call the script in my browser, and I still got the same error.

    I made a ./test.cgi and it printed hello world at my shell.
     
  5. kark

    kark Guest

    So what does it say when you change the first line to
    #!/usr/bin/perl -w ?

    Kark
     
  6. sevenfive

    sevenfive Guest

    sumsar
    there is a recognised problem with Plesk and cgi. Ask them to fix this for you.
    One workround depending on your script is to add -w to the shebang.
    #!usr/bin/perl -w
    hope you have some luck
    alex
     
  7. rackov

    rackov Guest

    I do not want to sound stupid, but have you checked if the domain has Perl and CGI checked?

    If yes, please forget about this post :|
     
  8. supercool

    supercool Guest

    What do you see in the suexec_log file?
     
  9. davidb

    davidb Guest

    U got everything right except this one little line that makes it work as cgi

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


    put that before print hello, it is always the little things
     
  10. webadmin

    webadmin Odin Team

    Messages:
    66
    why "-w"

    I run Tons of scripts on my servers all the time and never have to use "-w"

    Of course you need the "#!usr/bin/perl" stuff. That's normal, but I'm not sure why you would need the "-w"
     
  11. kark

    kark Guest

    The -w is for debugging only.

    Kark
     
  12. aswitzer

    aswitzer Kilo Poster

    Messages:
    60
    why "-w"?

    I was having the exact same problem as sumsar, and the suggestion by kark to add "-w" to the end of the first line solved the problem.

    My question is why does adding the debug flag fix this problem with viewing perl thru the browser, and does it add overhead?

    Is there a better fix for this?
     
  13. sevenfive

    sevenfive Guest

    -w should be used all the time when running perl scripts as a precaution.
    it'll help prevent wayward scripts getting up to mischief.
    if you still have problems try disabling mod_perl on your setup from within plesk (this will default back to perl) and run your scripts with regular perl.
     
  14. aswitzer

    aswitzer Kilo Poster

    Messages:
    60
    Thank you for the explanation Alex.
     
  15. sammy

    sammy Guest

    This is the strangest thing I've seen ... running this perl script kept giving me "
    failed to open log file
    fopen: Permission denied
    "
    in the error_log file

    I add the -w to /usr/bin/perl in the first line of the script and then it suddenly started working ?? Any ideas why it won't work without the -w option ?

    Also how do I disable mod_perl from withink plesk ? Is it an option in the plesk control panel ?

    Thanks,
    Sma
     
  16. sevenfive

    sevenfive Guest

    im guessing plesk wont run scripts without the -w so that errors are made apparent and hopefully fixed by the developer. a bad script could potentially wander off and bring a server down if error reporting isn't enabled.
    mod_perl is a strict (and excellent) perl environment but can cause probs under plesk. you can disable it in the plesk control panel easily. hope this helps.
     
  17. aswitzer

    aswitzer Kilo Poster

    Messages:
    60
    You can uncheck mod_perl support from the hosting button for a specific domain in plesk. Once you choose the type of hosting, it's on the second page right after "CGI support".

    Unfortunately, I didn't find that disabling mod_perl solved this problem - only adding "-w" to every CGI script seems to work.
     
  18. sammy

    sammy Guest

    yeah same here ... even with mod_perl disabled for that host I still had to add the -w

    There has to be a way for scripts to continue to run without the -w

    and it's not all scripts .. random ones.
     
  19. msalerno

    msalerno Guest

    Your problem has nothing to do with Apache, perl, plesk or CGI. Your problem is that you need to send HTTP headers !

    Since you are using Perl I would recommend using the CGI.pm so that you don't have to write all of the HTTP headers and other stuff.

    Go to:
    http://stein.cshl.org/WWW/CGI/

    There are hundreds of ways to do this. Although the CGI.pm does not look to be the most efficient way to do things, I promise you that if you are doing any CGI scripting with perl, it will save you hours !

    A great book that got me into cgi writing with perl is:
    O'reilly "CGI programming with perl"

    If you don't understand any of the below scripts you need to get a good book ! Not only will it teach you about cgi scripting but you will also learn about http communications.

    Here is an example without the CGI.pm

    #!/usr/bin/perl -wT
    use strict;

    print "Content-type: text/plain\n\n";
    print "Hello World !";


    Here are a few with the CGI.pm

    #!/usr/bin/perl -wT
    use strict;
    use CGI;
    my $q = new CGI;

    print $q->header;
    print $q->start_html;
    print "Hello World !";
    print end_html;


    Or you could do:

    #!/usr/bin/perl -wT
    use strict;
    use CGI;
    my $q = new CGI;

    print $q->header;
    print $q->start_html(),("Hello World !");
    print end_html;
     
  20. sammy

    sammy Guest

    I wish the solution was that easy ... this is the weirdest thing I've ever seen. I think I may have finally figured it out .. my proftpd settings were at binary when I uploaded this test script env.cgi
    Even though I edited it in vi .. deleted all the lines, retyped the perl code .. it still messes up. here's proof :

    [root@server1 htdocs]# cat env.cgi
    #!/usr/bin/perl

    print "Content-Type: text/html\n\n";
    print "Hello\n";

    [root@server1 htdocs]# cat env2.cgi
    #!/usr/bin/perl

    print "Content-Type: text/html\n\n";
    print "Hello\n";

    [root@server1 htdocs]# ls -l env.cgi
    -rwxr-xr-x 1 roslyn psacln 75 Mar 22 11:53 env.cgi
    [root@server1 htdocs]# ls -l env2.cgi
    -rwxr-xr-x 1 roslyn psacln 71 Mar 22 11:53 env2.cgi
    [root@server1 htdocs]# ./env2.cgi
    Content-Type: text/html

    Hello
    [root@server1 htdocs]# ./env.cgi
    bash: ./env.cgi: No such file or directory
    [root@server1 htdocs]#

    SO you see env2.cgi is 4 bytes larger than env.cgi.
    The only solution is to completely delete the file and recreate it :

    [root@server1 htdocs]# rm env.cgi
    rm: remove `env.cgi'? y
    [root@server1 htdocs]# cat>env.cgi
    #!/usr/bin/perl

    print "Content-Type: text/html\n\n";
    print "Hello\n";
    [root@server1 htdocs]# chmod 755 env.cgi
    [root@server1 htdocs]# ./env.cgi
    Content-Type: text/html

    Hello

    and it works!
     

Share This Page