【问题标题】:Deploying Django app using passenger使用乘客部署 Django 应用程序
【发布时间】:2011-11-02 22:37:12
【问题描述】:

我可以浏览他们 wiki 上的所有内容 - 然后我就迷路了。 http://wiki.dreamhost.com/Django

我有一个空白的 Django 模板,每当我尝试更改任何内容时,都会收到 500 内部服务器错误。

我已经在本地完全开发了我的 django 应用程序,只是想在线托管它 - 认为这很容易,但我慢慢知道它不是。

我将我的应用程序“videos”上传到此目录,然后将其放入已安装的应用程序并运行“python manage.py syncdb”,它没有找到任何固定装置(我觉得很奇怪)。

从那里,它只是得到一个内部服务器错误。

这是我得到的错误:http://tweettune.com/,这是错误日志:

[Wed Aug 24 01:49:15 2011] [error] [client 66.212.30.122] Premature end of script headers:
[Wed Aug 24 01:49:15 2011] [error] [client 66.212.30.122] Premature end of script headers: internal_error.html
[Wed Aug 24 08:16:40 2011] [error] [client 99.229.160.94] Premature end of script headers:
[Wed Aug 24 08:16:41 2011] [error] [client 99.229.160.94] Premature end of script headers: internal_error.html
[Wed Aug 24 08:21:38 2011] [error] [client 99.229.160.94] Premature end of script headers:
[Wed Aug 24 08:21:38 2011] [error] [client 99.229.160.94] Premature end of script headers: internal_error.html
[Wed Aug 24 08:27:41 2011] [error] [client 99.229.160.94] Premature end of script headers:
[Wed Aug 24 08:27:41 2011] [error] [client 99.229.160.94] Premature end of script headers: internal_error.html

我已经尝试了 6 个小时,但无法弄清楚我做错了什么。我想我根本不明白如何部署应用程序——我现在的思考过程是使用本地托管的应用程序并在线替换默认 django 模板中的所有文件。我不明白为什么这不起作用,但事实并非如此。我通过在passenger_wdgi文件中使用此代码尝试了“hello world app”示例,它可以工作...

