部署在 Apache2 系统上的 Dash 应用程序永远不会让用户通过“正在加载...”,因为它似乎无法找到其 dash .js 文件

Cel*_*lso 4 apache plotly-dash

我正在尝试dash使用 Apache2 服务器和 来服务 (python3.8.2) 应用程序mod_wsgi,但应用程序停止在“正在加载...”屏幕上。服务器为 dash 的每个 .js 脚本返回 404。我在使用 dash 的内置本地服务器时没有遇到任何问题...但我并不想在本地提供此服务。

我希望得到一些关于我哪里出错以及如何启动和运行这个特定应用程序的建议。

症状

使用我的设置,我可以提供一个简单的 python“hello world”和一个简单的 Flask 应用程序。然而,当创建一个简单的“破折号”应用程序时......

[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (polyfill@7.v1_6_0m1598005008.8.7.min.js, line 0)
... followed by 12 more similar error messages...
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (dash_renderer.v1_6_0m1598005008.min.js, line 0)
[Error] ReferenceError: Can't find variable: DashRenderer
    Global Code (hazcurves:28:103)
Run Code Online (Sandbox Code Playgroud)

我的 apache 访问日志显示了一系列收到 404 的 GET 请求(这是一个示例):

... "GET /_dash-component-suites/dash_renderer/dash_renderer.v1_6_0m1598005008.min.js HTTP/1.1" 404 495 "http://efehr-dev.ethz.ch/hazcurves/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15"
Run Code Online (Sandbox Code Playgroud)

额外的观察结果

当我将我提供的 html 与正常运行的 Dash 应用程序中的 html 进行比较时,我注意到 .js 名称存在差异。看起来我的似乎被某种方式损坏了。

功能版本,来自https://dash-gallery.plotly.host/dash-wind-streaming/

[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (polyfill@7.v1_6_0m1598005008.8.7.min.js, line 0)
... followed by 12 more similar error messages...
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (dash_renderer.v1_6_0m1598005008.min.js, line 0)
[Error] ReferenceError: Can't find variable: DashRenderer
    Global Code (hazcurves:28:103)
Run Code Online (Sandbox Code Playgroud)

我的应用程序的页脚使请求看起来很混乱,查询参数被吸收到文件名中。

... "GET /_dash-component-suites/dash_renderer/dash_renderer.v1_6_0m1598005008.min.js HTTP/1.1" 404 495 "http://efehr-dev.ethz.ch/hazcurves/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15"
Run Code Online (Sandbox Code Playgroud)

现在,这些 .js 文件 [未加扰的版本] 确实存在,并且_dash-component-suites似乎映射到我的.local/lib/python3.8/site-packages文件夹。

所以,从我上面展示的内容来看,我已经.../dash_renderer/dash_renderer.min.js并且.../dash_html_components.min.js

我的系统:

一个干净的系统,安装了 Ubuntu 20.04.1 LTS、Apache 2.4 和 mod_wsgi。
Python 3.8.2 安装了以下相关软件包 [使用 sudo pip3] :

dash==1.14.0
dash-core-components==1.10.2
dash-html-components==1.0.3
dash-renderer==1.6.0
dash-table==4.9.0
Flask==1.1.2
Flask-Compres=1.5.0
Run Code Online (Sandbox Code Playgroud)

这是我的 app.wsgi:

# Excerpt from functioning app's "footer" section
<footer>
  <script id="_dash-config" type="application/json">{"url_base_pathname": null, "requests_pathname_prefix": "/dash-wind-streaming/", "ui": false, "props_check": false, "show_undo_redo": false}</script>

  # [CGR] then there are  several components here that are variations of the following
  <script src="/dash-wind-streaming/_dash-component-suites/dash_renderer/dash_renderer.min.js?v=1.0.0&m=1583853516"></script>

  <script id="_dash-renderer" type="application/javascript">
        var renderer = new DashRenderer();
        </script>
Run Code Online (Sandbox Code Playgroud)

我已经能够使用内置服务器运行它,并在本地服务器上提供服务。

我的站点配置文件:

<footer>
  <script id="_dash-config" type="application/json">{"url_base_pathname": null, "requests_pathname_prefix": "/", "ui": false, "props_check": false, "show_undo_redo": false, "suppress_callback_exceptions": false, "update_title": "Updating..."}</script>
   
  # [CGR] then there are  several components here that are variations of the following
  <script src="/_dash-component-suites/dash_html_components/dash_html_components.v1_0_3m1598005008.min.js"></script>
  <script src="/_dash-component-suites/dash_renderer/dash_renderer.v1_6_0m1598005008.min.js"></script>
  <script id="_dash-renderer" type="application/javascript">var renderer = new DashRenderer();</script>
        </footer>
Run Code Online (Sandbox Code Playgroud)

有些事情我承认尝试过

  • 烧瓶中的你好世界:工作正常
  • 达世币中的“Hello world”:失败
  • 在服务器配置中创建别名 alias _dash-component-suites/ .local/lib/python3.8/site-packages
  • 更改 request_pathnames_prefix :似乎没有效果,也不会显示在 html 输出中。这给了我 403 而不是 404。但后来我也注意到了名字的不一致。
  • app.css.config.serve_locally = True # 也尝试过 False : 没有明显效果
  • app.scripts.config.serve_locally = True #也尝试过 False :没有明显效果

在尝试从与这个问题无关的网站上强行提出建议后,我开始感觉自己像一只猴子敲击钥匙——这感觉,嗯,错误的(而且不安全)

最后的警告:这是我第一次尝试在服务器端管理应用程序。

Cel*_*lso 7

我很高兴地报告我最终弄清楚了,并且我的应用程序现在运行得很好。这是我学到的。

打电话时我必须注意以下几点,dash.Dash(...)而不是我写的内容,我需要:

dash.Dash(__name__, requests_pathname_prefix='/hazcurves/')
Run Code Online (Sandbox Code Playgroud)

重要的教训如下:

  1. 不要覆盖服务器变量。不知何故,我丢失了我创建的所有配置。
  2. requests_pathname_prefix需要匹配 URL 别名(以斜线开头结尾)。

转移注意力(实际上并不重要的事情):

  1. 首先创建一个 Flask 实例
  2. 与本地服务相关的配置
app.css.config.serve_locally = True  
app.scripts.config.serve_locally = True
Run Code Online (Sandbox Code Playgroud)

因此,以下是我感兴趣的三个文件的摘录:

mysite.conf/etc/apache2/sites-available/mysite.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/myusername/public_html


    # ---- Hazard Curves App ---- #
    # using WSGIProcessGroup within Location allows multiple apps to run
    # without interfering with each other

    WSGIDaemonProcess curves/wsgi.py processes=2 threads=15 display-name=%{GROUP}
    WSGIScriptAlias /curves /home/myusername/public_html/curves/wsgi.py
    <Location /curves>
        WSGIProcessGroup curves/wsgi.py
    </Location>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

wsgi.py~/public_html/curves/wsgi.py

#!/usr/bin/python3

from myapp import app
application = app.server
Run Code Online (Sandbox Code Playgroud)

myapp.py~/public_html/curves/myapp.py

import dash
import dash_html_components as html

app = dash.Dash(__name__, requests_pathname_prefix='/curves/')

app.title = "Some Test"
app.layout = html.Div([html.H6("Hello World")])

if __name__ == '__main__':
    app.run_server(port=8051, debug=True) # unimportant in this context
Run Code Online (Sandbox Code Playgroud)