Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

500 Error With WSGI in Django

Tags:

django

wsgi

I'm deploying my first ever Django project and I get the feeling I'm very close, but just need some help getting over the line. Here's the problem:

My httpd.conf changes look like this:

WSGIScriptAlias / /home/miketnc/frontend/tncsite/wsgi.py
WSGIPythonPath /home/miketnc/frontend/tncsite

<Directory /home/miketnc/frontend/tncsite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

So far so good, the "hello world" script in wsgi.py runs just fine. The problem comes when I use the Django doc-recommended wsgi script:

import os, sys

sys.path.append('/home/miketnc/frontend/tncsite')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tncsite.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

This causes a 500 error:

File "/home/miketnc/frontend/tncsite/wsgi.py", line 10, in ?
mod_wsgi (pid=15494): Exception occurred processing WSGI script '/home/miketnc/frontend/tncsite/wsgi.py'.
mod_wsgi (pid=15494): Target WSGI script '/home/miketnc/frontend/tncsite/wsgi.py' cannot be loaded as Python module.

All of the support I've seen on the error relates back to bad installations in apache, not finding modules etc, which can't be the case if "hello world" is running.

Any ideas?

UPDATE

After restarting on a fresh server, I've managed to move things forward slightly. The good news is that python and WSGI seem to be playing nice, the bad is that I'm now getting a different kind of 500 error.

The only error I'm getting back in the log is: "[Mon Dec 05 18:22:45 2011] [error] [client ip] mod_wsgi (pid=19804): Exception occurred processing WSGI script '/home/miketnc/frontend/tncsite/apache/wsgi.py'."

The Hello World script still runs fine, the trigger for the error is the final line:

application = django.core.handlers.wsgi.WSGIHandler()

I've set all directories containing the project from frontend/ down to 777 and I've added a daemon process as myself:

LoadModule wsgi_module /usr/local/apache/extramodules/mod_wsgi.so
AddHandler wsgi-script .wsgi

WSGIDaemonProcess miketnc processes=2 maximum-requests=500 threads=1
WSGIProcessGroup miketnc

WSGIScriptAlias / /home/miketnc/frontend/tncsite/apache/wsgi.py

<Directory /home/miketnc/frontend/tncsite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

Anyone please able to advise further on how to get this working?

like image 643
Mike Avatar asked Feb 23 '23 06:02

Mike


2 Answers

In your case:

WSGIPythonPath /home/miketnc/frontend/tncsite

is redundant, as your are setting sys.path in the WSGI script file.

What you are missing though is adding the parent directory of the site:

sys.path.append('/home/miketnc/frontend')

This is in addition to the existing line adding '/home/miketnc/frontend/tncsite'.

Read:

http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

and watch:

http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations

which talk about paths and permissions.

like image 112
Graham Dumpleton Avatar answered Feb 24 '23 18:02

Graham Dumpleton


Solved.

I didn't realise that the error log in Cpanel is a tiny fraction of the error log Apache outputs. Once I viewed the Apache logs, the problem was obvious. In this case it was MySQLdb not being set up properly.

I worked through it, making some adjustments around .python-eggs, and all is now well.

Thanks for the responses and in particular thank you Graham for the work you've put into WSGI.

like image 27
Mike Avatar answered Feb 24 '23 18:02

Mike