简体   繁体   中英

500 internal Server error when configuring python cgi

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 .

  1. I have added AddHandler cgi-script.cgi.py in my crunchworld.conf file.

2.I have given permission for the file hello.py

  1. I have symlinked /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.

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:

  1. Make sure your CGI script has the correct permissions set: chmod 0755 hello.py just to be sure.
  2. Your script appears to be .py whereas your apache config appears to only specify .cgi files. Your AddHandler should be AddHandler cgi-script.cgi.py .
  3. You should symlink your /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 cd /etc/apache2/conf-enabled; sudo ln -s../conf-available/crunchworld.conf .
  4. Always remember to restart apache if you make any changes to your apache config: eg sudo service apache2 restart .
  5. Check that your hashbang line is correct. Does /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).
  6. Check the apache error logs once again. Eg tail -20 /var/log/apache2/error.log (or wherever your logs are).
  7. You can attempt further debugging with the cgitb module (see https://docs.python.org/3/library/cgitb.html ).

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