简体   繁体   English

如何在 Elastic Beanstalk 中配置 aws:elasticbeanstalk:container:python:staticfiles?

[英]How do I configure aws:elasticbeanstalk:container:python:staticfiles in Elastic Beanstalk?

I'm trying to run eb create to deploy my Django project to AWS.我正在尝试运行eb create以将我的 Django 项目部署到 AWS。 The error I'm getting is ERROR: ServiceError - Configuration validation exception: Invalid option specification (Namespace: 'aws:elasticbeanstalk:container:python:staticfiles', OptionName: '/static/'): Unknown configuration setting.我得到的错误是ERROR: ServiceError - Configuration validation exception: Invalid option specification (Namespace: 'aws:elasticbeanstalk:container:python:staticfiles', OptionName: '/static/'): Unknown configuration setting. I'm unsure what this error means and there's not much I can find on it.我不确定这个错误是什么意思,而且我找不到太多关于它的东西。

I've tried to define this variable in .ebextensions/django.config .我试图在.ebextensions/django.config中定义这个变量。

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango.wsgi:application
  aws:elasticbeanstalk:container:python:staticfiles:
    /static/: 'frontend/views/build/static/'

My settings.py is configured with the following vars:我的 settings.py 配置了以下变量:

STATIC_URL = '/static/'
    
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'frontend/views/build/static')
]

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

I'm trying to run this ebs instance on Amazon Linux 2我正在尝试在 Amazon Linux 2 上运行此 ebs 实例

If your environment uses a platform branch based on Amazon Linux 2, use the aws:elasticbeanstalk:environment:proxy:staticfiles namespace.如果您的环境使用基于 Amazon Linux 2 的平台分支,请使用 aws:elasticbeanstalk:environment:proxy:staticfiles 命名空间。

The following example configuration file tells the proxy server to serve files:以下示例配置文件告诉代理服务器提供文件:

Example.ebextensions/django.config Example.ebextensions/django.config

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

Note:笔记:

  1. Your static files are collected in STATIC_ROOT path not in STATICFILES_DIRS您的 static 文件收集在STATIC_ROOT路径而不是STATICFILES_DIRS

Ah yes, Amazon Linux 2 documentation are not entirely up to speed.啊,是的,Amazon Linux 2 文档并未完全跟上速度。

  1. update your django.config like so:像这样更新您的django.config
option_settings:
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: /path/to/static
  1. ensure that your static files are collected on deploy, to do so we can use hooks :确保在部署时收集您的 static 文件,为此我们可以使用钩子

Create a postdeploy hook file called .platform/hooks/01_collect_static.sh and make it executable with chmod.创建一个名为.platform/hooks/01_collect_static.sh的部署后挂钩文件,并使用 chmod 使其可执行。

Enter the following content:输入以下内容:

#!/bin/bash

source "$PYTHONPATH/activate" && {

  python manage.py collectstatic --noinput;

}

On deploy, django will now collect your static files into the folder you've defined in settings.py .在部署时,django 现在会将您的 static 文件收集到您在settings.py中定义的文件夹中。

Note, I include my migrate command here as well, such that my hook file looks like:注意,我在这里也包含了我的 migrate 命令,这样我的钩子文件看起来像:

#!/bin/bash

source "$PYTHONPATH/activate" && {  
    
  if [[ $EB_IS_COMMAND_LEADER == "true" ]];
  then 
    # log which migrations have already been applied
    python manage.py showmigrations;
    # migrate
    python manage.py migrate --noinput;
  else 
    echo "this instance is NOT the leader";
  fi
  python manage.py collectstatic --noinput;

}

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

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