Amazon弹性Beanstalk不服务django静态文件

时间:2021-09-09 23:22:41

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 manually. In debugging I even checked in a pushed the static files in the static directory to try to simplify things. The mapping seems very strange in that it doesn't seem to follow the STATIC_ROOT.

我想在橡皮筋上安装一个简单的django应用。我想我已经弄清楚了应用程序的静态部分,因为它与heroku和一个手动设置的服务器一起工作。在调试过程中,我甚至检入了静态目录中的静态文件,试图简化操作。这个映射看起来很奇怪,因为它似乎不遵循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

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

3 个解决方案

#1


16  

Have you found the solution yet? just to let you know. I came across the same problem today, and I realized that I forgot this option in the .ebextensions/.config file. make sure you have it too

你找到解决办法了吗?只是想让你知道。我今天遇到了同样的问题,我意识到我忘记了这个选项。配置文件。确保你也有

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

#2


3  

To support multiple apps and do this you need to run collectstatic

要支持多个应用程序并做到这一点,您需要运行collectstatic

Settings.py

Settings.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. (02_python.config or similar)

在您的ebs配置文件中。(02 _python。配置或类似)

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

Then before you upload to ebs run python manage.py collectstatic

然后在上传至ebs之前运行python管理。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部署

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部署时,它将为您运行collect静态

#3


2  

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

#1


16  

Have you found the solution yet? just to let you know. I came across the same problem today, and I realized that I forgot this option in the .ebextensions/.config file. make sure you have it too

你找到解决办法了吗?只是想让你知道。我今天遇到了同样的问题,我意识到我忘记了这个选项。配置文件。确保你也有

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

#2


3  

To support multiple apps and do this you need to run collectstatic

要支持多个应用程序并做到这一点,您需要运行collectstatic

Settings.py

Settings.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. (02_python.config or similar)

在您的ebs配置文件中。(02 _python。配置或类似)

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

Then before you upload to ebs run python manage.py collectstatic

然后在上传至ebs之前运行python管理。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部署

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部署时,它将为您运行collect静态

#3


2  

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