django+mod_wsgi on virtualenv not working

2020-07-10 08:57发布

I've just finished setting up a django app on virtualenv, deployment went smoothly using a fabric script, but now the .wsgi is not working, I've tried every variation on the internet but no luck. My .wsgi file is:

import os
import sys
import django.core.handlers.wsgi

# put the Django project on sys.path
root_path = os.path.abspath(os.path.dirname(__file__) + '../')
sys.path.insert(0, os.path.join(root_path, 'kcdf'))
sys.path.insert(0, root_path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'kcdf.settings'

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

I keep getting the same error:

[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159] mod_wsgi (pid=16938): Exception occurred processing WSGI script '/home/kcdfweb/webapps/kcdf.web/releases/current/kcdf/apache/kcdf.wsgi'.
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159] Traceback (most recent call last):
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 230, in __call__
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     self.load_middleware()
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 33, in load_middleware
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py", line 269, in __getattr__
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     self._setup()
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 40, in _setup
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     self._wrapped = Settings(settings_module)
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 75, in __init__
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159] ImportError: Could not import settings 'kcdf.settings' (Is it on sys.path? Does it have syntax errors?): No module named kcdf.settings

my virtual environment is on /home/user/webapps/kcdfweb my app is /home/user/webapps/kcdf.web/releases/current/project_name my wsgi file home/user/webapps/kcdf.web/releases/current/project_name/apache/project_name.wsgi

4条回答
ゆ 、 Hurt°
2楼-- · 2020-07-10 09:07

Do you have an __init__.py file in your "kcdf" directory? Without that your settings file cannot be imported.

Also, you should be calling site.addsitedir() on the virtualenv's site-packages directory, if you expect to be able to import stuff from the virtualenv. See the mod_wsgi docs for details. Though if it can't even import your settings, I don't think this is your current problem.

查看更多
Animai°情兽
3楼-- · 2020-07-10 09:15

I'd recommend that you look at the docs for using Virtualenv with mod_wsgi. They offer a few alternative approaches for hooking into your virtualenv which might work better for you.

查看更多
Fickle 薄情
4楼-- · 2020-07-10 09:15

If you're using a virtualenv, you'll need to activate it within the WSGI script to set the paths up correctly.

root_path = os.path.abspath(os.path.dirname(__file__) + '../')
activate_this = os.path.join(root_path, "bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
查看更多
Anthone
5楼-- · 2020-07-10 09:17

You need to add the directory that is two up from your wsgi file, so instead of:

root_path = os.path.abspath(os.path.dirname(__file__) + '../')

you should have

root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../', '../'))

...as your wsgi file is in a directory called apache, under your project folder.

查看更多
登录 后发表回答