1

I have set up mode_wsgi on Apache and it works fine but when I try to deploy Django on it i get 500 Internal server error. and the following is the trace I get from Apache error logs

[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1] mod_wsgi (pid=8212): Target WSGI          script 'H:/DEV/python/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1] mod_wsgi (pid=8212): Exception occurred processing WSGI script 'H:/DEV/python/mysite/mysite/wsgi.py'.
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "H:/DEV/python/mysite/mysite/wsgi.py", line 13, in <module>
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     from django.core.wsgi import get_wsgi_application
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\core\\wsgi.py", line 1, in <module>
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     from django.core.handlers.wsgi import WSGIHandler
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\wsgi.py", line 11, in <module>
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     from django.core.handlers import base
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\core\\handlers\\base.py", line 12, in <module>
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     from django.db import connections, transaction
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\db\\__init__.py", line 83, in <module>
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     signals.request_started.connect(reset_queries)
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\dispatch\\dispatcher.py", line 88, in connect
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     if settings.DEBUG:
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\conf\\__init__.py", line 54, in __getattr__
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     self._setup(name)
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\conf\\__init__.py", line 49, in _setup
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     self._wrapped = Settings(settings_module)
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]   File "C:\\Python27\\lib\\site-packages\\django\\conf\\__init__.py", line 132, in __init__
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1]     % (self.SETTINGS_MODULE, e)
[Thu Nov 28 12:14:40 2013] [error] [client 127.0.0.1] ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named mysite.settings

Any help on this will be greatly appreciated. Thanks

Here are the apache conf for the site

WSGIScriptAlias / "H:/DEV/python/mysite/mysite/wsgi.py"
WSGIPythonPath "H:/DEV/python/mysite:C:/Python27/Lib/site-packages"

<Directory "H:/DEV/python/mysite">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

and this is the code in the wsgi file

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
juliocesar
  • 5,706
  • 8
  • 44
  • 63
lemaiyan
  • 13
  • 1
  • 5

3 Answers3

2

Add this to wsgi script:

import sys
sys.path.append('H:/DEV/python/mysite')

Put it before the os.environ part

yuvi
  • 18,155
  • 8
  • 56
  • 93
  • Sure thing. Note that juliocesar answer, while not the answer to your current problem, is still correct, you should change your `directory` tag as he suggested – yuvi Nov 28 '13 at 09:51
  • @yuvi Sorry but I think you are wrong, actually I have my project without sys.path, since on apache config you define `WSGIPythonPath` as the expample: `WSGIPythonPath "H:/DEV/python/mysite:C:/Python27/Lib/site-packages"` you don't need to specify sys.path – juliocesar Nov 28 '13 at 10:03
  • From my experience, when the project was moved (and it usually is when moving to a production server) you need to specify it literally . So a project can definitely exist without sys.path, but sometimes you do need it. – yuvi Nov 28 '13 at 10:36
  • @yuvi The django docs don't talks about this practice (which you post in your answer), and also you didn't founds a bit strange that the autogenerated wsgi.py file don't add the path? so if it need (at least in some situations) then is a django bug? but I found this https://code.djangoproject.com/ticket/18496 – juliocesar Nov 28 '13 at 10:45
  • so even when this should solve the problem this is not the best solution ;) – juliocesar Nov 28 '13 at 10:51
  • Yeah there's not much mention of it but like I said, this is based on experience. I actually think it's better this way. Explicit is better than implicit – yuvi Nov 28 '13 at 11:54
  • @yuvi I edited my answer with real problem description and the solution, thanks you I finally understand the problem ;) – juliocesar Nov 28 '13 at 18:35
  • Interesting. I'll test it out after the weekend – yuvi Nov 28 '13 at 20:51
1

I think I finally find the real problem after a bit search:

WSGIPythonPath directive in apache config is wrong, you need to change the : by ; since you are on windows, so it must looks like:

WSGIPythonPath "H:/DEV/python/mysite;C:/Python27/Lib/site-packages"

That's why your wsgi didn't found your site settings, this is the correct way to handle this. With this fix you don't need to modify the auto-generated wsgi file to add your site in the sys.path (as suggeted @yuvi in his answer) because it is redundant (add the sys.path is what WSGIPythonPath do) and not a best practice.

(More on wsgi docs)

Also you should change this:

<Directory "H:/DEV/python/mysite">

by:

<Directory "H:/DEV/python/mysite/mysite">
juliocesar
  • 5,706
  • 8
  • 44
  • 63
0

I just met same problem. edit apache conf file 'http.conf' and Add your site packapge path to WSGIPythonPath may be helpful. Like follows:

    WSGIPythonPath "c:/Users/mysite;c:/Users/yoursite"

to see more details on django docs

In particular, the path separator between directory names on Windows is ; and not : as on UNIX systems.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
J.C
  • 233
  • 3
  • 7