Yesterday, I created this post: DjangoRestFramework browsable api looks different locally vs when deployed on server?
昨天,我创建了这篇文章:DjangoRestFramework可浏览的api在本地与在服务器上部署时看起来不同?
Basically, when I did python manage.py runserver
, this showed up:
基本上,当我做python manage.py runserver时,出现了这个:
But after I deployed it to AWS (eb deploy
), this is what I see when I access the site:
但在我将其部署到AWS(eb deploy)之后,这就是我访问网站时看到的内容:
The answer to the post above mentioned that it is because my static files were missing. So I searched how to deploy static files on AWS and came across this tutorial: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html#python-django-update-app
上面帖子的答案提到这是因为我的静态文件丢失了。所以我搜索了如何在AWS上部署静态文件并遇到了这个教程:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html#python-django-update-应用
Under the "Create a Site Administrator" section, it mentions that in order to serve static files, I must first define STATIC_ROOT
in settings.py
(so I did: STATIC_ROOT = os.path.join(BASE_DIR, "ebdjangoapp/static/")
) and then I did eb deploy
. However, the site still looks the same as the 2nd image (without static files). I then tried doing python manage.py collectstatic
(this created the static folder with the rest_framework
directory inside it, containing the css
files etc.) and then did eb deploy
again but the site stil looks the same as the 2nd image.
在“创建站点管理员”部分下,它提到为了提供静态文件,我必须首先在settings.py中定义STATIC_ROOT(所以我做了:STATIC_ROOT = os.path.join(BASE_DIR,“ebdjangoapp / static /”) ))然后我做了eb部署。但是,该站点仍然看起来与第二个图像相同(没有静态文件)。然后我尝试做python manage.py collectstatic(这创建了包含其中的rest_framework目录的静态文件夹,包含css文件等)然后再次进行eb部署,但站点stil看起来与第二个图像相同。
How come the static files still aren't showing up?
为什么静态文件仍未显示?
Note, I searched around and came across this post: Django app deployment not loading static files and the answer says:
注意,我搜索过并发现了这篇文章:Django app部署没有加载静态文件,答案是:
"You then need to serve settings.STATIC_ROOT
at settings.STATIC_URL
via your web server of choice, very commonly nginx as a reverse proxy behind your Apache-mod_wsgi app server."
“然后,您需要通过您选择的Web服务器在settings.STATIC_URL处提供settings.STATIC_ROOT,非常常见的是nginx作为Apache-mod_wsgi应用服务器后面的反向代理。”
But I have no idea how web servers (nginx, reverse proxy, Apache-mod_wsgi) works. I have a Django app I run locally with python manage.py runserver
, and I have AWS elastic beanstalk. I deploy my Django app to AWS by doing eb deploy
. What steps do I need to take in order for the static files to appear on deployment (assuming I don't know how to configure nginx, reverse proxy etc.).?
但我不知道Web服务器(nginx,反向代理,Apache-mod_wsgi)是如何工作的。我有一个Django应用程序,我在本地运行python manage.py runserver,我有AWS弹性beanstalk。我通过执行eb部署将我的Django应用程序部署到AWS。我需要采取哪些步骤才能在部署时显示静态文件(假设我不知道如何配置nginx,反向代理等)。
2 个解决方案
#1
13
There is definitive guide about deploying a django app to AWS Elastic Beanstalk from RealPython - here it is. It has whole section about static files and how to configure it with eb and you don't need to know anything about nginx/apache etc.
关于从RealPython向AWS Elastic Beanstalk部署django应用程序的确切指南 - 就在这里。它有关于静态文件以及如何使用eb配置的整个部分,您不需要了解有关nginx / apache等的任何信息。
Basically you should define container_commands
in your eb config, these commands will be executed after application deploy is finished. For example migrate
and collectstatic
, so this is an example of such section in eb config file:
基本上你应该在你的eb配置中定义container_commands,这些命令将在应用程序部署完成后执行。例如migrate和collectstatic,所以这是eb配置文件中此类部分的示例:
container_commands:
01_migrate:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py migrate --noinput"
leader_only: true
02_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py collectstatic --noinput"
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "iotd.settings"
"PYTHONPATH": "/opt/python/current/app/iotd:$PYTHONPATH"
"ALLOWED_HOSTS": ".elasticbeanstalk.com"
"aws:elasticbeanstalk:container:python":
WSGIPath: iotd/iotd/wsgi.py
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"
Pay attention to aws:elasticbeanstalk:container:python:staticfiles
part. And also you should define this part in your django settings file:
注意aws:elasticbeanstalk:container:python:staticfiles part。您还应该在django设置文件中定义此部分:
STATIC_ROOT = os.path.join(BASE_DIR, "..", "www", "static")
STATIC_URL = '/static/'
I copied this example almost entirely from article above, you should really check it, it's awesome.
我几乎完全从上面的文章中复制了这个例子,你应该检查一下,它真棒。
UPD: how to debug missing staticfiles. I usually do this (it involves sshing to your eb instance):
UPD:如何调试缺少的静态文件。我通常这样做(它涉及到你的eb实例sshing):
- Make sure that
django.contrib.staticfiles
is included in myINSTALLED_APPS
. - 确保django.contrib.staticfiles包含在我的INSTALLED_APPS中。
- Check in browser console url to missing file e.g.
/static/js/somefile.js
- 将浏览器控制台URL检入丢失的文件,例如/static/js/somefile.js
- Make sure in my django settings
STATIC_URL
is the same e.g./static/
. - 请确保在我的django设置中STATIC_URL是相同的,例如/静态的/。
- Check actual value in
STATIC_ROOT
and check that this folder actually contains your static files in production server. - 检查STATIC_ROOT中的实际值,并检查该文件夹是否实际包含生产服务器中的静态文件。
- Check that my eb config is pointing to correct folder (under your
option_settings
section in config) - 检查我的eb配置是否指向正确的文件夹(在config中的option_settings部分下)
Also you can try to collect static into /static
dir on your production server (it's where eb looks for them by default). If all of a sudden it starts working - it means that your setting failed to override default one and you should check where else it was defined.
您还可以尝试在生产服务器上收集静态到/静态目录(默认情况下,eb会查找它们)。如果它突然开始工作 - 这意味着您的设置无法覆盖默认设置,您应该检查它的定义位置。
I hope these steps will help you to find right direction.
我希望这些步骤能帮助您找到正确的方向。
#2
1
This is what worked for me.
这对我有用。
Remove any staticfiles directives from .config
files. Find Static Files section under the Software Configuration. The left column is each of your /static/
url. The right is your static folder relative to your parent directory.
从.config文件中删除所有staticfiles指令。 “软件配置”下的“查找静态文件”部分左列是/ static / url中的每一个。右边是相对于父目录的静态文件夹。
Make sure your STATIC_ROOT
setting matches the value on the right. Don't forget the trailing /
.
确保您的STATIC_ROOT设置与右侧的值匹配。不要忘记尾随/。
Pasting my settings anyway.
无论如何都要粘贴我的设置。
STATIC_URL = '/static/'
STATICFILES_DIRS = ('assets',)
STATIC_ROOT = os.path.join(BASE_DIR, '..', 'www', 'static')
And this is what my folder structure looks like relative to the settings files
这就是我的文件夹结构相对于设置文件的样子
project/
├── __init__.py
├── settings
│ ├── base.py
│ ├── __init__.py
│ ├── local.py
│ ├── production.py
wsgi.py
, urls.py
are located in project
folder. www
folder is one level above project
folder.
wsgi.py,urls.py位于项目文件夹中。 www文件夹是项目文件夹之上的一级。
Hope this saves your Sunday at least.
希望这至少可以节省你的星期天。
#1
13
There is definitive guide about deploying a django app to AWS Elastic Beanstalk from RealPython - here it is. It has whole section about static files and how to configure it with eb and you don't need to know anything about nginx/apache etc.
关于从RealPython向AWS Elastic Beanstalk部署django应用程序的确切指南 - 就在这里。它有关于静态文件以及如何使用eb配置的整个部分,您不需要了解有关nginx / apache等的任何信息。
Basically you should define container_commands
in your eb config, these commands will be executed after application deploy is finished. For example migrate
and collectstatic
, so this is an example of such section in eb config file:
基本上你应该在你的eb配置中定义container_commands,这些命令将在应用程序部署完成后执行。例如migrate和collectstatic,所以这是eb配置文件中此类部分的示例:
container_commands:
01_migrate:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py migrate --noinput"
leader_only: true
02_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py collectstatic --noinput"
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "iotd.settings"
"PYTHONPATH": "/opt/python/current/app/iotd:$PYTHONPATH"
"ALLOWED_HOSTS": ".elasticbeanstalk.com"
"aws:elasticbeanstalk:container:python":
WSGIPath: iotd/iotd/wsgi.py
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"
Pay attention to aws:elasticbeanstalk:container:python:staticfiles
part. And also you should define this part in your django settings file:
注意aws:elasticbeanstalk:container:python:staticfiles part。您还应该在django设置文件中定义此部分:
STATIC_ROOT = os.path.join(BASE_DIR, "..", "www", "static")
STATIC_URL = '/static/'
I copied this example almost entirely from article above, you should really check it, it's awesome.
我几乎完全从上面的文章中复制了这个例子,你应该检查一下,它真棒。
UPD: how to debug missing staticfiles. I usually do this (it involves sshing to your eb instance):
UPD:如何调试缺少的静态文件。我通常这样做(它涉及到你的eb实例sshing):
- Make sure that
django.contrib.staticfiles
is included in myINSTALLED_APPS
. - 确保django.contrib.staticfiles包含在我的INSTALLED_APPS中。
- Check in browser console url to missing file e.g.
/static/js/somefile.js
- 将浏览器控制台URL检入丢失的文件,例如/static/js/somefile.js
- Make sure in my django settings
STATIC_URL
is the same e.g./static/
. - 请确保在我的django设置中STATIC_URL是相同的,例如/静态的/。
- Check actual value in
STATIC_ROOT
and check that this folder actually contains your static files in production server. - 检查STATIC_ROOT中的实际值,并检查该文件夹是否实际包含生产服务器中的静态文件。
- Check that my eb config is pointing to correct folder (under your
option_settings
section in config) - 检查我的eb配置是否指向正确的文件夹(在config中的option_settings部分下)
Also you can try to collect static into /static
dir on your production server (it's where eb looks for them by default). If all of a sudden it starts working - it means that your setting failed to override default one and you should check where else it was defined.
您还可以尝试在生产服务器上收集静态到/静态目录(默认情况下,eb会查找它们)。如果它突然开始工作 - 这意味着您的设置无法覆盖默认设置,您应该检查它的定义位置。
I hope these steps will help you to find right direction.
我希望这些步骤能帮助您找到正确的方向。
#2
1
This is what worked for me.
这对我有用。
Remove any staticfiles directives from .config
files. Find Static Files section under the Software Configuration. The left column is each of your /static/
url. The right is your static folder relative to your parent directory.
从.config文件中删除所有staticfiles指令。 “软件配置”下的“查找静态文件”部分左列是/ static / url中的每一个。右边是相对于父目录的静态文件夹。
Make sure your STATIC_ROOT
setting matches the value on the right. Don't forget the trailing /
.
确保您的STATIC_ROOT设置与右侧的值匹配。不要忘记尾随/。
Pasting my settings anyway.
无论如何都要粘贴我的设置。
STATIC_URL = '/static/'
STATICFILES_DIRS = ('assets',)
STATIC_ROOT = os.path.join(BASE_DIR, '..', 'www', 'static')
And this is what my folder structure looks like relative to the settings files
这就是我的文件夹结构相对于设置文件的样子
project/
├── __init__.py
├── settings
│ ├── base.py
│ ├── __init__.py
│ ├── local.py
│ ├── production.py
wsgi.py
, urls.py
are located in project
folder. www
folder is one level above project
folder.
wsgi.py,urls.py位于项目文件夹中。 www文件夹是项目文件夹之上的一级。
Hope this saves your Sunday at least.
希望这至少可以节省你的星期天。