一尘不染

ImportError:没有名为django.core.wsgi的模块Apache + VirtualEnv + AWS + WSGI

django

我试图将我的网站发布在Amazon的EC2实例上,但不断出现500错误。我真的不知道为什么。

//日志文件

    [Sun Feb 17 23:12:48.066802 2013] mod_wsgi (pid=2102): Target WSGI script '/srv/www/app/poka/apache/wsgi.py' cannot be loaded as Python module.
    [Sun Feb 17 23:12:48.066840 2013] mod_wsgi (pid=2102): Exception occurred processing WSGI script '/srv/www/app/poka/apache/wsgi.py'.
    [Sun Feb 17 23:12:48.066864 2013] Traceback (most recent call last):
    [Sun Feb 17 23:12:48.066889 2013] File "/srv/www/mysite/poka/apache/wsgi.py", line 26, in <module>
    [Sun Feb 17 23:12:48.066920 2013] from django.core.wsgi import get_wsgi_application
    [Sun Feb 17 23:12:48.066945 2013] ImportError: No module named django.core.wsgi

// Apache Config文件

    WSGIScriptAlias / /srv/www/app/mysite/apache/wsgi.py

    WSGIDaemonProcess mysite python-path=/srv/www/app/mysite:/home/ec2-user/.virtualenvs/mysite-main/lib/python2.7/site-packages
    WSGIProcessGroup mysite

    <Directory /srv/www/app/mysite/apache/>
    <Files wsgi.py>
    Order deny,allow
    Allow from all
    </Files>
    </Directory>

    <Directory /srv/www/app/mysite/apache/>
    Order deny,allow
    Allow from all
    </Directory>

    <Directory /home/ec2-user/app/mysite/static>
    Order deny,allow
    Allow from all
    </Directory>

    <Directory /home/ec2-user/app/mysite/media>
    Order deny,allow
    Allow from all
    </Directory>

//wsgi.py

    import os
    import sys
    import site

    site.addsitedir('/home/ec2-user/.virtualenvs/mysite-main/lib/python2.7/site-packages')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()

    path = '/srv/www/app/mysite'

    if path not in sys.path:
        sys.path.append(path)

阅读 370

收藏
2020-03-29

共1个答案

一尘不染

我知道这是一个旧线程,但是我碰到了同样的问题,我不认为这是由缺少软件包引起的。由于Django核心发行版已经包含正确的wsgi处理程序。

这里的问题是,执行wsgi.py时,它会丢失你virtualenv中的站点软件包。(如果你已经激活了virtualenv,并完成了pip install django的安装,那么一切都很好。你已经有了必要的django软件包)。

就我而言,我修复了修改Path / to / Project / Project / wsgi.py文件中sys.path的问题。

你必须将项目目录和virtualenv站点程序包附加到sys.path列表中。这是我的项目中包含的wsgi.py文件(谈论用django-admin.py start-project创建的wsgi.py)…为了使其与Apache一起使用,我必须对其进行修改。

# =====================
# wsgi.py file begin 

import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('<PATH_TO_MY_DJANGO_PROJECT>/hellodjango')

# add the virtualenv site-packages path to the sys.path
sys.path.append('<PATH_TO_VIRTUALENV>/Lib/site-packages')

# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# wsgi.py file end
# ===================

确保:

  1. 你将mod_wsgi添加到Apache模块目录dir mod_wsgi必须针对你拥有的OS,Apache和Python版本进行编译
  2. 在你的httpd.conf中添加了load module命令以加载mod_wsgi模块LoadModule wsgi_module modules / mod_wsgi.so
  3. 你在httpd.conf或你在httpd.conf中包含的任何conf中配置的Django特定细节

基于文档,如何将Django与Apache和mod_wsgi结合使用

WSGIScriptAlias / <PATH_TO_PROJECT>/hellodjango/hellodjango/wsgi.py
WSGIPythonPath <PATH_TO_PROJECT>:<PATH_TO_VIRTUALENV>/Lib/site-packages

<Directory <PATH_TO_PROJECT>/hellodjango/hellodjango> 
  <Files wsgi.py>
    Order deny,allow
    Require all granted
  </Files>
</Directory>

希望这可以帮助。它为我工作。

2020-03-29