ImportError:在 apache 上安装 django mod_wsgi 配置中没有名为 django.core.handlers.wsgi 的模块
我尝试安装 django 以与 apache 和 mod_wsgi 一起使用,但收到此错误:
ImportError: No module named django.core.handlers.wsgi,
我读到这可能是用户错误...
在控制台(ssh)上,使用 root 访问权限,访问 django 没有任何问题。 core.handlers.wsgi ,但是当 apache 要求访问它时,它不能
我的 django.wsgi:
import os
import sys
sys.path.append('my/rep/parents/of/my/project')
sys.path.append('/usr/lib/python2.4/site-packages/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'montest.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
我的 vhost.conf:
Alias /media/ my/rep/parents/of/my/projet/montest/media/
<Directory my/rep/parents/of/my/projet/montest/media>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /montest my/rep/parents/of/my/projet/django.wsgi
<Directory my/rep/parents/of/my/projet>
Order deny,allow
Allow from all
</Directory>
编辑:
好的我的结果 ldd mod_wsgi.so
linux-gate.so.1 => (0x0013c000)
libpython2.6.so.1.0 => /usr/lib/libpython2.6.so.1.0 (0x00663000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00bff000)
libdl.so.2 => /lib/libdl.so.2 (0x0023b000)
libutil.so.1 => /lib/libutil.so.1 (0x00420000)
libm.so.6 => /lib/libm.so.6 (0x00110000)
libc.so.6 => /lib/libc.so.6 (0x00240000) /lib/ld-linux.so.2 (0x0059f000)
所以我决定使用 test.wsgi test.wsgi 测试我的 mod_wsgi 安装
我
def application(environ, start_response):
status = '200 OK'
output = 'Hello world, I am a wsgi app!'
response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
的 vhost.conf:
WSGIScriptAlias /test /var/www/vhosts/mydomain.fr/subdomains/django/httpdocs/test.wsgi
<Directory /var/www/vhosts/mydomain.fr/subdomains/django/httpdocs>
Order allow,deny
Allow from all
Options +ExecCGI
</Directory>
它现在可以工作...接下来我将尝试我的 django.wsgi 配置
I tried to install django to work with apache and mod_wsgi but get this error:
ImportError: No module named django.core.handlers.wsgi,
I'v read that it may be user error...
On the console (ssh), with root access, I don't have any problems accessing django.core.handlers.wsgi , but when apache asks to access it, it can't
My django.wsgi:
import os
import sys
sys.path.append('my/rep/parents/of/my/project')
sys.path.append('/usr/lib/python2.4/site-packages/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'montest.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
My vhost.conf:
Alias /media/ my/rep/parents/of/my/projet/montest/media/
<Directory my/rep/parents/of/my/projet/montest/media>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /montest my/rep/parents/of/my/projet/django.wsgi
<Directory my/rep/parents/of/my/projet>
Order deny,allow
Allow from all
</Directory>
EDIT :
Ok my result for
ldd mod_wsgi.so
linux-gate.so.1 => (0x0013c000)
libpython2.6.so.1.0 => /usr/lib/libpython2.6.so.1.0 (0x00663000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00bff000)
libdl.so.2 => /lib/libdl.so.2 (0x0023b000)
libutil.so.1 => /lib/libutil.so.1 (0x00420000)
libm.so.6 => /lib/libm.so.6 (0x00110000)
libc.so.6 => /lib/libc.so.6 (0x00240000) /lib/ld-linux.so.2 (0x0059f000)
So i decide to test my mod_wsgi install with the test.wsgi
test.wsgi
def application(environ, start_response):
status = '200 OK'
output = 'Hello world, I am a wsgi app!'
response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
my vhost.conf:
WSGIScriptAlias /test /var/www/vhosts/mydomain.fr/subdomains/django/httpdocs/test.wsgi
<Directory /var/www/vhosts/mydomain.fr/subdomains/django/httpdocs>
Order allow,deny
Allow from all
Options +ExecCGI
</Directory>
It now works... next I will try my django.wsgi config
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我通过添加站点包的位置解决了这个问题,我将 django 子目录(/Library/python/2.7/site-packages)保存到 WSGIDaemonProcess:
如果您使用嵌入式服务器模式,请在 httpd.conf 中使用代码>:
I solved the problem by adding location of site-packages, where I have kept django subdirectory (/Library/python/2.7/site-packages) to WSGIDaemonProcess:
If you are using embedded server mode use in
httpd.conf
:为什么你甚至尝试将 site-packages 目录添加到 sys.path 中?如果您的 mod_wsgi 是针对 Python 2.4 编译的,那么它应该已经在 site-packages 目录中查找。听起来你的 mod_wsgi 甚至没有针对 Python 2.4 进行编译。
针对已安装的 mod_wsgi.so 文件运行:
以找出其编译的 Python 版本并发布结果。
Why are you even trying to add the site-packages directory into sys.path? If your mod_wsgi is compiled against Python 2.4, then it should already be looking in the site-packages directory. Sounds like your mod_wsgi isn't even compiled against Python 2.4.
Run:
against your installed mod_wsgi.so file to work out what Python version it is compiled for and post the result.
我以前遇到过这个问题,这是因为 Apache/mod_wsgi 进程没有读取模块的权限。您可以使您的 site-packages/django 目录可供所有人读取,或添加其他适当的用户/组权限。
I've had this issue before, and it was because the Apache/mod_wsgi process did not have permission to read the modules. You can make your site-packages/django directory world-readable, or add other appropriate user/group permissions.
错误:
正确:
我花了太多时间试图找出为什么我的 virtualenv 没有正确加载 django。
Wrong:
Right:
I spent way too much time trying to figure out why my virtualenv wasn't loading django properly.
我通过将保存 django 安装的父目录添加到 wsgi.py 中的 sys.path 解决了这个问题。这是我的设置,FWIW:
/home/banjer/myproject/wsgi.py:
I solved this issue by adding the parent directory that holds my django installation to sys.path in wsgi.py. Here are my settings, FWIW:
/home/banjer/myproject/wsgi.py:
检查您的站点包文件权限。
在修复文件权限之前,上述解决方案都不适合我。
这是我的 ssl_error_log 文件中的内容:
但我在我的服务器上解决了它。如果您可以在命令行上执行此操作,那么此解决方案适合您:
有效的是我 chmod go+rx site-packages libpython*
(这可能有点过头了,但它对我有用。)
我以 httpd 作为 apache.user 运行,并以 root 身份运行 python,可以很好地看到软件包,但我的权限设置不正确(每个人都可以阅读) ,这就是为什么 httpd 无法读取包的原因。
Check your site-package file permissions.
None of the above solutions worked for me until I fix file permissions.
Here's what's in my ssl_error_log file:
But I solved it on my server. If you can do this on the command line, then this solution is for you:
What worked is that I chmod go+rx site-packages libpython*
(this might be overkill, but it worked for me.)
I'm running as httpd as apache.user, and running python as root, could see the packages just fine, but my permissions were not setup correctly (to read by everyone), and that's why httpd could not read the packages.
这行肯定是错误的:
Install Django with/for the version of Python that mod_wsgi isbuilt against。
This line is certainly wrong:
Install Django with/for the version of Python that mod_wsgi was built against.
我知道这是一个有点老的问题,但我想我会为未来可能会发现这个问题的用户插话:
您的 mod_wsgi 链接到 python2.6,但您正在使用 python 2.4 根据您的配置运行 django ?
我假设你的 /usr/bin/python 指向的是 mod_wsgi 编译所针对的 2.6 以外的版本。这也可能是由于您正在针对 2.4 运行 django。当 django 使用 python2.7 时,当我加载与 python2.6 链接的 mod_wsgi 时,我收到了相同的错误。使用我安装的 mod_wsgi 版本 - 它支持 python2.[6-7],所以我所要做的就是删除 /usr/lib/apache2/modules/ 中 mod_wsgi.so 的符号链接 -> mod_wsgi.so-2.6 并将其更改为 mod_wsgi.so -> mod_wsgi.so-2.7。
很容易。
I know this is a somewhat old question, but I thought I'd chime in for future SO users who might find this question:
Your mod_wsgi is linked to python2.6, yet you're using python 2.4 to run django according to your config?
I'm going to assume your /usr/bin/python is pointing to something other than the 2.6 which is what mod_wsgi is compiled against. It might also be due to the fact that you're running django against 2.4. I received the same error when I was loading mod_wsgi linked against python2.6 when django was using python2.7. With the version of mod_wsgi I have installed - it came with support for both python2.[6-7], so all I had to do was remove the symlink in /usr/lib/apache2/modules/ for mod_wsgi.so -> mod_wsgi.so-2.6 and change it to mod_wsgi.so -> mod_wsgi.so-2.7.
Easy enough.
如果你从旧的 python 库中删除 django
,然后将其重新安装到你正在使用的当前 python 的“site-packages”文件夹中,这将是一个更好的主意:
这就是我所做的,我没有收到该错误不再了!
It would be a better idea if you remove django from your old python library..
..and reinstall it inside the 'site-packages' folder of the current python you are using:
That's what i did and i don't get that error anymore!
你好!
如果您使用 Linux(Debian、Ubuntu 等)的 deb-distributive,请编辑文件
此文件包含正确的 wsgi-library 路径(适用于活动版本的 Python 解释器)。如果您使用Python2.6,将字符串更改
为
Also, you can change soft link to mod_wsgi module:
不要忘记更改 /etc/apache2/modules/wsgi.load 中文件的链接并重新启动 apache
服务器抱歉我的英语不好
Hello!
If you use deb-distributive of Linux (Debian, Ubuntu, etc), edit the file
This file contained path to correct wsgi-library (for active version Python interpreter). If you use Python2.6, change string
to
Also, you can change soft link to mod_wsgi module:
Not forget change link to file in /etc/apache2/modules/wsgi.load and restart apache server
P.S. Sorry for my bad English