参考链接:
首先要有一个服务器:
sudo apt-get update && sudo apt-get upgrade
sudo apt-get update
sudo apt-get install python3.6 python3.6-dev
curl https://bootstrap.pypa.io/get-pip.py | sudo python3.6
sudo apt-get install apache2 apache2-dev
pip3.6 install mod_wsgi
mod_wsgi-express module-config
//LoadModule wsgi_module "/usr/local/lib/python3.6/dist-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/usr"
nano /etc/apache2/mods-available/wsgi.load
a2enmod wsgi
pip3.6 install Flask
nano /etc/apache2/sites-available/FlaskApp.conf
ServerName xxx.xxx.xxx.xxx
ServerAdmin [email protected]
WSGIScriptAlias / /var/www/FlaskApp/FlaskApp.wsgi
Order allow,deny
Allow from all
ErrorLog ${APACHE_LOG_DIR}/FlaskApp-error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/FlaskApp-access.log combined
把上面的xxx.xxx.xxx.xxx换成你服务器的IP,除此之外,FlaskApp的文件夹的名字可以改成你想要的任何的名字。
sudo a2ensite FlaskApp
service apache2 reload
mkdir /var/www/FlaskApp
cd /var/www/FlaskApp
nano FlaskApp.wsgi
#!/usr/bin/python3.6
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import server as application
mkdir FlaskApp
cd FlaskApp
创建结束之后,目录为/var/www/FlaskApp/FlaskApp;
nano __init__.py
import dash
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div(children=[
html.H1(children='Dash Tutorials'),
dcc.Graph(
id='example',
figure={
'data': [
{'x': [1, 2, 3, 4, 5], 'y': [9, 6, 2, 1, 5], 'type': 'line', 'name': 'Boats'},
{'x': [1, 2, 3, 4, 5], 'y': [8, 7, 2, 7, 3], 'type': 'bar', 'name': 'Cars'},
],
'layout': {
'title': 'Basic Dash Example'
}
}
)
])
server = app.server
if __name__ == '__main__':
app.run_server(debug=True)
chown -R www-data:www-data /var/www/FlaskApp
service apache2 reload
然后直接访问IP,即可看到Dash项目。
这里他提到了一点 为什么要加一句话:server = app.server,
Notice that the only thing running the server is in the dunder bit at the end? Since it's WSGI that is actually running this app, this script wont be run as main. Thus, we need to add the following right above the
if __name__ == '__main__':
part:server = app.server
为了避免我忘记 Apache的错误日志的位置:/var/log/apache2