Amazon Elastic Beanstalk not serving django static files Amazon Elastic Beanstalk not serving django static files django django

Amazon Elastic Beanstalk not serving django static files


I came across the same problem today, and I realized that I forgot this option in the .ebextensions/.config file. Make sure you have it too

option_settings:  - namespace: aws:elasticbeanstalk:container:python:staticfiles    option_name: /static/    value: static/


To support multiple apps and do this you need to run collectstatic

Settings.py

STATIC_ROOT = os.path.join(BASE_DIR, "static")

Make sure you have a folder called "static" in your root

In your ebs config file eg. (02_python.config or similar)

option_settings:    ...    "aws:elasticbeanstalk:container:python:staticfiles":        /static/: "static/"

Then before you upload to ebs run python manage.py collectstatic

This collects all the static files in one folder which you have already pointed to in your config.

Then you can run eb deploy like normal

Opptionally if you don't want to commit the static files to your source control twice and want the server to do this for you add this to your config

container_commands:01_collectstatic:    command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput"

So your file should look something like this:

container_commands:01_collectstatic:  command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput"option_settings:    "aws:elasticbeanstalk:container:python":      WSGIPath: app/wsgi.py    "aws:elasticbeanstalk:container:python:staticfiles":      /static/: "static/"

This will run collect static for you when you run eb deploy


Just so you guys know, namespace for static files in recent versions of EBS, changed to aws:elasticbeanstalk:environment:proxy:staticfiles, like this:

option_settings:  aws:elasticbeanstalk:environment:proxy:staticfiles:    /static: static