Reputation: 37
I have been trying to run a simple python script hello.py
with CGI but am getting 500 Internal Server Error.
My python code.
#!/usr/bin/python2.7
print '<html>'
print '<head>'
print '<title>Hello World - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello World! This is my first CGI program</h2>'
print '</body>'
print '</html>'
The directory which i have the python script running is in /var/www/crunchworld
.The conf file which i enabled is in `/etc/apache2/conf-available/crunchworld.conf
The conf file looks like
<Directory /var/www/crunchworld>
Options +ExecCGI
AddHandler cgi-script .cgi
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I have cgi enabled and given the necessary permission for the file hello.py but its still showing me internal server error.When i checked the logs i see
End of script output before headers: hello.py
I have researched about the error and give appropriate permissions for the file but it doesnt work.
Any help would be so much appreciated. Thanks in advance.
Further changes i have made.
AddHandler cgi-script .cgi .py
in my crunchworld.conf file.2.I have given permission for the file hello.py
/etc/apache2/conf-available/crunchworld.conf
in /etc/apache2/conf-enabled
4.I had already installed python2.7 on the path /usr/bin/python2.7
and i have also tried using #!/usr/bin/env python
but still it doesnt work.
Upon checking the logs i found End of script output before headers: hello.py, referer: http://localhost/
Thank you for your recommendations but it is still showing 500 internal error.
Upvotes: 1
Views: 2296
Reputation: 5237
Your CGI script must also output header information.
The minimum required is the Content-type
header -- which in this case should be set to text/html
.
Add this to the start of your CGI script (before you print anything else).
print 'Content-type: text/html\n'
Take note of the extra trailing newline -- its necessary to leave at least one blank line between the header(s) and the content itself.
Update:
For further troubleshooting, do the following:
chmod 0755 hello.py
just to be sure..py
whereas your apache config appears to only specify .cgi
files. Your AddHandler
should be AddHandler cgi-script .cgi .py
./etc/apache2/conf-available/crunchworld.conf
file in /etc/apache2/conf-enabled
if you haven't already done so. Do so by running the following: cd /etc/apache2/conf-enabled; sudo ln -s ../conf-available/crunchworld.conf
.sudo service apache2 restart
./usr/bin/python2.7
exist? You can try setting instead to #!/usr/bin/env python
or #!/usr/bin/env python2
. (Or better yet, switch to python3
if possible on your system).tail -20 /var/log/apache2/error.log
(or wherever your logs are).cgitb
module (see https://docs.python.org/3/library/cgitb.html).Upvotes: 1