Apache not handling python scripts (*.py) via browser

8,557

Solution 1

Error 13 from apache indicates a filesystem permissions problem.
Is SElinux enabled? (what's the output of "ls -laZ test.py")

I doubt it's a problem with ScriptAlias or AddHandler/ExecCGI (either of which will get apache to execute scripts) - since you're getting a 500 error and not the python source apache is clearly trying to execute the file.

Solution 2

I've only ever used mod_python for a Trac install and they provide fairly explicit instructions for their application.

However, while we were testing mod_python, I found this article helpful - you may too.

Solution 3

Apache will only execute files that are located in designated cgi-bin directories. Everything else is considered content that is passed to the viewer. Your root directory isn't, and shouldn't, be marked as such.

Use the ScriptAlias <url-path> <directory> directive to set your cgi-bin directories. eg: ScriptAlias /cgi-bin/ /webroot/cgi-bin/. Copy your scripts there, then call http://www.example.com/cgi-bin/test.py. That should work for you.

Share:
8,557

Related videos on Youtube

Structure
Author by

Structure

Updated on September 17, 2022

Comments

  • Structure
    Structure almost 2 years

    Edit: OS is CentOS 5

    I installed Python 2.5.5 and am trying to run some Python scripts via the browser.

    Honestly, I have not worked with Python before. I attempted to load the python module into Apache, but it is already loaded and was skipped. I also confirmed that I can run python scripts from my command line if I make them executable.

    However when I put "http://www.example.com/test.py" into my browser, it returns unparsed HTML as follows:

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>500 Internal Server Error</title>
    </head><body>
    <h1>Internal Server Error</h1>
    <p>The server encountered an internal error or
    misconfiguration and was unable to complete
    your request.</p>
    <p>Please contact the server administrator,
     root@localhost and inform them of the time the error occurred,
    and anything you might have done that may have
    caused the error.</p>
    <p>More information about this error may be available
    in the server error log.</p>
    <hr>
    <address>Apache/2.2.3 (CentOS) Server at www.example.com Port 80</address>
    </body></html>
    

    I also have the following in my httpd.conf file.

    AddHandler cgi-script .py
    

    I am stumped as I do not know where to look from here. Does this ring a bell for anyone? Hopefully nothing too obvious that I am overlooking here...

    Thank you in advance.

    Edit: Found the following in the Apache error_log.

    [Fri Feb 26 19:58:38 2010] [error] [client xxx.xxx.xxx.xxx] (13)Permission denied: exec of 'test.py' failed
    [Fri Feb 26 19:58:38 2010] [error] [client xxx.xxx.xxx.xxx] Premature end of script headers: test.py
    [Fri Feb 26 20:04:56 2010] [notice] mod_python: Creating 4 session mutexes based on 256 max processes and 0 max threads.
    
    • joschi
      joschi over 14 years
      What does your Apache error log say? If you are using SuExec: What does your suexec.log say? Both files should be found in /var/log/apache2/.
    • Structure
      Structure over 14 years
      I posted that information into my original message.
    • Graham Dumpleton
      Graham Dumpleton over 14 years
      FYI. You don't need mod_python if all you want to do is write CGI scripts using Python.
  • Structure
    Structure over 14 years
    I configured the ScriptAlias as follows: ScriptAlias /cgi-bin/ /webroot/test/ Then I copied my script to that directory and called it from the browser, but the same error occured.
  • Structure
    Structure over 14 years
    -rw-r--r-- user user test.py
  • quadruplebucky
    quadruplebucky over 14 years
    Ah, chmod a+x that script. As an aside, cat /etc/sysconfig/selinux or sestatus will tell you SElinux status. If SElinux is not enabled then...
  • Structure
    Structure over 14 years
    SELINUX=disabled, also, I ran chmod a+x but to no avail. I am going to take a look Apache permissions, etc.
  • quadruplebucky
    quadruplebucky over 14 years
    Wait, I didn't notice this before... AddHandler python-program .py (not cgi-script) Also, try renaming your script - python has a builtin module "test".
  • Structure
    Structure over 14 years
    Thanks, I added the following three lines within a <Directory> directive. AddHandler python-program .py, PythonHandler mytest, PythonDebug On... With these directives in my httpd.conf file I am able to run the test script. I am going to mark this as the answer given that it got me closest to the solution. Thank you for the assistance!