1

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.

1 Answers1

1

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.
  4. Always remember to restart apache if you make any changes to your apache config: e.g. 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. E.g. 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).
costaparas
  • 5,047
  • 11
  • 16
  • 26
  • i have added it but it doesnt solved the issue ..Still its showing internal server error! – Avinash Babu Dec 19 '20 at 13:54
  • @avinash-babu I've added some further debugging steps in my answer which may assist you. If your problem persists, please [edit](https://stackoverflow.com/posts/65368312/edit) your question with details on the steps you carried out, and the exact log information or errors you found. Good luck! – costaparas Dec 20 '20 at 00:39
  • Thank you for your help so far.But its still showing the same error from localhost..I have edited my question as you said.. – Avinash Babu Dec 20 '20 at 08:04
  • Does it work when you run it on the command-line directly: `./hello.py`? Also, what does `ls -ld . hello.py` show? – costaparas Dec 21 '20 at 06:13
  • -rwxr-xr-x 1 avinash avinash 264 Dec 20 13:35 hello.py (result of ls -ld hello.py) – Avinash Babu Dec 21 '20 at 13:26
  • And there are no other error lines in the apache log? Also, apache will run it like this: `./hello.py`, so check that it works by running it like that with only `./` in front -- which will use the interpreter in the hashbag line. – costaparas Dec 21 '20 at 13:36
  • when i run /hello.py it gives me /usr/bin/env: ‘python’: No such file or directory..I run python scripts as python2.7 .. I guess this might be the problem..What should i do in this case to make it work ??..Should i install python on /usr/bin/env ?..Thanx for your help so far – Avinash Babu Dec 21 '20 at 15:22
  • I don't know where your Python is installed on your system; it could be in `/usr/local/bin` instead. Run `which python2` and `which python2.7` to get a suitable (Python 2) version. – costaparas Dec 21 '20 at 22:32
  • I have already tried that before and added the path (/usr/bin/python2.7) to the hello,py file as #!/usr/bin/python2.7..but still it doesnt work – Avinash Babu Dec 22 '20 at 16:04