Deploying Django app using passenger

2020-06-16 04:48发布

问题:

I can get through everything on their wiki - and then I'm lost. http://wiki.dreamhost.com/Django

I have a blank Django template, and whenever I try to change anything I get a 500 internal server error.

I have completely developed my django app locally and just want to host it online - figured it would be easy but am slowly learning that it is not.

I upload my app "videos" to this directory and then put it into the installed apps and ran "python manage.py syncdb", which finds no fixtures (which I Found odd).

From there, it just gets an internal server error.

Here is the error I am getting: http://tweettune.com/ and here is the error log:

[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

I've been trying for 6 hours now and can not figure out what I am doing wrong. I suppose I just don't understand how to deploy an application at all - my thought process now is take my locally hosted app and replace all the files in the default django template online. I don't see why this should not work but it's not. I tried the "hello world app" example by using this code in my passenger_wdgi file and it works...

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

Any direction would be helpful.

EDIT: Here are the contents of my passenger_wsgi.py file which may be helpful (although it is automatically generated by dreamhost...so figured it would be correct).

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)

回答1:

I had the same problem. The solution was to add the folder of my application in the 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()

This link was very useful to me: http://discussion.dreamhost.com/thread-128918.html



回答2:

If using django>1.7 replace two last line with

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


回答3:

It's difficult to know exactly what you're doing wrong without seeing your setup. I followed the instructions, and it wasn't that hard.

One thing you can do to debug your application is to run through manage.py. It won't be able to bind to a socket (and if it does, it will get autokilled after a few minutes), but that will at least show if there are other problems that prevent your app from running.

One other thing to note: the file is called passenger_wsgi.py, and should live in your site root. For example, I have ~/testing.tustincommercial.com/passenger_wsgi.py, and all of my project code lives under ~/testing.tustincommercial.com/oneclickcos. My static content lives under ~/testing.tustincommercial.com/public.

It can help to have some error-handling middleware installed, so that errors don't propagate all the way to passenger, thus triggering a 500 error.

My wsgi_passenger.py is:

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)

This does a bunch of stuff, most of which isn't strictly necessary - all of the stuff at the top is to make sure that the libraries I've installed are available. The stuff at the bottom installs middleware. You're probably better off with paste.exceptions.errormiddleware.ErrorMiddleware, unless the werkzeug debugger works for you (it won't work for me on Dreamhost).

Edit: I think your config is wrong. Please go the directory which holds your project, and use pwd to get the full path. I think you will find you have not got your paths right.



回答4:

I had the exact same problem. The answers here put me in the right direction, thanks. I use virtualenv with my django app and the os.getcwd() did it for me.

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()