apache服务器上部署的Dash因“Dash对象不可调用”而失败

2024-09-30 16:40:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将python dash应用程序部署到我的apache服务器上。我遵循了我所能找到的关于这个配置的少量信息(officials docsthis troubleshooting thread was a bit better)。当我访问网站时,页面返回一个500 Internal Server Error,在服务器错误日志中被描述为"Dash object not callable"。以下是配置文件:

>> cat /var/www/html/wsgi/dashGAF.wsgi
#!/usr/bin/python
import sys
sys.path.insert(0,"/home/ubuntu/dashboards/")
from dashGAF import app as application
>> cat /etc/apache2/sites-available/dash.conf 
WSGIDaemonProcess dashGAF user=ubuntu group=ubuntu home=/home/ubuntu threads=5
WSGIScriptAlias /dashGAF /var/www/html/wsgi/dashGAF.wsgi
    <Directory /home/ubuntu/dashboards>
        WSGIProcessGroup dashGAF
            WSGIApplicationGroup %{GLOBAL}
            WSGIScriptReloading On
        Require all granted
    </Directory>
>> cat dashGAF.py
# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets, routes_pathname_prefix='/dashGAF/')
server = app.server

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True, host='0.0.0.0')

当我在the_ip_address/dashGAF访问我的dash应用程序时,我得到一个500 Internal Server Error。检查error.log我看到:

[Sat Jun 20 04:42:59.502528 2020] [wsgi:error] [pid 6064:tid 140622992238336] [client 118.210.193.245:50042] mod_wsgi (pid=6064): Exception occurred processing WSGI script '/var/www/html/wsgi/dashGAF.wsgi'.
[Sat Jun 20 04:42:59.502675 2020] [wsgi:error] [pid 6064:tid 140622992238336] [client 118.210.193.245:50042] TypeError: 'Dash' object is not callable

如果您能帮助解决此问题,我们将不胜感激!此外,对配置文件的任何更改建议都会有所帮助

更多详情:

  • 我收到一个模块导入错误,如my/var/log/apache2/error.log中的以下内容:
[Sat Jun 20 03:38:58.556219 2020] [wsgi:error] [pid 583:tid 140297735726848] [client 118.210.193.245:55574]   File "/home/ubuntu/dashboards/dashGAF.py", line 2, in <module>
[Sat Jun 20 03:38:58.556265 2020] [wsgi:error] [pid 583:tid 140297735726848] [client 118.210.193.245:55574]     import dash
[Sat Jun 20 03:38:58.556285 2020] [wsgi:error] [pid 583:tid 140297735726848] [client 118.210.193.245:55574] ImportError: No module named dash

我通过做一个sudo pip install dash==1.13.2来解决这个问题

  • 我制作了所有的*.py和*.wsgi文件-rwxr-xr-x

  • 我使用sudo a2ensite dash.conf启用了站点配置,并使用udo systemctl reload apache2重新加载了配置

  • 我相信正在运行的python版本是python2.7(基于apacheerror.log);不确定具体如何指定2.7或3

  • 如果我将dashGAF.wsgi编辑为具有from dashGAF import server as application,则会出现500个内部错误,但服务器日志中包含以下详细信息:

[Sun Jun 21 06:33:28.181450 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221] mod_wsgi (pid=12237): Target WSGI script '/var/www/html/wsgi/dashGAF.wsgi' cannot be loaded as Python module.
[Sun Jun 21 06:33:28.181512 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221] mod_wsgi (pid=12237): Exception occurred processing WSGI script '/var/www/html/wsgi/dashGAF.wsgi'.
[Sun Jun 21 06:33:28.181545 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221] Traceback (most recent call last):
[Sun Jun 21 06:33:28.181577 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221]   File "/var/www/html/wsgi/dashGAF.wsgi", line 4, in <module>
[Sun Jun 21 06:33:28.181685 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221]     from dashGAF import server as application
[Sun Jun 21 06:33:28.181714 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221] ImportError: cannot import name server

可能在该位有一个有用的细节,它说“Target WSGI script'/var/www/html/WSGI/dashGAF.WSGI'不能作为Python模块加载。”??

  • 如果我编辑dashGAF.wsgi使其具有application = app.server,我会得到一个404 Not Foun:
from dashGAF import app
application = app.server

Tags: importclientappwsgiservervarhtmlwww
2条回答

我对是否应该回答自己的问题有点犹豫@埃米尔的回答是问题的一部分,但不是整个解决方案。我需要修复一些问题,其中大部分故障排除是由@GrahamDumpleton ongithub指导的。如果他愿意,我非常高兴他能提供答案

然而,以下是需要出现的问题和修复:

问题和修复:

  • 将Flask服务器定位为@emher建议的from dashGAF import server as application
  • 没有必要包含一个routes_pathname_prefix,它正在将仪表板解析为https://ip.address/dashGAF/dashGAF`
  • {}可以大大缩短`(见下文)
  • _dash-component-suites/dash_renderer/dash_renderer.dev.js的请求失败,我必须将requests_pathname_prefix='/dashGAF/'选项添加到我的app = dash.Dash行(see link on github

最终设置:

/etc/apache2/sites available/dash.conf

WSGIDaemonProcess dashGAF user=ubuntu group=ubuntu home=/home/ubuntu threads=5
WSGIScriptAlias /dashGAF /var/www/html/wsgi/dashGAF.wsgi

WSGIProcessGroup dashGAF
WSGIApplicationGroup %{GLOBAL}

/var/www/html/wsgi/dashGAF.wsgi

#!/usr/bin/python
import sys
sys.path.insert(0,"/home/ubuntu/dashboards/")
from dashGAF import server as application

仪表板/dashGAF.py 如上所述,但包括:

app = dash.Dash(__name__, external_stylesheets=external_stylesheets, requests_pathname_prefix='/dashGAF/')
server = app.server

通常,在wsgi脚本中,您的目标是Flask服务器,而不是Dash应用程序。也就是说,而不是

from dashGAF import app as application

应该是

from dashGAF import server as application

相关问题 更多 >