def application(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/plain')])
    return ["Hello, world!"]

任何方向都会有所帮助。

编辑:以下是我的 Passenger_wsgi.py 文件的内容,可能会有所帮助(尽管它是由 dreamhost 自动生成的......所以认为它是正确的)。

import sys, os
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = "sotd.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
project_path='/home/tweettune.com/sotd/'
sys.path.insert(1, project_path)

【问题讨论】:

  • 抱歉,我不确定你的配置文件是什么意思? passenger_wsgi.py 的内容?请原谅我对这个主题的无知 - 第一次尝试部署项目。
  • 我认为您的路径错误。在 shell 中导航到您的项目目录,并使用 pwd 打印该目录的全名。

标签: django passenger dreamhost


【解决方案1】:

我遇到了同样的问题。 解决方案是在 wsgi_passenger.py 中添加我的应用程序的文件夹

import sys, os
sys.path.append(os.getcwd())
sys.path.append(os.path.join(os.getcwd(), 'include your apps folder here'))
os.environ['DJANGO_SETTINGS_MODULE'] = "cpc.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

这个链接对我非常有用: http://discussion.dreamhost.com/thread-128918.html

【讨论】:

    【解决方案2】:

    如果使用 django>1.7 将最后两行替换为

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

    【讨论】:

      【解决方案3】:

      如果不查看您的设置,很难确切地知道您做错了什么。我按照说明进行操作,并没有那么难。

      调试应用程序可以做的一件事是运行 manage.py。它无法绑定到套接字(如果绑定了,它会在几分钟后自动终止),但这至少可以显示是否有其他问题阻止您的应用运行。

      还有一点需要注意:该文件名为passenger_wsgi.py,应该位于您的站点根目录中。例如,我有~/testing.tustincommercial.com/passenger_wsgi.py,我所有的项目代码都在~/testing.tustincommercial.com/oneclickcos 下。我的静态内容位于~/testing.tustincommercial.com/public 下。

      安装一些错误处理中间件会有所帮助,这样错误就不会一直传播到乘客,从而触发 500 错误。

      我的wsgi_passenger.py 是:

      import sys, os, re
      cwd = os.getcwd()
      sys.path.append(os.getcwd())
      
      #add all installed eggs to path
      for x in ['/home/marcintustin/django/'+x for x in os.listdir('/home/marcintustin/django/') if re.search('egg$', x)]:
          sys.path.insert(0,x)
      
      sys.path.insert(0,'/home/marcintustin/django')
      sys.path.insert(0,'/home/marcintustin/django/Django-1.3')
      sys.path.insert(0,'/home/marcintustin/django/Paste-1.7.5.1-py2.5.egg')
      sys.path.insert(0,'/home/marcintustin/django/South-0.7.3-py2.5.egg')
      sys.path.insert(0,'/home/marcintustin/django/Werkzeug-0.6.2-py2.5.egg')
      
      myapp_directory = cwd + '/oneclickcos'
      sys.stdout = sys.stderr
      sys.path.insert(0,myapp_directory)
      sys.path.append(os.getcwd())
      
      os.environ['DJANGO_SETTINGS_MODULE'] = "oneclickcos.settings"
      import django.core.handlers.wsgi
      #from paste.exceptions.errormiddleware import ErrorMiddleware
      from werkzeug.debug import DebuggedApplication
      from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
      
      application = django.core.handlers.wsgi.WSGIHandler()
      handler = AdminMediaHandler(application, '/home/marcintustin/testing.tustincommercial.com/public/static/admin')
      application = DebuggedApplication(handler, evalex=True)
      

      这做了很多事情,其中​​大部分不是绝对必要的 - 顶部的所有内容都是为了确保我安装的库可用。底部的东西安装中间件。使用paste.exceptions.errormiddleware.ErrorMiddleware 可能会更好,除非 werkzeug 调试器对你有用(它在 Dreamhost 上对我不起作用)。

      编辑:我认为你的配置是错误的。请转到保存项目的目录,并使用pwd 获取完整路径。我想你会发现你的路径不对。

      【讨论】:

      • 我想我有一个问题。如果我的设置有问题,我会期望看到正常的 django 调试错误,还是会返回 500 服务器错误?
      • 500 错误可能是由于您的代码的异常一直传播到顶部,也可能是您的配置有问题。安装中间件的目的是捕获并显示代码中出现的异常。因此,错误的配置会导致 500 错误,但这不是唯一可能的来源。
      • PWD 响应 - /home/brandonmattalo/tweettune.com 和我的 django 项目本身是 /home/brandonmattalo/tweettune.com/sotd
      • 对不起,当您说配置时,您指的是什么,以便我可以进一步研究。只是一个问题,所以我也可以自己帮助调试它 - 在 django 应用程序中,如果它抛出一个正常的 django 异常(例如找不到模板,或 python 语法错误),我希望在 Dreamhost 上运行它时看到这个(假设 settings.py 有 Debug=True)?或者它只是去这个 500 错误。
      • 嗯,我在设置中更改了一个小东西后,我现在似乎遇到了 django 错误安装的应用程序 . 而不仅仅是 但现在我有更多(虽然更熟悉的错误)虽然工作。感谢您朝着正确的方向推动。
      【解决方案4】:

      我遇到了完全相同的问题。这里的答案让我朝着正确的方向前进,谢谢。我将 virtualenv 与我的 django 应用程序一起使用,而 os.getcwd() 为我完成了它。

      import os, sys
      
      #Fix for passenger
      INTERP = "/var/webapps/myapp_env/bin/python"
      if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
      sys.path.append(os.getcwd())
      #
      
      os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
      from django.core.wsgi import get_wsgi_application
      application = get_wsgi_application()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-26
        • 2011-01-30
        • 2015-01-06
        • 2016-04-20
        • 1970-01-01
        相关资源
        最近更新 更多