I have a very basic Python script being run as CGI, I am able to run it with only print statements and they execute and display as HTML. When I try to do any file system calls such as using logging, making files or using subprocesses I get an error 500.
#!/usr/bin/python
import cgitb; cgitb.enable()
import cgi
import sys
from subprocess import call
call('ls') #Error here, if I remove I get no error 500 and see "Hello"
form = cgi.FieldStorage()
print "Content-Type: text/html"
print
print "<html><body>Hello"
print "</body></html>"
The CGI script has execute permission:
chmod a+x example.cgi
In my Apache configuration I have the virtual hosts to execute CGI setup:
<Directory "/path/to/cgi/cgi-bin/">
AddHandler cgi-script cgi pl
Options +FollowSymLinks +ExecCGI
AllowOverride None
</Directory>
And setup the CGI module for Apache:
sudo a2enmod cgi
If I run the cgi script directly on the server, all the file manipulation works perfectly, if I try to access it from the browser, i.e example.com/test.cgi it gives me the 500 error. I also ran dos2unix
to make sure it's formatted correctly.