简体   繁体   中英

Executing .py python gives apache error 2 (internal server error)

I'm trying to execute .py files from the html folder of my ec2 instance.

I have python 2.6.8 installed, and have configured the httpd.conf to recognize and allow execution of cgi .py files in the /var/www/html directory.

The test index.py file has the follow script:

#!/usr/bin/python

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain\r\n\r\n"
print "Hello World!"

Whenever I run the mywebsite.com/index.py, the browser says there is an internal server error. From the httpd error_log, I get the following:

[Sun Sep 09 21:52:46 2012] [error] (2)No such file or directory: exec of '/var/www/html/index.py' failed
[Sun Sep 09 21:52:46 2012] [error] Premature end of script headers: index.py

What I think is happening is that the #!/usr/bin/python isn't finding the python to execute the script. So I typed in the following into the console:

ls -l -F /usr/bin/python*

and it gives me the following:

lrwxrwxrwx 1 root root    9 Sep  7 18:31 /usr/bin/python -> python2.6*
lrwxrwxrwx 1 root root    9 Sep  7 18:31 /usr/bin/python2 -> python2.6*
-rwxr-xr-x 2 root root 3536 Jun 29 06:51 /usr/bin/python26*
-rwxr-xr-x 2 root root 3536 Jun 29 06:51 /usr/bin/python2.6*
-rwxr-xr-x 1 root root 1418 Jun 29 06:51 /usr/bin/python2.6-config*

So technically the request should be working. What am I missing?

Note: I should mention that the .py file has been chmod-ed to be executable and have 777 access (for the time being).

Edit: I have WSGI installed, but I cant tell if it is running or not. I also typed in whereis python, and got the following:

python: /usr/bin/python2.6-config /usr/bin/python /usr/bin/python2.6 /usr/lib/python2.6 /usr/include/python2.6

Best practice for running python programs through Apache is using WSGI.

A good versions is: http://code.google.com/p/modwsgi/

Some quick start guides that should give you an idea of how to configure everything for use: Installation - http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide Configuration - http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

Between those two pieces of documentation you should have everything you need to configure and run a python application through mod_wsgi. Specifically pay attention to http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide#WSGI_Application_Script_File and http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide#Mounting_The_WSGI_Application once you have mod_wsgi loaded into Apache.

Some of the few reasons that they recommend you not use CGI are detailed here: http://docs.python.org/howto/webservers.html#common-gateway-interface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM