简体   繁体   English

Amazon Elastic Beanstalk 不提供 django static 个文件

[英]Amazon Elastic Beanstalk not serving django static files

I am trying to put up a simple django app on elastic beanstalk.我正在尝试在 Elastic Beanstalk 上安装一个简单的 django 应用程序。 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 manually.我以为我已经弄清楚了应用程序的 static 部分,因为它与 heroku 一起工作并且在手动设置的服务器上。 In debugging I even checked in a pushed the static files in the static directory to try to simplify things.在调试中,我什至检查了 static 目录中的推送 static 文件,以尝试简化事情。 The mapping seems very strange in that it doesn't seem to follow the STATIC_ROOT.该映射看起来很奇怪,因为它似乎不遵循 STATIC_ROOT。

My relevant configs: settings.py我的相关配置:settings.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网址.py

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

LOGS:日志:

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

I came across the same problem today, and I realized that I forgot this option in the .ebextensions/.config file.我今天遇到了同样的问题,我意识到我在 .ebextensions/.config 文件中忘记了这个选项。 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要支持多个应用程序并执行此操作,您需要运行 collectstatic

Settings.py设置.py

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

Make sure you have a folder called "static" in your root确保您的根目录中有一个名为“static”的文件夹

In your ebs config file eg.在您的 ebs 配置文件中,例如。 (02_python.config or similar) (02_python.config 或类似的)

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

Then before you upload to ebs run python manage.py collectstatic然后在上传到 ebs 之前运行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然后你可以像往常一样运行eb deploy

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当您运行eb deploy时,这将为您运行 collect static

Just so you guys know, namespace for static files in recent versions of EBS, changed to aws:elasticbeanstalk:environment:proxy:staticfiles , like this:只是让你们知道,最新版本的 EBS 中静态文件的命名空间更改为aws:elasticbeanstalk:environment:proxy:staticfiles ,如下所示:

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

For me, the problem was having对我来说,问题在于

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

Instead, I changed it to相反,我将其更改为

STATIC_ROOT = 'static'

Also, my .conf file has另外,我的 .conf 文件有

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

If your environment uses a platform branch based on Amazon Linux 2, the right settings for .config file inside .ebextensions folder如果您的环境使用基于 Amazon Linux 2 的平台分支,则 .ebextensions 文件夹中的 .config 文件的正确设置

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

Inside your project settings.py you should have:在你的项目settings.py 中,你应该有:

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

All previous answers didn't help me This works for me.以前的所有答案都没有帮助我这对我有用。

Basically I created two steps inside .ebextensions基本上我在.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 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...!!"

An important thing, you settings/*.py should match with your static path that EBS serves, in my case this is my config.重要的是,您的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')
...

I did the following to fix static path in beanstalk我做了以下修复 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/"

I struggled for quite a while on that, thinking that the issue was with :我为此挣扎了很长时间,认为问题出在:

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

But actually my issue was with the other commands in the xxx.config file.但实际上我的问题是 xxx.config 文件中的其他命令。 basicaly, make sure the other lines are correct.基本上,确保其他行是正确的。

If you want to know my personal setup, I used the settings file shown above and I added the static directory in the root of my project.如果你想知道我的个人设置,我使用了上面显示的设置文件,并在我的项目的根目录中添加了静态目录。 For the settings.py file here is what I had for the static_url and root :对于 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'

Hope it helps !希望能帮助到你 !

Add a file name static-files.config under.ebextensions, and add below content:在.ebextensions下添加文件名static-files.config,并添加以下内容:

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

That works for me.这对我行得通。 I am using django2.2 + python 3.7我正在使用 django2.2 + python 3.7

For more detail please check: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environment-cfg-staticfiles.html#environment-cfg-staticfiles.namespace有关详细信息,请查看: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environment-cfg-staticfiles.html#environment-cfg-staticfiles.namespace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM