Configuring gunicorn for Django on Heroku

↘锁芯ラ 提交于 2019-12-19 03:25:27

问题


I'm trying to setup a test Django project on Heroku. Following the advice here and in the Heroku Getting Started I'm trying to use gunicorn instead of the Django dev server.

This was my first attempt at the Procfile:

web: gunicorn_django --workers=4 --bind=0.0.0.0:$PORT my_project/settings.py
worker: python my_project/manage.py celeryd -E -B --loglevel=INFO

This gave me this error:

ImportError: Could not import settings 'settings.py' (Is it on sys.path?): No module named py

I decided to take a different track and followed the advice here. Now my Procfile looked like this:

web: gunicorn_django -b 0.0.0.0:\$PORT -w 9 -k gevent --max-requests 250 --preload my_project.settings

(I also updated my requirements file to include gevent.) It gave me the same error:

ImportError: Could not import settings

Finally, I just set it to settings:

web: gunicorn_django -b 0.0.0.0:\$PORT -w 9 -k gevent --max-requests 250 --preload settings

But now I get this error:

Error: django project not found

The way my Django project is set up is that the settings.py file is in the parent directory of the repo -- I don't have the Django project under another directory. It's at the same level as the virtualenv and git files. Would that be a problem? I'm sure I'm doing something simple wrong -- any help would be much appreciated.


If I follow the instructions from Heroku here and change the Procfile to this:

web: gunicorn hellodjango.wsgi -b 0.0.0.0:$PORT

Nothing happens -- no errors in the logs, but no proceses run and the app just appears dead in the water.


回答1:


I have just run into this same issue. In the procfile you copied from the Heroku guide, change hellodjango.wsgi to yourproject.wsgi

Looks like we all fall victim to blindly copy-pasting now and then, but in your (and my) defense, it looks like there's no *.wsgi file that's actually being opened, it's just how you signal to gunicorn that you want it to run your django project.




回答2:


I had the same exact issue that you are having. The way I was able to finally get it working was to use the django app gunicorn.

I added gunicorn to the django settings.py

'gunicorn',

I then used this as my web entry in my Procfile.

web: python manage.py run_gunicorn -b 0.0.0.0:\$PORT -w 9 -k gevent --max-requests 250 --preload

You may have to alter you .manage.py if you use a different directory structure then I did. My app was in /app, and my python path was also /app.




回答3:


I had this issue and landed up having to point directly to the python path and then set the settings reference.

In the end my Procfile looks like this:

web: gunicorn_django --pythonpath=/app/project --settings=settings

I had to run heroku run which showed the env variables and that's where I was able to find the /app which I prepended to my project name.




回答4:


Do you have a requirements.txt in the root folder (containing the word django), as well as a settings.py? Those appear to the be the requirements for Django app detection, as documented here.



来源:https://stackoverflow.com/questions/10527512/configuring-gunicorn-for-django-on-heroku

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