我在使用django设置wgsi时遇到问题。我正在关注这个http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/。然而,我仍然真的很困惑在哪里放置.wsgi文件,如果我需要设置sys.path。我已经在web root的外部和内部都尝试过了,我无法按预期工作。
# /home/ben/public_html/django_test/testproject/apache/django.wsgi:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Relivant apache conf:
DocumentRoot "/home/ben/public_html/django_test/testproject/"
WSGIScriptAlias / "/home/ben/public_html/django_test/testproject/apache/django.wsgi"
Apache Logs Error(标准apache 500页):
ImportError: Could not import settings 'testproject.settings' (Is it on sys.path? ...
我可以通过使用它来让django至少抛出它自己的错误:
import os
import sys
path = '/home/ben/public_html/django_test/testproject'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
导致了这个django错误页面:
ImportError at /admin/
No module named testproject.urls
答案 0 :(得分:8)
我把wsgi放在与settings.py相同的级别,看起来像这样:
import os
import sys
sys.path.insert(0,os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2]))
os.environ['DJANGO_SETTINGS_MODULE'] = 'yourprojectname.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
这是apache conf文件:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName www.yourprojectname.com
Alias /media/ /home/diegueus9/workspace/yourprojectname/media/
<Directory /home/diegueus9/workspace/yourprojectname/media/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptReloading On
WSGIDaemonProcess yourprojectname
WSGIProcessGroup yourprojectname
WSGIApplicationGroup yourprojectname
WSGIPassAuthorization On
WSGIScriptAlias / /home/diegueus9/workspace/yourprojectname/yourfile.wsgi
ErrorLog /var/log/apache2/yourprojectname-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/yourprojectname-access.log combined
</VirtualHost>
答案 1 :(得分:1)
Here is a nice tutorial向您展示如何将mod_wsgi与Django以及与此问题无关的其他项目一起使用。
答案 2 :(得分:1)
作为快速修复,您可以将其添加到sys.path:
path = '/home/ben/public_html/django_test/testproject'
还添加:
path2 = '/home/ben/public_html/django_test'
您还可以在Apache VirtualHost中更改Python路径:
WSGIDaemonProcess [...] python-path=/home/ben/public_html/django_test
但你也应该复习mod_wsgi文档并了解Graham Dumpleton在那里的建议。