2
votes

I think I made some stupid mistake but I am not able to find it. Trying to run a simple python cgi script on apache webserver. My Code is

#/usr/bin/env python
import cgitb
import cgi
cgitb.enable()
print 'Content-Type: text/html'
print 
print '<html>'
print '<head><title>My first Python CGI app</title></head>'
print '  <body>'
print '    <p>something to try<p>'
print '  </body>'
print '</html>'

this code was in /usr/lib/cgi-bin I tried to run it by localhost/cgi-bin/test.py but it shows 500 INTERNAL SERVER ERROR. I tried with changing its permissions,nothing happened. I am able to run html file from /var/www/html/file_name.html So I tried moving this in www/ and www/html/ (in both) with making cgi-bin folder and without it, nothing solved.

I had added this in apache2.conf

    <Directory /usr/lib/cgi-bin>
    Options +ExecCGI
    AddHandler cgi-script .cgi .py
    Order allow,deny
    Allow from all
    </Directory>

and var/log/apache2/error.log says
[Tue Jan 13 22:54:11.422641 2015] [cgid:error] [pid 10157:tid 140434248509184] [client 127.0.0.1:44941] End of script output before headers: test.py
[Wed Jan 14 00:20:24.434351 2015] [cgid:error] [pid 26512:tid 140434573543296 (8)Exec format error: AH01241: exec of '/usr/lib/cgi-bin/test.py' failed
[Wed Jan 14 00:20:24.434778 2015] [cgid:error] [pid 10158:tid 140434456491776] [client 127.0.0.1:45974] End of script output before headers: test.py

1
Firstly, you need to print out the content type first (print 'Content-Type: text/html'; print); if that doesn't work, you should post your apache configFoon
didn't work. do you mean apache2.conf?aniket
Yeah, or however you're configuring apache to use cgi (also, does /var/log/httpd/error.log (or similar) give any more details?)Foon
I have added some more details in my question. anything?aniket
You have a typo; see my answer for more details and tips.Alex Reynolds

1 Answers

5
votes

When you run into 500 errors, one tip is to run the script on the command line, which often tells you of typos or other interpreter-level problems. Looking at the log can also report the same errors, but it is usually easier to see what's going on by directly running the script.

Specifically, at least one issue is the typo in this line:

#/usr/bin/env python

Which should be:

#!/usr/bin/env python

The clue is that running the script as-is shows errors that would otherwise show up via a bash interpreter, when you want this to run through the environment's python interpreter.