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

hivapdat  于 2023-02-11  发布在  Python
关注(0)|答案(2)|浏览(154)

我是一个托管Raspberry Pi Apache服务器的新手,我有一个简单的Dash应用程序,我想通过一个.wsgi文件来托管,下面是Flask的official documentationthis post的答案,modwsgi的documentationthis指南,用于连接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/apache 2/站点可用/短划线配置

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}

/变量/www/html/wsgi/破折号.wsgi

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

cl25kdpy1#

正如所猜测的那样,答案非常简单,只是在我使用的参考资料中不明显。This演练提醒我需要使用命令sudo /usr/sbin/a2ensite dash.confa2ensite和我的.config文件之间建立一个虚拟路径

biswetbf

biswetbf2#

我正在使用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
然后自定义文件:

    • 应用程序. 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')
    • 短划线配置**

位于/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

相关问题