我试图在亚马逊的EC2实例上发布我的网站,并且我一直收到500错误.我真的不知道为什么. //日志文件 [Sun Feb 17 23:12:48.066802 2013] mod_wsgi (pid=2102): Target WSGI script '/srv/www/app/poka/apache/wsgi.py' c
//日志文件
[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配置文件
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)我知道这是一个老线程,但我刚刚碰到同样的问题,我不认为这是由于缺少包裹造成的.由于Django核心发行版已包含正确的wsgi处理程序.
这里的问题是,当执行wsgi.py时,它缺少来自virtualenv的site-packages包. (如果你已经激活了你的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 # ===================
确保:
>您将mod_wsgi添加到Apache模块目录
必须为您拥有的OS,Apache和Python版本编译mod_wsgi
>将load module命令添加到httpd.conf中以加载mod_wsgi模块
LoadModule wsgi_module modules / mod_wsgi.so
>在httpd.conf中配置Django细节或在httpd.conf中包含任何conf
基于文档How to use Django with Apache and 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>
希望这可以帮助.它对我有用.