[Fixed]-How to serve static files to AWS when deploying Django app (`python manage.py collectstatic` didn't work)?

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):

  1. Make sure that django.contrib.staticfiles is included in my INSTALLED_APPS.
  2. Check in browser console url to missing file e.g.
    /static/js/somefile.js
  3. Make sure in my django settings STATIC_URL is the same e.g. /static/.
  4. Check actual value in STATIC_ROOT and check that this folder actually contains your static files in production server.
  5. 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.

πŸ‘€valignatev

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.

enter image description here

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.

πŸ‘€manu

Leave a comment