开发者

sys.path cannot find settings module on apache

开发者 https://www.devze.com 2023-01-29 13:56 出处:网络
I\'m using virtual env with mod_wsgi and django. I set my virtual env at \'/home/project_name\' I cannot get apache to find my settings.py.Any ideas?

I'm using virtual env with mod_wsgi and django.

I set my virtual env at '/home/project_name'

I cannot get apache to find my settings.py. Any ideas?

My wsgi looks like:

import os
import sys
# put the Django project on sys.path
path = '/home/project_name/lib/python2.6/site-packages'
if path not in sys.path:
    sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

and my apache virtual host file looks like:

    WSGIScriptAlias / /home/project_name/releases/current/project_name/wsgi-scripts/project_name.wsgi
    <Direct开发者_Go百科ory /home/project_name/releases/current/project_name/wsgi-scripts>
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog /var/log/apache2/error.log
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined


The path you want to append to sys.path (if it's not there) is the folder ABOVE your django project folder. Instead of rooting all that out to find your specific problem though, I have been using a WSGI setup script that alleviates any path hard-coding like you have and makes things much simpler to setup/deploy. I have a subdirectory called 'apache' under my projects main folder with one file only django.wsgi. As follows...

/djangoproject
    __init__.py
    settings.py
    ...
    /apache
        django.wsgi

In django.wsgi the script below is portable to any other project with the same apache project folder by simply changing the settings module string prefix...

import os
import sys

apache_dir = os.path.dirname(__file__)
project = os.path.dirname(apache_dir)
workspace = os.path.dirname(project)
if workspace not in sys.path:
    sys.path.append(workspace)

os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoproject.settings'
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

Apache vhosts setup as follows...

...

WSGIScriptAlias / /var/www/sitename/djangoproject/apache/django.wsgi
WSGIDaemonProcess djp_wsgi user=myusername group=admin processes=1 threads=10
WSGIProcessGroup djp_wsgi

<Directory /var/www/sitename/djangoproject/apache/>
    Order deny,allow
    Allow from all
</Directory>

...

Hope that helps, I never have any issues with this config. One additional note, the folder name apache and file name django.wsgi can be named pretty much whatever you want, those specific names are just what I chose.


There could be various reasons, mainly related to permissions. Watch this presentation and read slides for the possibilities.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消