Deploying Django app using passenger
Asked Answered
B

4

8

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)
Bundy answered 24/8, 2011 at 15:37 Comment(2)
Sorry, I'm not sure what you mean by my config files? The passenger_wsgi.py contents? Please excuse my ignorance on this subject - first time trying to deploy a project.Bundy
I think you have got your paths wrong. Navigate to your project directory in the shell, and use pwd to print the full name of that directory.Sure
P
7

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

Phalan answered 27/8, 2011 at 7:11 Comment(0)
S
7

If using django>1.7 replace two last line with

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Searching answered 23/10, 2014 at 8:30 Comment(0)
S
1

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.

Sure answered 24/8, 2011 at 15:58 Comment(9)
I suppose I have a question then. Would I expect to see the normal django debug errors if there was an issue with my setup or will it just turn back a 500 server error?Bundy
A 500 error could be caused either by exceptions from your code propagating all the way to the top, or by a problem with your configuration. The point of installing the middleware is to catch and display exceptions coming out of your code. So, bad configuration will cause a 500 error, but it is not the only possible source.Sure
PWD response - /home/brandonmattalo/tweettune.com and my django project itself is /home/brandonmattalo/tweettune.com/sotdBundy
Sorry, when you say config what would you be referring to so that I can look into it a bit more. And just a question so I can help debug it myself as well - in a django app if it threw up a normal django exception (For example template not found, or python syntax error) would I expect to see this when running it on dreamhost (assuming settings.py has Debug=True)? OR does it just go to this 500 error.Bundy
Hmm I seem to now be getting django errors after I changed one small thing in my settings installed apps <sitename>.<app> as opposed to just <app> but now I have a bunch more (although more familiar errors) to work though. Thanks for the push in the right direction.Bundy
Did you fix your project path?Sure
Yes I did it worked perfectly, sorry meant to come back here and thank you. if you want to check out the finished project Tweet Tune - thanks again for the help. The main problem ended up being that on my dev server I could do a "from video.models import Video" However on the actual server I had to do from <project_name>.video.models import Video - and this was for all models. Not sure why since it was the same project paths but it did end up working.Bundy
Note that the default python version on dreamhost is 2.5.2. I don't know if the details of package handling have changed since that version.Sure
I noted this as well and installed my own version using env since I had package dependencies that I required and don't have root access since I'm on a VPS.Bundy
T
0

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()
Trustless answered 7/6, 2013 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.