【问题标题】:Django EC2 mod_wsgi configuration with Apache not working使用 Apache 的 Django EC2 mod_wsgi 配置不起作用
【发布时间】:2012-04-21 01:07:44
【问题描述】:

我认为我的 apache2 服务器即将设置,但它仍然返回 500 错误。这是我的.wsgi 文件:

import os, sys

#path to directory of the .wsgi file ('apache/')
wsgi_dir = os.path.abspath(os.path.dirname(__file__))

#path to project root directory (parent of 'apache/')
project_dir = os.path.dirname(wsgi_dir)

sys.path.append(project_dir)
project_settings = os.path.join(project_dir, 'settings')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ecomstore.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

这是我的virtualhost 文件:

NameVirtualHost *:80
 <VirtualHost *:80>
      ServerAdmin admin@site.com
      ServerName www.site.com
      ServerAlias site.com

      Alias /static /home/ecomstore/static

      DocumentRoot /home/ecomstore
      WSGIScriptAlias / /home/ecomstore/apache/ecomstore.wsgi

      ErrorLog /var/log/apache2/error.log

      LogLevel warn

      CustomLog /var/log/apache2/access.log combined

 </VirtualHost>

这是我的服务器重启:

sudo /etc/init.d/apache2 restart
 * Restarting web server apache2
[Sun Apr 08 02:47:31 2012] [warn] module wsgi_module is already loaded, skipping
 ... waiting ..........[Sun Apr 08 02:47:42 2012] [warn] module wsgi_module is already loaded, skipping
   ...done.

但是,即使我在重新启动时没有收到 mod-wsgi 配置错误,我仍然收到之前提到的 500 Internal Server 错误。我有什么遗漏吗?

【问题讨论】:

    标签: django ubuntu apache2 amazon-ec2 mod-wsgi


    【解决方案1】:

    您需要查看发出请求时的 Apache 错误日志,而不是重启时的错误日志。

    问题的一个来源是 sys.path 不包含项目目录的父目录,由于您将 DJANGO_SETTINGS_MODULE 设置为,这将是必需的。阅读以下内容了解此要求:

    http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

    并修复它。

    除此之外,您还需要确定它是 Apache 500 页面还是 Django 页面。如果它是 Django 的,那么在 Django 设置文件中打开 DEBUG 并重新启动 Apache,这样你就会在浏览器中看到一个完整的错误。解决后关闭 DEBUG。

    【讨论】:

    • 是的,我检查了日志,发现:ImportError: Could not import settings 'ecomstore.settings' (Is it on sys.path? Does it have syntax errors?): No module named ecomstore.settings
    • 通过添加sys.path.append('/home')修复了上面的问题
    • 顺便说一句,不建议将 DocumentRoot 设置为您所拥有的。如果您稍后填充 Apache 配置,您的所有源代码都可以下载,包括 Django 设置文件中的数据库密码。切勿将 DocumentRoot 设置为包含您不希望用户看到的敏感内容的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2016-07-31
    相关资源
    最近更新 更多