Amazon Elastic Beanstalk not serving django static files

后端 未结 8 2055
你的背包
你的背包 2021-02-18 19:40

I am trying to put up a simple django app on elastic beanstalk. I thought I had the static parts of the app figured out as it works with heroku and on a server that was set up

8条回答
  •  野性不改
    2021-02-18 20:02

    All previous answers didn't help me This works for me.

    Basically I created two steps inside .ebextensions

    01_django.config

    container_commands:
        01_migrate:
            command: "source /opt/python/current/env && source /opt/python/run/venv/bin/activate && cd /opt/python/current/app && python manage.py migrate --noinput"
            leader_only: true
        02_touch_superuser:
            command: "source /opt/python/current/env && source /opt/python/run/venv/bin/activate && cd /opt/python/current/app && python manage.py touch_superuser"
            leader_only: true
    option_settings:
        aws:elasticbeanstalk:container:python:
            WSGIPath: config/wsgi.py
            NumProcesses: 2
            NumThreads: 10
        aws:elasticbeanstalk:application:environment:
            STAGING: 1
            DJANGO_SETTINGS_MODULE: config.settings.production
        aws:elasticbeanstalk:container:python:staticfiles:
            "/static/": "htdocs/static/"
            "/media/": "htdocs/media/"
    

    config/wsgi.py Could be a different path in your project

    02_collec_static.config

    files:
      "/opt/elasticbeanstalk/hooks/appdeploy/post/10_collect_static.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
          set -xe
    
          source /opt/python/current/env
          source /opt/python/run/venv/bin/activate
          cd /opt/python/current/app && python manage.py collectstatic --noinput
    
          echo "Statics collected...!!"
    
    

    An important thing, you settings/*.py should match with your static path that EBS serves, in my case this is my config.

    ...
    PROJECT_PATH = dirname(dirname(dirname(__file__)))
    MEDIA_ROOT = os.path.join(PROJECT_PATH, 'htdocs/media')
    STATIC_ROOT = os.path.join(PROJECT_PATH, 'htdocs/static')
    ...
    

提交回复
热议问题