22π
There is definitive guide about deploying a django app to AWS Elastic Beanstalk from RealPython β here it is.
It has whole section about static files and how to configure it with eb and you donβt need to know anything about nginx/apache etc.
Basically you should define container_commands
in your eb config, these commands will be executed after application deploy is finished. For example migrate
and collectstatic
, so this is an example of such section in eb config file:
container_commands:
01_migrate:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py migrate --noinput"
leader_only: true
02_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py collectstatic --noinput"
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "iotd.settings"
"PYTHONPATH": "/opt/python/current/app/iotd:$PYTHONPATH"
"ALLOWED_HOSTS": ".elasticbeanstalk.com"
"aws:elasticbeanstalk:container:python":
WSGIPath: iotd/iotd/wsgi.py
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"
Pay attention to aws:elasticbeanstalk:container:python:staticfiles
part.
And also you should define this part in your django settings file:
STATIC_ROOT = os.path.join(BASE_DIR, "..", "www", "static")
STATIC_URL = '/static/'
I copied this example almost entirely from article above, you should really check it, itβs awesome.
UPD: how to debug missing staticfiles.
I usually do this (it involves sshing to your eb instance):
- Make sure that
django.contrib.staticfiles
is included in myINSTALLED_APPS
. - Check in browser console url to missing file e.g.
/static/js/somefile.js
- Make sure in my django settings
STATIC_URL
is the same e.g./static/
. - Check actual value in
STATIC_ROOT
and check that this folder actually contains your static files in production server. - Check that my eb config is pointing to correct folder (under your
option_settings
section in config)
Also you can try to collect static into /static
dir on your production server (itβs where eb looks for them by default). If all of a sudden it starts working β it means that your setting failed to override default one and you should check where else it was defined.
I hope these steps will help you to find right direction.
2π
This is what worked for me.
Remove any staticfiles directives from .config
files. Find Static Files section under the Software Configuration. The left column is each of your /static/
url. The right is your static folder relative to your parent directory.
Make sure your STATIC_ROOT
setting matches the value on the right. Donβt forget the trailing /
.
Pasting my settings anyway.
STATIC_URL = '/static/'
STATICFILES_DIRS = ('assets',)
STATIC_ROOT = os.path.join(BASE_DIR, '..', 'www', 'static')
And this is what my folder structure looks like relative to the settings files
project/
βββ __init__.py
βββ settings
βΒ Β βββ base.py
βΒ Β βββ __init__.py
βΒ Β βββ local.py
βΒ Β βββ production.py
wsgi.py
, urls.py
are located in project
folder. www
folder is one level above project
folder.
Hope this saves your Sunday at least.
- Is there a way to render a html page without view model?
- Postgresql socket error on OSX 10.7.3 when running Django's syncdb
- How do I write a Django model with ManyToMany relationsship with self through a Model