问题
This blog advises to put the following in my Procfile
.
web: python myproject_django/manage.py collectstatic --noinput; bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT myproject_django/settings.py
But running my project on Heroku fails. This is from $ Heroku logs
:
app[web.1]: bash: bin/gunicorn_django: No such file or directory
So there is a gunicorn_django
file/dir missing. What should that file/dir contain and where should I put it?
Tree
.
├── Procfile
├── myproject_django
│ ├── admin
│ ├── core
│ ├── __init__.py
│ ├── manage.py
│ ├── project_static
│ ├── settings.py
│ ├── templates
│ ├── urls.py
└── requirements.txt
Edit
When I change my Procfile
to web: python myproject_django/manage.py run_gunicorn -b 0.0.0.0:$PORT -w 3
my project runs fine(also serving static files).
When I then push to heroku, and do heroku run python myproject_django/manage.py collectstatic
and then heroku open
, then my site is being served except for the static files.
Tree (Note that the staticfiles
dir is empty)
.
├── Procfile
├── myproject_django
│ ├── admin
│ ├── core
│ │ ├── admin.py
│ │ ├── __init__.py
│ │ ├── models.py
│ │ ├── static
│ │ │ ├── css
│ │ │ │ ├── base.css
│ │ │ │ ├── layout.css
│ │ │ └── media
| | | ├── pek.ico
| │ │ ├── pek.png
| │ │ ├── pek_symbol.png
│ │ ├── tests.py
│ │ ├── views.py
│ ├── __init__.py
│ ├── manage.py
│ ├── settings.py
│ ├── staticfiles
│ ├── templates
│ │ └── core
│ │ ├── 404.html
│ │ ├── 500.html
│ │ ├── home.html
│ │ └── install.html
│ ├── urls.py
└── requirements.txt
In settings.py
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'static/media')
STATIC_ROOT = os.path.join(PROJECT_PATH,'staticfiles')
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'core/static'),
)
Edit2 (To Maxime R.)
Starting from the setup in the first edit, I ran python myproject_django/manage.py collectstatic
, then pushed to heroku. heroku open
still does not serve the static files.
Clicking on an image while DEBUG=True
returns a 404 error: request url: http://myproject_django.herokuapp.com/static/media/pyk1.png
.
Edit3 (To Maxime R.)
heroku logs
2012-08-18T10:18:37+00:00 heroku[web.1]: State changed from up to starting
2012-08-18T10:18:37+00:00 heroku[slugc]: Slug compilation finished
2012-08-18T10:18:38+00:00 heroku[web.1]: Starting process with command `python myproject_django/manage.py run_gunicorn -b 0.0.0.0:47262 -w 3`
2012-08-18T10:18:39+00:00 app[web.1]: /app/myproject_django
2012-08-18T10:18:39+00:00 app[web.1]: STATIC_ROOT: /app/myproject_django/staticfiles
2012-08-18T10:18:39+00:00 app[web.1]: STATICFILES_DIRS: '/app/myproject_django/core/static'
2012-08-18T10:18:39+00:00 app[web.1]: STATIC_ROOT: /app/myproject_django/staticfiles
2012-08-18T10:18:39+00:00 app[web.1]: STATICFILES_DIRS: '/app/myproject_django/core/static'
2012-08-18T10:18:39+00:00 app[web.1]: /app/myproject_django
2012-08-18T10:18:39+00:00 app[web.1]: 2012-08-18 05:18:39 [2] [INFO] Starting gunicorn 0.14.6
2012-08-18T10:18:39+00:00 app[web.1]: 2012-08-18 05:18:39 [2] [INFO] Listening at: http://0.0.0.0:47262 (2)
2012-08-18T10:18:39+00:00 app[web.1]: 2012-08-18 05:18:39 [2] [INFO] Using worker: sync
2012-08-18T10:18:39+00:00 app[web.1]: 2012-08-18 05:18:39 [5] [INFO] Booting worker with pid: 5
2012-08-18T10:18:39+00:00 app[web.1]: 2012-08-18 05:18:39 [6] [INFO] Booting worker with pid: 6
2012-08-18T10:18:39+00:00 app[web.1]: 2012-08-18 05:18:39 [7] [INFO] Booting worker with pid: 7
2012-08-18T10:18:39+00:00 app[web.1]: /app/myproject_django/app/myproject_django
2012-08-18T10:18:39+00:00 app[web.1]: /app/myproject_django
2012-08-18T10:18:39+00:00 app[web.1]: STATICFILES_DIRS: '/app/myproject_django/core/static'
2012-08-18T10:18:39+00:00 app[web.1]: STATIC_ROOT: /app/myproject_django/staticfilesSTATIC_ROOT: /app/myproject_django/staticfiles
2012-08-18T10:18:39+00:00 app[web.1]: STATICFILES_DIRS: '/app/myproject_django/core/static'
2012-08-18T10:18:39+00:00 app[web.1]:
2012-08-18T10:18:39+00:00 app[web.1]:
2012-08-18T10:18:39+00:00 app[web.1]: STATICFILES_DIRS: '/app/myproject_django/core/static'
2012-08-18T10:18:39+00:00 app[web.1]: STATIC_ROOT: /app/myproject_django/staticfiles
2012-08-18T10:18:40+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2012-08-18T10:18:40+00:00 heroku[web.1]: State changed from starting to up
2012-08-18T10:18:41+00:00 app[web.1]: 2012-08-18 05:18:41 [7] [INFO] Worker exiting (pid: 7)
2012-08-18T10:18:41+00:00 app[web.1]: 2012-08-18 05:18:41 [2] [INFO] Handling signal: term
2012-08-18T10:18:41+00:00 app[web.1]: 2012-08-18 05:18:41 [6] [INFO] Worker exiting (pid: 6)
2012-08-18T10:18:41+00:00 app[web.1]: 2012-08-18 05:18:41 [5] [INFO] Worker exiting (pid: 5)
2012-08-18T10:18:41+00:00 app[web.1]: 2012-08-18 05:18:41 [2] [INFO] Shutting down: Master
2012-08-18T10:18:42+00:00 heroku[web.1]: Process exited with status 0
2012-08-18T10:18:43+00:00 heroku[router]: GET myproject.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=36ms status=200 bytes=5474
2012-08-18T10:18:43+00:00 heroku[router]: GET myproject.herokuapp.com/static/media/pek.png dyno=web.1 queue=0 wait=0ms service=16ms status=404 bytes=2613
回答1:
The blog post you refers to dates back to november 2011 when the virtualenv was installed in /app
. The new style virtualenv commit moved the virtual env from /app
to /app/.heroku/venv
.
This explains your initial error, /app/.heroku/venv/bin/gunicorn_django
should exist.
This is bad practice:
You won't succeed to durably collect static files by collecting them after the push. Read the doc, especially the ephemeral filesystem part: collectstatic
and open
run in two different dynos, what's collected in the first won't be available to the second thus explaining the inconsistencies you pointed in your edit.
It only works if you collect files and launch the server in the same [web]
process but then it would recollect all static files every time the dyno is restarted. Just feels like bad design.
Either:
- collect your static files before pushing to heroku
- use another solution (like S3 even if the purpose of the blog post was not to use it)
Anyways, serving static assets with an elegant and efficient workflow ain't easy.
IMHO, better to stick with one command per process in Procfile
, I'll suggest you start scripting your deploys if you want to chain commands.
回答2:
make sure you have gunicorn as an installed app in settings.py.
and try running:
python my_project/manage.py run_gunicorn <options>
来源:https://stackoverflow.com/questions/12008569/heroku-deployment-using-django-and-gunicorn-missing-gunicorn-django-file-or-dir