[英]Amazon Elastic Beanstalk not serving django static files
我正在嘗試在 Elastic Beanstalk 上安裝一個簡單的 django 應用程序。 我以為我已經弄清楚了應用程序的 static 部分,因為它與 heroku 一起工作並且在手動設置的服務器上。 在調試中,我什至檢查了 static 目錄中的推送 static 文件,以嘗試簡化事情。 該映射看起來很奇怪,因為它似乎不遵循 STATIC_ROOT。
我的相關配置: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',
)
網址.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"
我今天遇到了同樣的問題,我意識到我在 .ebextensions/.config 文件中忘記了這個選項。 確保你也擁有它
option_settings:
- namespace: aws:elasticbeanstalk:container:python:staticfiles
option_name: /static/
value: static/
要支持多個應用程序並執行此操作,您需要運行 collectstatic
設置.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
只是讓你們知道,最新版本的 EBS 中靜態文件的命名空間更改為aws:elasticbeanstalk:environment:proxy:staticfiles
,如下所示:
option_settings:
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
對我來說,問題在於
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')
相反,我將其更改為
STATIC_ROOT = 'static'
另外,我的 .conf 文件有
option_settings:
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"
如果您的環境使用基於 Amazon Linux 2 的平台分支,則 .ebextensions 文件夾中的 .config 文件的正確設置
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
在你的項目settings.py 中,你應該有:
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
以前的所有答案都沒有幫助我這對我有用。
基本上我在.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')
...
我做了以下修復 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/"
我為此掙扎了很長時間,認為問題出在:
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'
希望能幫助到你 !
在.ebextensions下添加文件名static-files.config,並添加以下內容:
option_settings:
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
這對我行得通。 我正在使用 django2.2 + python 3.7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.