How do I host a Dash app on an Apache Server?
Question:
I’m new to hosting a Raspberry Pi Apache server and I have a simple Dash application I would like to host via a .wsgi file. Following Flask’s official documentation, this post’s answer, modwsgi’s documentation, and this guide for connecting Flask to Apache; I was able to get my files and structure to the state it is in below, but navigating to http://#.#.#.#/dash returns a 404, while http://#.#.#.# navigates to the default Apache page. I’m sure I am missing something and that it is relatively straight forward, I’m just not sure what. The apache error log has no errors or abnormalities.
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/apache2/sites-available/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
Answers:
As suspected, the answer was very simple, just not obvious in the resources I used. This walkthrough reminded me that I need to establish a virtual path between a2ensite
and my .config file using the command sudo /usr/sbin/a2ensite dash.conf
I am using Ubuntu 20.04 and have created a customized virtual enviroment.
First I ran:
sudo apt-get install libapache2-mod-wsgi-py3
While in the Folder
sudo chown -R www-data *
sudo chmod -R 775 *
For a multi page app, I tried to do as minimun as possible, i just copy and paste this code: https://dash.plotly.com/urls:
Then customize the files:
app.py
Located in a folder /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
Located in a folder /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>
Right after
a2ensite dash
wsgi.py
Located in a folder /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
To track problems while running I ran the commands:
tail -200 /var/log/apache2/error.log
systemctl status apache2.service
I’m new to hosting a Raspberry Pi Apache server and I have a simple Dash application I would like to host via a .wsgi file. Following Flask’s official documentation, this post’s answer, modwsgi’s documentation, and this guide for connecting Flask to Apache; I was able to get my files and structure to the state it is in below, but navigating to http://#.#.#.#/dash returns a 404, while http://#.#.#.# navigates to the default Apache page. I’m sure I am missing something and that it is relatively straight forward, I’m just not sure what. The apache error log has no errors or abnormalities.
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/apache2/sites-available/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
As suspected, the answer was very simple, just not obvious in the resources I used. This walkthrough reminded me that I need to establish a virtual path between a2ensite
and my .config file using the command sudo /usr/sbin/a2ensite dash.conf
I am using Ubuntu 20.04 and have created a customized virtual enviroment.
First I ran:
sudo apt-get install libapache2-mod-wsgi-py3
While in the Folder
sudo chown -R www-data *
sudo chmod -R 775 *
For a multi page app, I tried to do as minimun as possible, i just copy and paste this code: https://dash.plotly.com/urls:
Then customize the files:
app.py
Located in a folder /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
Located in a folder /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>
Right after
a2ensite dash
wsgi.py
Located in a folder /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
To track problems while running I ran the commands:
tail -200 /var/log/apache2/error.log
systemctl status apache2.service