Deploying Django app using passenger

主宰稳场 提交于 2019-12-03 12:46:36

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

If using django>1.7 replace two last line with

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

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.

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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!