【发布时间】:2014-08-07 23:06:00
【问题描述】:
我有一个安装了 Apache2、mod_wsgi 和 Django 的 EC2 服务器。服务器同时安装了 python 2.7 和 3.4,随服务器 os Ubuntu 14.04 一起提供。
我有正确配置的 virtualenvwrapper 指向 Python 3.4,这是用于网络服务器的目标 python versino。
问题是尝试访问我的 django 项目时,浏览器显示 500 internal server error。错误日志还表明正在使用 Python 2.7 而不是 3.4
Django 项目 wsgi.py
导入系统
导入操作系统
导入网站
site.addsitedir('home/userid/.virtualenvs/myproject/lib/python3.4/site-packages')
sys.path.append('/home/userid/myproject/myproject')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
# 激活你的虚拟环境
activate_env=os.path.expanduser("~/.virtualenvs/myproject/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
#with open(activate_env) as f:
# 代码 = 编译 (f.read(), activate_env, 'exec')
# exec(code, dict(__file__=activate_env))
从 django.core.wsgi 导入 get_wsgi_application
application = get_wsgi_application()
/etc/apache2/sites-enabled/000-default.conf:
WSGIScriptAlias /domain1 /home/userid/myproject/myproject/myproject/wsgi.py
WSGIDaemonProcess domain1 user=userid
<Directory /home/userid/myproject/myproject/myproject>
WSGIProcessGroup domain1
Options All
AllowOverride All
Require all granted
</Directory>
输出:(env1) $ ~/myproject/myproject$ pip3 freeze
Django==1.6.5
djangorestframework==2.3.13
apache2错误日志:
[Tue Jun 17 19:16:34.990478 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] mod_wsgi (pid=789): 处理 WSGI 脚本 '/home/userid /myproject/myproject/myproject/wsgi.py'。
[Tue Jun 17 19:16:34.990567 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] Traceback(最近一次通话最后):
[2014 年 6 月 17 日星期二 19:16:34.990601] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/core /handlers/wsgi.py",第 206 行,在 调用
[2014 年 6 月 17 日星期二 19:16:34.990650] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] response = self.get_response(request)
[2014 年 6 月 17 日星期二 19:16:34.990674] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/core /handlers/base.py”,第 194 行,在 get_response
[Tue Jun 17 19:16:34.990711 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
[2014 年 6 月 17 日星期二 19:16:34.990734] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/core /handlers/base.py",第 229 行,在 handle_uncaught_exception
[2014 年 6 月 17 日星期二 19:16:34.990769] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 返回 debug.technical_500_response(request, *exc_info) [2014 年 6 月 17 日星期二 19:16:34.990792] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/views /debug.py”,第 69 行,位于 Technical_500_response
[2014 年 6 月 17 日星期二 19:16:34.990827] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] html = report.get_traceback_html()
[2014 年 6 月 17 日星期二 19:16:34.990850] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/views /debug.py”,第 324 行,在 get_traceback_html
[2014 年 6 月 17 日星期二 19:16:34.990883] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 返回 t.render(c)
[2014 年 6 月 17 日星期二 19:16:34.990906] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/template /base.py",第 140 行,在渲染中
[2014 年 6 月 17 日星期二 19:16:34.990941] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 返回 self._render(context)
[2014 年 6 月 17 日星期二 19:16:34.990963] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/template /base.py”,第 134 行,在 _render
[2014 年 6 月 17 日星期二 19:16:34.991018] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 返回 self.nodelist.render(context)
[2014 年 6 月 17 日星期二 19:16:34.991042] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/template /base.py",第 840 行,在渲染中
[2014 年 6 月 17 日星期二 19:16:34.991076] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] bit = self.render_node(node, context)
[2014 年 6 月 17 日星期二 19:16:34.991099] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/template /debug.py”,第 78 行,在 render_node
[2014 年 6 月 17 日星期二 19:16:34.991133] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 返回 node.render(context) [2014 年 6 月 17 日星期二 19:16:34.991155] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/template /debug.py",第 88 行,在渲染中
[2014 年 6 月 17 日星期二 19:16:34.991189] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 输出 = self.filter_expression.resolve(context) [2014 年 6 月 17 日星期二 19:16:34.991211] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/template /base.py",第 613 行,解决
[2014 年 6 月 17 日星期二 19:16:34.991245] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] new_obj = func(obj, *arg_vals)
[2014 年 6 月 17 日星期二 19:16:34.991267] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/template /defaultfilters.py",第 705 行,日期
[2014 年 6 月 17 日星期二 19:16:34.991301] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 返回格式(值,arg)
[2014 年 6 月 17 日星期二 19:16:34.991323] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /dateformat.py",第 312 行,格式
[2014 年 6 月 17 日星期二 19:16:34.991357] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 返回 df.format(format_string)
[2014 年 6 月 17 日星期二 19:16:34.991380] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /dateformat.py",第 35 行,格式
[2014 年 6 月 17 日星期二 19:16:34.991413] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049]pieces.append(force_text(getattr(self,piece)())) [2014 年 6 月 17 日星期二 19:16:34.991435] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /dateformat.py",第 216 行,在 r
[Tue Jun 17 19:16:34.991469 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return self.format('D, j M Y H:i:s O') [2014 年 6 月 17 日星期二 19:16:34.991491] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /dateformat.py",第 35 行,格式
[2014 年 6 月 17 日星期二 19:16:34.991523] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049]pieces.append(force_text(getattr(self,piece)())) [2014 年 6 月 17 日星期二 19:16:34.991546] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /encoding.py",第 100 行,在 force_text
[2014 年 6 月 17 日星期二 19:16:34.991580] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] s = s.__unicode__() [2014 年 6 月 17 日星期二 19:16:34.991602] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /functional.py",第 138 行,在 __text_cast
[Tue Jun 17 19:16:34.991635 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return func(*self.__args, **self.__kw)
[2014 年 6 月 17 日星期二 19:16:34.991658] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /translation/__init__.py",第 76 行,在 ugettext
[2014 年 6 月 17 日星期二 19:16:34.991705] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 返回 _trans.ugettext(message)
[2014 年 6 月 17 日星期二 19:16:34.991729] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /translation/trans_real.py",第 281 行,在 ugettext
[2014 年 6 月 17 日星期二 19:16:34.991764] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 返回 do_translate(message, 'ugettext')
[2014 年 6 月 17 日星期二 19:16:34.991787] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /translation/trans_real.py”,第 263 行,在 do_translate
[2014 年 6 月 17 日星期二 19:16:34.991820] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] _default = translation(settings.LANGUAGE_CODE)
[2014 年 6 月 17 日星期二 19:16:34.991843] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /translation/trans_real.py”,第 177 行,翻译中
[2014 年 6 月 17 日星期二 19:16:34.991877] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] default_translation = _fetch(settings.LANGUAGE_CODE) [2014 年 6 月 17 日星期二 19:16:34.991899] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /translation/trans_real.py”,第 159 行,在 _fetch
[2014 年 6 月 17 日星期二 19:16:34.991932] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] app = import_module(appname)
[2014 年 6 月 17 日星期二 19:16:34.991955] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] 文件“/usr/local/lib/python2.7/dist-packages/django/utils /importlib.py",第 40 行,在 import_module
[2014 年 6 月 17 日星期二 19:16:34.991988] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] /import(名称)
[2014 年 6 月 17 日星期二 19:16:34.992020] [:error] [pid 789:tid 139997025199872] [远程 205.250.126.130:28049] ImportError: 没有名为 rest_framework 的模块
这是我第一次尝试使用这些工具设置服务器,所以即使在互联网上搜索并在线查看不同的教程后,我也不确定问题出在哪里。
更新: 在设置 WSGIPythonHome(感谢yuvl)后,我发现我的 mod_wsgi 可能配置为 python 2.7。
[2014 年 6 月 18 日星期三 05:08:31.960637] [mpm_event:notice] [pid 1828:tid 140269602740096] AH00491:捕获 SIGTERM,正在关闭
[2014 年 6 月 18 日星期三 05:08:33.243412] [mpm_event:notice] [pid 2168:tid 140466175211392] AH00489:Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 已配置 -- 恢复正常操作
[2014 年 6 月 18 日星期三 05:08:33.243753] [core:notice] [pid 2168:tid 140466175211392] AH00094:命令行:'/usr/sbin/apache2'
ImportError: 没有名为站点的模块
所以我继续使用重新安装 mod_wsgi
sudo apt-get build-dep libapache2-mod-wsgi
sudo a2enmod wsgi
但是现在说apache无法加载wsgi:
$ sudo /etc/init.d/apache2 重启 * 重启 web 服务器 apache2 [失败]
* apache2 配置测试失败。
配置测试的输出是:
apache2:/etc/apache2/apache2.conf 第 140 行的语法错误:/etc/apache2/mods-enabled/wsgi.load 第 1 行的语法错误:无法加载 /usr/lib/apache2/modules/mod_wsgi.so进入服务器:/usr/lib/apache2/modules/mod_wsgi.so:无法打开共享对象文件:没有这样的文件或目录
操作“configtest”失败。
Apache 错误日志可能包含更多信息。
Apache 错误日志:
$ tail -f /var/log/apache2/error.log
ImportError:没有名为站点的模块
ImportError:没有名为站点的模块
ImportError:没有名为站点的模块
ImportError:没有名为站点的模块
ImportError:没有名为站点的模块
ImportError:没有名为站点的模块
ImportError:没有名为站点的模块
ImportError:没有名为站点的模块
ImportError:没有名为站点的模块
[2014 年 6 月 18 日星期三 05:25:14.759140] [mpm_event:notice] [pid 2168:tid 140466175211392] AH00491:捕获 SIGTERM,正在关闭
有什么建议可以解决这个问题吗?
发布更新后,我还尝试按照this tutorial 和this tutorial 重新安装mod_wsgi,但我仍然遇到同样的失败。 Apache 不知道 mod_wsgi。
提前感谢您的帮助!
【问题讨论】:
-
我不确定这是否有帮助,但您似乎忘记在路线开头添加斜线
site.addsitedir('home/userid/.virtualenvs/myproject/lib/python3.4/site-packages')尝试为此行更改它:site.addsitedir('/home/userid/.virtualenvs/myproject/lib/python3.4/site-packages') -
感谢您指出:)
-
您能否也分享一下您的 Apache 错误日志的内容?正如消息中所说,它可能包含更多信息
-
我用错误日志更新了我的问题。请看一下。谢谢!