10

I am trying to run a simple cgi script after configuring my server.

My script looks like this:

print "Content-type: text/html"
print
print "<html><head><title>CGI</title></head>"
print "<body>"
print "hello cgi"
print "</body>"
print "</html>"

When I go to my scripts url http://127.0.0.1/~flybywire/cgi-bin/main.py I get:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

And in error.log I get the following:

[error] (8)Exec format error: exec of '/home/flybywire/www/cgi-bin/main.py' failed [error] [client 127.0.0.1] Premature end of script headers: main.py

Other info: Apache/2.2.8 (Ubuntu) DAV/2 SVN/1.4.6 Server at 127.0.0.1 Port 80

flybywire
  • 261,858
  • 191
  • 397
  • 503

8 Answers8

25

You might need a #!/usr/bin/python at the top of your script to tell Apache to use Python to execute it. At least, I did that and it worked for me :-) .

Paul Stephenson
  • 67,682
  • 9
  • 49
  • 51
  • 1
    I think add `#!/usr/bin/python` to all cgi script is scary. How to set it by apache SetEnv PYTHONPATH? – gonjay Mar 10 '15 at 09:21
12

Also, save the file (if this is a Linux server) with Unix line endings. You did make it executable using chmod +x didn't you?

You can use #!/usr/bin/env python to cover the current running Python version if you're running in various environments (hence the env part).

Dave Everitt
  • 17,193
  • 6
  • 67
  • 97
  • When I tried it without `chmod +x` there was a different error: `(13)Permission denied: exec of '/home/flybywire/www/cgi-bin/main.py' failed`. – Paul Stephenson Sep 23 '09 at 08:58
  • 1
    That's simply because chmod +x makes the file permissions executable - making the file executable is a prerequisite, really. – Dave Everitt Sep 23 '09 at 10:15
1

It looks like Apache has trouble executing it. Typically for a unix script you also need to specify the interpreter at the top of the script.

Try adding this to the top:

#!/usr/bin/python
Andre Miller
  • 15,255
  • 6
  • 55
  • 53
1

Putting

#!/usr/bin/env python

on the top of the script works fine. I put it on top, but Netbeans was putting extra code (import commands) by itself on the top of the page which drove me crazy :(

Shafiul
  • 2,832
  • 9
  • 37
  • 55
0

Maybe your problem is that new python version needs parentheses ( ).

So your:

print "<body>"

Now should to be:

print ("<body>")
Mark Chackerian
  • 21,866
  • 6
  • 108
  • 99
Heitor Giacomini
  • 384
  • 2
  • 12
0

remove the 2nd line in your program (print) I tried it on my apache server (mac os x) it works fine. don't forget to chmod 755 and reboot with sudo apachectl restart This is for python 2.7

print "Content-type: text/html"
print "<html><head><title>CGI</title></head>"
print "<body>"
print "hello cgi"
print "</body>"
print "</html>"
0

Apache does not know where to find python on your PC to execute that file. Try to add #!C:/python as 1st line on your .py file (or wherever location Python is installed on your PC).

0

If it's python 3, you may add #!/usr/bin/python3 at the top of the program instead of #!/usr/bin/python. (As I'm French, I think my English is not totally correct)

French
  • 1