Cgi scripts in apache not working

Discussion in 'Python' started by David, Jul 28, 2005.

  1. David

    David Guest

    I've looked all over the place for an answer, and the only one I can
    find doesn't mean anything to me. The end of line issue with writting
    it in Windows and then uploading it doesn't help me since I'm doing
    this all from Linux. I've been trying to get python cgi scripts to
    work, and I always end up with a "Premature end of script headers" in
    the error log.

    The scripts are executable by all and my httpd.conf seems fine. I also
    tested it out in /var/www/cgi-bin/ and there it seems to work fine.

    test2.py:

    #!/usr/bin/python
    print "Content-type: text/html\n\n"
    print """<html>
    <head></head>
    <body>
    <h1>Hello World</h1>
    </body>
    </html>"""

    httpd.conf:

    <Directory "/home/*/public_html/cgi/"> << Tried it with "" and no ""
    Options ExecCGI << Tried with "Options +ExecCGI"
    AddHandler cgi-script .cgi .py << Tried with "SetHandler
    cgi-script", but still no go
    </Directory>

    error_log:

    [Thu Jul 28 11:44:53 2005] [error] [client xxx.xxx.xxx.xxx] Premature
    end of script headers: test2.py

    suexec.log:

    [2005-07-28 11:44:53]: uid: (500/davidnoriega) gid: (500/500) cmd:
    test2.py
    [2005-07-28 11:44:53]: directory is writable by others:
    (/home/davidnoriega/public_html/cgi)
    David, Jul 28, 2005
    #1
    1. Advertising

  2. David

    Paul Boddie Guest

    David wrote:
    >
    > httpd.conf:
    >
    > <Directory "/home/*/public_html/cgi/"> << Tried it with "" and no ""
    > Options ExecCGI << Tried with "Options +ExecCGI"
    > AddHandler cgi-script .cgi .py << Tried with "SetHandler
    > cgi-script", but still no go
    > </Directory>
    >
    >
    > error_log:
    >
    > [Thu Jul 28 11:44:53 2005] [error] [client xxx.xxx.xxx.xxx] Premature
    > end of script headers: test2.py


    I assume that you have a UserDir directive elsewhere in your
    configuration; otherwise, what you've written probably won't be enough.
    See this guide for the full details:

    http://httpd.apache.org/docs/2.0/howto/public_html.html

    I typically find that using the ScriptAlias directive does what I want,
    although I don't work with user directories myself. Here's an example:

    ScriptAlias /cgi/ "/home/paul/some_directory/"

    I imagine that the ScriptAliasMatch directive would also be able to do
    the home directory matching that you are doing in your configuration
    file. See the documentation for both directives here:

    http://httpd.apache.org/docs/2.0/mod/mod_alias.html#scriptalias

    Apart from using ScriptAlias, I don't usually do anything more than
    check the ownership/permissions on the CGI directory and the script.
    Naturally, the PYTHONPATH (or sys.path) needs to be configured to find
    non-installed modules and packages, but such things are beyond this
    particular problem.

    Paul
    Paul Boddie, Jul 28, 2005
    #2
    1. Advertising

  3. David a écrit :
    > I've looked all over the place for an answer, and the only one I can
    > find doesn't mean anything to me. The end of line issue with writting
    > it in Windows and then uploading it doesn't help me since I'm doing
    > this all from Linux. I've been trying to get python cgi scripts to
    > work, and I always end up with a "Premature end of script headers" in
    > the error log.
    >
    > The scripts are executable by all


    Is it executable by yourself ?

    and my httpd.conf seems fine. I also
    > tested it out in /var/www/cgi-bin/ and there it seems to work fine.
    >
    > test2.py:
    >
    > #!/usr/bin/python
    > print "Content-type: text/html\n\n"
    > print """<html>
    > <head></head>
    > <body>
    > <h1>Hello World</h1>
    > </body>
    > </html>"""
    >
    > httpd.conf:
    >
    > <Directory "/home/*/public_html/cgi/"> << Tried it with "" and no ""


    shoud be without "" AFAIK (well, it's without on all our servers !-)

    > Options ExecCGI << Tried with "Options +ExecCGI"


    > AddHandler cgi-script .cgi .py << Tried with "SetHandler
    > cgi-script", but still no go


    <ot>
    SetHandler tells apache to treat everything as a cgi , whatever the .xxx
    </ot>


    I've tried your script (renamed py.cgi, because .py is not in my default
    apache conf and I wanted to try it out of the box), with the following
    conf for userdirs:

    <Directory /home/*/public_html/cgi-bin>
    Options +ExecCGI -Includes -Indexes
    SetHandler cgi-script
    </Directory>

    runs fine.

    I've then added a .htaccess in my ~/public_html:
    Options +ExecCGI
    AddHandler cgi-script .cgi

    And mv'd py.cgi from ~/public_html/cgi-bin to ~/public_html.

    runs fine.


    > error_log:
    >
    > [Thu Jul 28 11:44:53 2005] [error] [client xxx.xxx.xxx.xxx] Premature
    > end of script headers: test2.py
    >


    What are the previous and next lines ? I've found such a line in my
    error_log, due to me forgetting to make the script executable, and there
    was a

    [Fri Jul 29 01:14:33 2005] [error] [client 127.0.0.1] (13)Permission
    denied: exec of '/home/bruno/public_html/cgi-bin/py.cgi' failed

    before.

    Then I tried chmod u-x py.cgi, and got the same error message as you
    did, but this time with no "Permission denied" before.

    Aayway, I think your problem is with permissions and/or your apache
    conf, so you may find more useful help on an apache ng.

    My 2 cents
    Bruno Desthuilliers, Jul 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    320
  2. Replies:
    1
    Views:
    357
    =?ISO-8859-1?Q?Stefan_N=E4we?=
    Nov 1, 2005
  3. davidj411
    Replies:
    0
    Views:
    483
    davidj411
    Jun 27, 2008
  4. James
    Replies:
    1
    Views:
    89
    Nigel Horne
    Aug 4, 2003
  5. BestFriend
    Replies:
    2
    Views:
    746
Loading...

Share This Page