Apache – How to Handle Python Scripts (*.py) via Browser

apache-2.2python

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.

Best Answer

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.