python - 内部服务器错误 : Dash on Flask on AWS Elastic Beanstalk

标签 python amazon-web-services flask amazon-elastic-beanstalk

注意: Dash 是一个 python 包,用于由 plotly(网站 here)构建仪表板。 Flask 是一个用于构建 Python Web 框架的 Python 包(网站 here 和 AWS 教程 here)。我使用的是 python 3.6,所有包版本都可以在下面的 requirements.txt 中看到

问题:在我的 AWS EB 上启动我的 Dash 应用程序时出现错误。

当我在 127.0.0.1:8050 上本地运行时,我的 dash 可以正常工作。但是,当我 $ eb deploy 时,我在我的 AWS EB URL 上得到了这个:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

我的文件结构是这样的(eb-flask 是我的 flask 目录):

~/AWS/servers/flask-s
  |-- eb-virt                   }
  |    |-- bin                  }
  |    |-- include              } virtualenv files etc
  |    |-- lib                  }
  |    `-- pip-selfcheck.json   }
  |
  |-- eb-flask              <--- where I ran eb init
  |    |-- application.py
  |    |-- requirements.txt
  |    |-- .gitignore
  |    |-- .elasticbeanstalk
  `         `-- config.yml

当使用$ eb config时:

aws:elasticbeanstalk:container:python:
  NumProcesses: '1'
  NumThreads: '15'
  StaticFiles: /static/=static/
  WSGIPath: application.py

我的 application.py 代码几乎来自 https://dash.plot.ly/deployment作为测试。

import flask
import os
import dash
import dash_core_components as dcc
import dash_html_components as html

#app = dash.Dash(_name_)
#server = app.server

server = flask.Flask(_name_)
application = dash.Dash(_name_, server=server)

application.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})

application.layout = html.Div([
    html.H2('Hello World'),
    dcc.Dropdown(
        id='dropdown',
        options=[{'label': i, 'value': i} for i in ['LA', 'NYC', 'MTL']],
        value='LA'
    ),
    html.Div(id='display-value')
])

@application.callback(dash.dependencies.Output('display-value', 'children'),
              [dash.dependencies.Input('dropdown', 'value')])
def display_value(value):
return 'You have selected "{}"'.format(value)

if _name_ == '_main_':
    application.run_server(debug=True)
#app.run_server()

requirements.txt 显然是 pip freeze。此时只需要flask、dash和plotly所需的包:

certifi==2018.4.16
chardet==3.0.4
click==6.7
dash==0.21.1
dash-core-components==0.22.1
dash-html-components==0.10.1
dash-renderer==0.12.1
decorator==4.3.0
Flask==1.0.2
Flask-Compress==1.4.0
gunicorn==19.8.1
idna==2.6
ipython-genutils==0.2.0
itsdangerous==0.24
Jinja2==2.10
jsonschema==2.6.0
jupyter-core==4.4.0
MarkupSafe==1.0
nbformat==4.4.0
numpy==1.14.3
pandas==0.23.0
plotly==2.6.0
python-dateutil==2.7.3
pytz==2018.4
requests==2.18.4
six==1.11.0
traitlets==4.3.2
urllib3==1.22
Werkzeug==0.14.1

服务器端显然有问题。当我运行 (eb-virt) $ python application.py 时,我可以在本地访问该站点。我从 HTTP GET 得到这个响应:

(eb-virt): eb-flask $ python application.py 
 * Serving Flask app "application" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 190-847-760
127.0.0.1 - - [22/May/2018 18:41:30] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [22/May/2018 18:41:31] "GET /_dash-layout HTTP/1.1" 200 -
127.0.0.1 - - [22/May/2018 18:41:31] "GET /_dash-dependencies HTTP/1.1" 200 -
127.0.0.1 - - [22/May/2018 18:41:31] "POST /_dash-update-component HTTP/1.1" 200 -
127.0.0.1 - - [22/May/2018 18:41:31] "GET /favicon.ico HTTP/1.1" 200 -

然而,当我 $ eb deploy 时,我得到了前面提到的内部服务器错误。感谢您的帮助。

当我 enable logging ,我在本地得到 application.logger.addHandler(handler) AttributeError: 'Dash' object has no attribute 'logger'。部署时,日志文件中没有附加任何错误,只是保持空白。

最佳答案

我认为问题在于 application.run_server(debug=True) 方法将 localhost 作为默认主机。这意味着应用程序部署到 127.0.0.1,您只能从运行它的机器内部访问它。这解释了为什么您只能在本地访问该应用程序(无论是从您的计算机还是在实例内部)。部署到 Elastic Beanstalk 时,您需要将主机指定为 0.0.0.0 - 所有 IP 地址(不鼓励在生产中使用 debug=True 进行部署!):

if __name__ == '__main__':
    app.run_server(host="0.0.0.0")

检查 this GitHub 示例 Flask 应用程序 Beanstalk 部署。

关于python - 内部服务器错误 : Dash on Flask on AWS Elastic Beanstalk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50474000/

相关文章:

python - 如何调试这个缓冲文件队列?

python - tf.print() vs Python print vs tensor.eval()

amazon-web-services - 带有 git-remote-codecommit 的 AWS CodeCommit

linux - 将 ssh key 添加到 ssh-agent 失败 w/running agent,设置环境变量

python - flask-sqlalchemy 插入未映射

python - 如何将 linux 包正确安装到 Google 云平台项目中?

python - 根据两个 pandas DataFrame 之间的条件为新列分配值

amazon-web-services - AWS-ECS-如何在现有ECS(带有1个EC2)实例上重新部署更新的Docker镜像?

python - 使用 WTForms 和 Flask 预填充编辑表单

python - 如何使用 Flask-sqlalchemy 与额外数据创建多对多关联?