用Django 1.6.5配置Apache服务器
我有一台安装了Apache2、mod_wsgi和Django的EC2服务器。这个服务器上同时安装了Python 2.7和3.4,操作系统是Ubuntu 14.04。
我已经正确配置了virtualenvwrapper,让它指向Python 3.4,这是我想在web服务器上使用的Python版本。
问题是,当我尝试访问我的Django项目时,浏览器显示500内部服务器错误。错误日志也表明它正在使用Python 2.7,而不是3.4。
这是我的Django项目的wsgi.py文件:
import sys
import os
import site
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:
# code = compile (f.read(), activate_env, 'exec')
# exec(code, dict(__file__=activate_env))
from django.core.wsgi import 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] 回溯(最近的调用在最后):
[Tue Jun 17 19:16:34.990601 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py",第206行,在 call
[Tue Jun 17 19:16:34.990650 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] response = self.get_response(request)
[Tue Jun 17 19:16:34.990674 2014] [:error] [pid 789:tid 139997025199872] [remote 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())
[Tue Jun 17 19:16:34.990734 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py",第229行,在 handle_uncaught_exception
[Tue Jun 17 19:16:34.990769 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return debug.technical_500_response(request, *exc_info) [Tue Jun 17 19:16:34.990792 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/views/debug.py",第69行,在 technical_500_response
[Tue Jun 17 19:16:34.990827 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] html = reporter.get_traceback_html()
[Tue Jun 17 19:16:34.990850 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/views/debug.py",第324行,在 get_traceback_html
[Tue Jun 17 19:16:34.990883 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return t.render(c)
[Tue Jun 17 19:16:34.990906 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/template/base.py",第140行,在 render
[Tue Jun 17 19:16:34.990941 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return self._render(context)
[Tue Jun 17 19:16:34.990963 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/template/base.py",第134行,在 _render
[Tue Jun 17 19:16:34.991018 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return self.nodelist.render(context)
[Tue Jun 17 19:16:34.991042 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/template/base.py",第840行,在 render
[Tue Jun 17 19:16:34.991076 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] bit = self.render_node(node, context)
[Tue Jun 17 19:16:34.991099 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/template/debug.py",第78行,在 render_node
[Tue Jun 17 19:16:34.991133 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return node.render(context) [Tue Jun 17 19:16:34.991155 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/template/debug.py",第88行,在 render
[Tue Jun 17 19:16:34.991189 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] output = self.filter_expression.resolve(context) [Tue Jun 17 19:16:34.991211 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/template/base.py",第613行,在 resolve
[Tue Jun 17 19:16:34.991245 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] new_obj = func(obj, *arg_vals)
[Tue Jun 17 19:16:34.991267 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py",第705行,在 date
[Tue Jun 17 19:16:34.991301 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return format(value, arg)
[Tue Jun 17 19:16:34.991323 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py",第312行,在 format
[Tue Jun 17 19:16:34.991357 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return df.format(format_string)
[Tue Jun 17 19:16:34.991380 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py",第35行,在 format
[Tue Jun 17 19:16:34.991413 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] pieces.append(force_text(getattr(self, piece)())) [Tue Jun 17 19:16:34.991435 2014] [:error] [pid 789:tid 139997025199872] [remote 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') [Tue Jun 17 19:16:34.991491 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py",第35行,在 format
[Tue Jun 17 19:16:34.991523 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] pieces.append(force_text(getattr(self, piece)())) [Tue Jun 17 19:16:34.991546 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py",第100行,在 force_text
[Tue Jun 17 19:16:34.991580 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] s = s.__unicode__() [Tue Jun 17 19:16:34.991602 2014] [:error] [pid 789:tid 139997025199872] [remote 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)
[Tue Jun 17 19:16:34.991658 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py",第76行,在 ugettext
[Tue Jun 17 19:16:34.991705 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return _trans.ugettext(message)
[Tue Jun 17 19:16:34.991729 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py",第281行,在 ugettext
[Tue Jun 17 19:16:34.991764 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return do_translate(message, 'ugettext')
[Tue Jun 17 19:16:34.991787 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py",第263行,在 do_translate
[Tue Jun 17 19:16:34.991820 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] _default = translation(settings.LANGUAGE_CODE)
[Tue Jun 17 19:16:34.991843 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py",第177行,在 translation
[Tue Jun 17 19:16:34.991877 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] default_translation = _fetch(settings.LANGUAGE_CODE)
[Tue Jun 17 19:16:34.991899 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py",第159行,在 _fetch
[Tue Jun 17 19:16:34.991932 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] app = import_module(appname)
[Tue Jun 17 19:16:34.991955 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] 文件"/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py",第40行,在 import_module
[Tue Jun 17 19:16:34.991988 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] /import(name)
[Tue Jun 17 19:16:34.992020 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] ImportError: 没有名为rest_framework的模块
这是我第一次尝试使用这些工具搭建服务器,所以即使在网上搜索和查看不同的教程后,我也不确定问题出在哪里。
更新: 在设置WSGIPythonHome后(感谢 yuvl),我发现我的mod_wsgi可能配置为Python 2.7。
[Wed Jun 18 05:08:31.960637 2014] [mpm_event:notice] [pid 1828:tid 140269602740096] AH00491: 捕获到SIGTERM,正在关闭
[Wed Jun 18 05:08:33.243412 2014] [mpm_event:notice] [pid 2168:tid 140466175211392] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6已配置 -- 恢复正常操作
[Wed Jun 18 05:08:33.243753 2014] [core:notice] [pid 2168:tid 140466175211392] AH00094: 命令行: '/usr/sbin/apache2'
ImportError: 没有名为site的模块
所以我重新安装了mod_wsgi,使用以下命令:
sudo apt-get build-dep libapache2-mod-wsgi
sudo a2enmod wsgi
但现在它显示Apache无法加载wsgi:
$ sudo /etc/init.d/apache2 restart * 正在重启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: 没有名为site的模块
ImportError: 没有名为site的模块
ImportError: 没有名为site的模块
ImportError: 没有名为site的模块
ImportError: 没有名为site的模块
ImportError: 没有名为site的模块
ImportError: 没有名为site的模块
ImportError: 没有名为site的模块
ImportError: 没有名为site的模块
[Wed Jun 18 05:25:14.759140 2014] [mpm_event:notice] [pid 2168:tid 140466175211392] AH00491: 捕获到SIGTERM,正在关闭
有没有什么建议可以帮助我解决这个问题?
在我发布更新后,我还尝试按照 这个教程 和 这个教程 重新安装mod_wsgi,但我仍然遇到同样的失败。Apache不知道mod_wsgi。
提前感谢你的帮助!
2 个回答
在我的情况下,我只是手动停止和启动我的apache2,所有东西都能正常工作。
但如果想要一个永久的解决办法,
可以参考这个链接:Apache服务器一直崩溃,“捕获到SIGTERM,正在关闭”,
然后根据下面的链接,编辑你的deploy.sh文件中的一些内容。
http://httpd.apache.org/docs/2.2/stopping.html
如果你没有使用系统自带的Python版本,那么你也需要告诉Apache这个信息。只需设置一下 WSGIPythonHome 指令,像这样:
WSGIPythonHome /path/to/virtualenv
另外,如果这样还是不行,确保你的mod_wsgi是针对Python 3.4编译的,而不是2.7(而且,最好使用 3.5或更新的版本,主要是出于安全考虑)