【问题标题】:Amazon Elastic Beanstalk not serving django static filesAmazon Elastic Beanstalk 不提供 django 静态文件
【发布时间】:2012-12-12 03:48:00
【问题描述】:

我正在尝试在弹性 beantalk 上建立一个简单的 django 应用程序。我以为我已经弄清楚了应用程序的静态部分,因为它可以与 heroku 和手动设置的服务器一起使用。在调试中,我什至签入了静态目录中的静态文件以尝试简化事情。该映射似乎很奇怪,因为它似乎不遵循 STATIC_ROOT。

我的相关配置: 设置.py

PROJECT_ROOT = os.path.abspath(os.path.dirname(__name__))
STATIC_ROOT = os.path.join(PROJECT_ROOT,'static/')
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

urls.py

(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),

日志:

[Wed Dec 26 15:39:04 2012] [error] [client 10.29.203.20] File does not exist: /opt/python/current/app/css, referer 10.29.203.20 - - 
[26/Dec/2012:15:39:04 +0000] "GET /static/css/styles.css HTTP/1.1" 404 329 "http://" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11"

【问题讨论】:

    标签: django amazon-web-services amazon-elastic-beanstalk django-staticfiles


    【解决方案1】:

    如果您的环境使用基于 Amazon Linux 2 的平台分支,则为 .ebextensions 文件夹中的 .config 文件

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

    在您的项目 settings.py 中,您应该有:

    STATIC_URL = '/static/'
    STATIC_ROOT = 'static'
    

    【讨论】:

    • 你解决了我 4 小时的问题。这个答案非常适合我。注意“Amazon Linux 2”实例。
    • 没有撇号对我来说是行不通的。应该是 "/static/": "static"
    【解决方案2】:

    我今天遇到了同样的问题,我意识到我在 .ebextensions/.config 文件中忘记了这个选项。确保你也拥有它

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

    【讨论】:

    • 只是为了说清楚:值static/ 指的是您的静态文件所在的文件夹。在我的情况下是app/static,这让我想知道为什么static/ 不是工作。
    • 我遇到语法错误...这似乎是新语法:option_settings: "aws:elasticbeanstalk:container:python:staticfiles": /static/: "app/static/" 不过我认为这不是最佳解决方案,因为它似乎是 app-具体..
    • 如果有几个应用程序都有自己的static 目录,这会起作用吗?
    • 请帮助!我应该如何对弹性 beantalk 上的多容器 Django 应用程序做同样的事情?我最关心的是这条线namespace: aws:elasticbeanstalk:container:python:staticfiles
    【解决方案3】:

    大家知道,最近版本的 EBS 中静态文件的命名空间已更改为 aws:elasticbeanstalk:environment:proxy:staticfiles,如下所示:

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

    【讨论】:

    【解决方案4】:

    所有以前的答案都没有帮助我 这对我有用。

    基本上我在.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 可能是您项目中的不同路径

    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...!!"
    
    

    重要的是,您的settings/*.py 应该与 EBS 服务的静态路径相匹配,在我的情况下,这是我的配置。

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

    【讨论】:

      【解决方案5】:

      我为此挣扎了很长时间,认为问题出在:

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

      但实际上我的问题在于 xxx.config 文件中的其他命令。 基本上,确保其他行是正确的。

      如果你想知道我的个人设置,我使用了上面显示的设置文件,并在我的项目的根目录中添加了静态目录。 对于 settings.py 文件,这里是我对 static_url 和 root 的内容:

      # Static files (CSS, JavaScript, Images)
      # https://docs.djangoproject.com/en/2.2/howto/static-files/
      
      STATIC_URL = '/static/'
      STATIC_ROOT = 'static'
      

      希望对你有帮助!

      【讨论】:

        【解决方案6】:

        我做了以下修复 beanstalk 中的静态路径

        STATIC_URL = '/static/'
        
        BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        STATIC_ROOT = os.path.join(BASE_DIR, 'static')
        
        option_settings:
          ...
          ...
          "aws:elasticbeanstalk:container:python:staticfiles":
            "/static/": "static/"
        

        【讨论】:

        • 谢谢。但对我(AWS Elastic Beanstalk)来说,我需要做STATIC_ROOT = 'static'
        【解决方案7】:

        对我来说,问题出在

        STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')
        

        相反,我将其更改为

        STATIC_ROOT = 'static'
        

        另外,我的 .conf 文件有

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

        【讨论】:

        • 为什么os.path.join(os.path.dirname(__file__), 'static')错了?
        【解决方案8】:

        要支持多个应用并执行此操作,您需要运行 collectstatic

        Settings.py

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

        确保您的根目录中有一个名为“static”的文件夹

        在您的 ebs 配置文件中,例如。 (02_python.config 或类似)

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

        然后在你上传到 ebs 之前运行python manage.py collectstatic

        这会将所有静态文件收集到您在配置中已经指向的一个文件夹中。

        然后你可以正常运行eb deploy

        如果您不想将静态文件两次提交到源代码管理并希望服务器为您执行此操作,则可以选择将其添加到您的配置中

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

        所以你的文件应该是这样的:

        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/"
        

        当您运行 eb deploy 时,这将为您运行 collect static

        【讨论】:

        • 有了这个,静态文件现在加载,但我得到的 container_commands 条目:'.ebextensions/django.config' - 包含无效键:'01_collectstatic'。摸索文档尚不清楚这些是如何处理的。但是这些例子有帮助:github.com/awsdocs/elastic-beanstalk-samples/blob/…
        猜你喜欢
        • 2018-05-18
        • 2018-08-09
        • 2020-11-08
        • 2018-10-16
        • 2014-08-26
        • 2020-09-28
        • 2020-11-15
        • 2021-07-08
        相关资源
        最近更新 更多