如何在Apache服务器上托管Dash应用程序?



我刚开始托管树莓派Apache服务器,我有一个简单的Dash应用程序,我想通过.wsgi文件托管。以下是Flask的官方文档,这篇文章的答案,modwsgi的文档,以及连接Flask到Apache的指南;我能够将我的文件和结构恢复到下面的状态,但是导航到http://#.#.#.#/dash返回404,而http://#.#.#.#导航到默认的Apache页面。我确信我遗漏了一些东西,而且它是相对直接的,我只是不确定是什么。apache的错误日志中没有错误和异常。

dash.py

from datetime import date
import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import data_controller as dc
external_stylesheets = ['/style.css']
data = dc.Data()
app = dash.Dash(__name__, external_stylesheets=external_stylesheets, requests_pathname_prefix='/dash/')
server = app.server
def serve_layout():
data = dc.Data()
today = date.today()
df = data.display_data()
return dcc.Tabs([
html.H1([children='Hello Apache!']),
dash_table.DataTable(columns=[{'name':i,'id':i} for i in df.columns],data=df.loc[:].to_dict('records'))
])
app.layout = serve_layout
if __name__ == '__main__':
app.run_server(debug=True, host='0.0.0.0')

/etc/输入/网站/dash.conf

WSGIDaemonProcess dash user=pi group=pi home=/home/pi/Documents/programming/ threads=5
WSGIScriptAlias /dash /var/www/html/wsgi/dash.wsgi
WSGIProcessGroup dash
WSGIApplicationGroup %{GLOBAL}

/var/www/html/wsgi/dash.wsgi

#!/usr/bin/python
import sys
sys.path.insert(0,'/home/pi/Documents/programming/dashboard/')
from dash import server as application

正如所料,答案非常简单,只是在我使用的资源中不明显。这个演练提醒我,我需要使用命令sudo /usr/sbin/a2ensite dash.conf

a2ensite和.config文件之间建立一个虚拟路径

我使用的是Ubuntu 20.04,并创建了一个自定义的虚拟环境。

首先我跑了:

sudo apt-get install libapache2-mod-wsgi-py3

在文件夹

sudo chown -R www-data *
sudo chmod -R 775 *

对于一个多页面的应用程序,我试图做尽可能少,我只是复制和粘贴这段代码:https://dash.plotly.com/urls:

然后自定义文件:

app.py

位于/var/www/html/dash文件夹

from dash import Dash, html, dcc
import dash
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = Dash(__name__, 
external_stylesheets=external_stylesheets,
use_pages=True,
requests_pathname_prefix='/Dash/')
server = app.server
app.scripts.config.serve_locally = True
app.css.config.serve_locally = True
app.layout = html.Div([
html.H1('Multi-page app with Dash Pages'),
html.Div(
[
html.Div(
dcc.Link(
f"{page['name']} - {page['path']}", href=page["relative_path"]
)
)
for page in dash.page_registry.values()
]
),
dash.page_container
])
if __name__ == '__main__':
app.run_server(debug=True,  port='8050')

dash.conf

位于/etc/apache2/sites-available文件夹

<VirtualHost *:80>
ServerName 10.0.10.10
ServerAdmin user@localhost
DocumentRoot /var/www/html/Dash
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIDaemonProcess Dash threads=5 user=www-data group=www-data python-path=/home/user/env/pweb/lib/python3.8/site-packages python-home=/home/user/env/pweb
WSGIScriptAlias /Dash /var/www/html/Dash/wsgi.py
<Files wsgi.py>
Require all granted
</Files>
<Directory /var/www/html/Dash>
AddHandler wsgi-script .py
WSGIProcessGroup Dash
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Allow from all
Options Indexes FollowSymLinks MultiViews ExecCGI
Require all granted
</Directory>
</VirtualHost>

之后
a2ensite dash

wsgi.py位于/var/www/html/dash

文件夹中
import sys
sys.path.insert(0,"/var/www/html/Dash/")
sys.path.insert(0,"/home/user/env/pweb/lib/python3.8/site-packages")
sys.path.insert(0,"/home/user/env/pweb")
from app import server as application

为了在运行时跟踪问题,我运行了以下命令:

tail -200 /var/log/apache2/error.log
systemctl status apache2.service

最新更新