How to fix Django FastCGI Internal Server Error (500)
You can mention this error in log files as follows:
[Fri Sep 28 00:43:49 2007] [error] [client 85.141.235.249] FastCGI: comm with (dynamic) server "/storage/home/username/domain.ru/dispatch.fcgi" aborted: (first read) idle timeout (30 sec)
[Fri Sep 28 00:43:49 2007] [error] [client 85.141.235.249] FastCGI: incomplete headers (0 bytes) received from server "/storage/home/username/domain.ru/dispatch.fcgi"
One of the reason of such error is incorrect path to Python. You can find a correct path to Python using command 'whereis':
$ whereis python
python: /usr/local/bin/python /usr/local/man/man1/python.1.gz /usr/ports/lang/python
Then you should check contents of the file ~/domain.ru/dispath.fcgi, the first string should look like this #!/usr/local/bin/python
There are few peculiarities when you are using Django on virtual hosting and FastCGI. For example we create our new website using the official documentation, we create Polls model, save everything, then we open our brand new website and we see a many-coloured purple-blue page with an error:
TemplateDoesNotExist
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
As I can understand the reason of the following error is that there are few FastCGI threads working at the same time, one thread have old code version cached, while another thread has a new version of code cached. That is why this error appears from time to time. I saw an advice on Dreamhost wiki for such case, you should kill python with command pkill python, but it does not help for me, this command did not worked. Probably the reason was that fcgi, python work from user www-data while I was logged in with another username and I unable to kill those processes. There is a solution for such problem:
- create a script in cgi-bin directory with the following content
#!/bin/sh killall python
- set appropriate permissions ($chmod 750 kill_python2ADF23x.sh)
- open the page from a web browser with this script http://www.domain.ru/cgi-bin/kill_python2ADF23x.sh Strange filename for such script created especially, so no one would run this this script (except people who should know about this script).
- after we run script from browser, we should remove all python byte-compiled files and renew timestamp for a dispatch.fcgi file using following commands:
cd ~/django_projects/ $ find ./ -name *.pyc -type f -exec rm {} ; $ find ./ -name *.pyo -type f -exec rm {} ; $ touch ~/domain.ru/dispatch.fcgi ~/build/flup-0.5/flup/server/fcgi.py