Django 和 Apache 问题

发布于 2024-09-09 06:56:51 字数 1813 浏览 4 评论 0原文

我正在尝试使用 Mod_wsgi 让 Django 和 Apache 一起工作,目前我收到以下错误:

[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] mod_wsgi (pid=4803): Target WSGI script '/home/webdev/websites/virtualenvs/polaris/polaris_project.py' cannot be loaded as Python module.
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] mod_wsgi (pid=4803): Exception occurred processing WSGI script '/home/webdev/websites/virtualenvs/polaris/polaris_project.py'.
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] Traceback (most recent call last):
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73]   File "/home/webdev/websites/virtualenvs/polaris/polaris_project.py", line 8, in <module>
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73]     import django.core.handlers.wsgi
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] ImportError: No module named django.core.handlers.wsgi

我的 apache conf 看起来像

Alias /polaris_django/media/ "/home/webdev/websites/virtualenvs/polaris/polaris/static/"
WSGIScriptAlias /polaris_django /home/webdev/websites/virtualenvs/polaris/polaris_project.py
WSGIApplicationGroup %{GLOBAL}

<Directory "/home/webdev/websites/virtualenvs/polaris">
        Order deny,allow
        Allow from all
</Directory>

我的 Mod_WSGi 文件看起来像

import os, sys

sys.path.append('/home/webdev/websites/virtualenvs/polaris')
sys.path.append('/home/webdev/websites/virtualenvs/polaris/polaris/apps')
sys.path.append('/home/webdev/websites/virtualenvs/polaris/polaris/extra_settings')
os.environ['DJANGO_SETTINGS_MODULE'] = 'polaris.settings'
print >> sys.stderr, sys.path
import django.core.handlers.wsgi

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

如何让 apache 正确服务 Django?

I'm trying to get Django and Apache working together using Mod_wsgi and currently I'm getting the following errors:

[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] mod_wsgi (pid=4803): Target WSGI script '/home/webdev/websites/virtualenvs/polaris/polaris_project.py' cannot be loaded as Python module.
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] mod_wsgi (pid=4803): Exception occurred processing WSGI script '/home/webdev/websites/virtualenvs/polaris/polaris_project.py'.
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] Traceback (most recent call last):
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73]   File "/home/webdev/websites/virtualenvs/polaris/polaris_project.py", line 8, in <module>
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73]     import django.core.handlers.wsgi
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] ImportError: No module named django.core.handlers.wsgi

My apache conf looks like

Alias /polaris_django/media/ "/home/webdev/websites/virtualenvs/polaris/polaris/static/"
WSGIScriptAlias /polaris_django /home/webdev/websites/virtualenvs/polaris/polaris_project.py
WSGIApplicationGroup %{GLOBAL}

<Directory "/home/webdev/websites/virtualenvs/polaris">
        Order deny,allow
        Allow from all
</Directory>

My Mod_WSGi file looks like

import os, sys

sys.path.append('/home/webdev/websites/virtualenvs/polaris')
sys.path.append('/home/webdev/websites/virtualenvs/polaris/polaris/apps')
sys.path.append('/home/webdev/websites/virtualenvs/polaris/polaris/extra_settings')
os.environ['DJANGO_SETTINGS_MODULE'] = 'polaris.settings'
print >> sys.stderr, sys.path
import django.core.handlers.wsgi

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

How can I get apache serving Django properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷到爆炸 2024-09-16 06:56:51

看起来您正在使用 virtualenv - 您需要在 WSGI 脚本中激活它才能正确设置路径。

activate_this = os.path.join("path/to/my/virtualenv", "bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))

It looks like you're using a virtualenv - you need to activate that within your WSGI script to set up the paths correctly.

activate_this = os.path.join("path/to/my/virtualenv", "bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
流云如水 2024-09-16 06:56:51

我猜测 apache 正在使用不同版本的 python 或者正在使用不同的 sys.path。 sys.path 的输出是什么?

I'm guessing that apache is using either a different version of python or is using a different sys.path. What do you get as output for the sys.path?

浅忆流年 2024-09-16 06:56:51

Django 安装在哪里?从命令行 Python 执行以下操作:

import django
print django.__file__

如果它没有安装在 /usr/lib/python2.6 或 /usr/local/lib/python2.6 下 sys.path 上的适当目录中,那么这就是问题所在。

假设您实际安装了 Django,可能会发生这种情况,因为您安装了多个 Python 版本,并且您使用的版本与 mod_wsgi 用于安装 Django 的版本不同。或者您使用了虚拟环境,但没有告诉 mod_wsgi 它在哪里。或者您设法使用 Apache 无法读取的权限安装 Django。

请观看演讲视频并阅读引用的幻灯片:

http://blog.dscpl.com.au/2010/06/sydney-pycon-modwsgi-talk-slides.html

他们涵盖了此类问题以及与以下内容相关的许多其他内容权限等

Where is Django installed? From command line Python do:

import django
print django.__file__

If it isn't installed in appropriate directory on sys.path under /usr/lib/python2.6 or /usr/local/lib/python2.6, then that is the problem.

Presuming you actually installed Django, this may occur because you have multiple installed Python versions and you used different one to what mod_wsgi is using to install Django. Or you used a virtual environment and haven't told mod_wsgi where that is. Or you managed to install Django with permissions that Apache cannot read.

Go watch the video of talk and read through slides referenced at:

http://blog.dscpl.com.au/2010/06/sydney-pycon-modwsgi-talk-slides.html

They cover this sort of issue as well as a lot of other stuff related to permissions etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文