如何把Dash项目(Python项目)部署到服务器上

参考链接:

  • basic-flask-website-tutorial
  • deploy-vps-dash-data-visualization

首先要有一个服务器:

  • 登录服务器
  • 更新
sudo apt-get update && sudo apt-get upgrade
  • 更新
sudo apt-get update
  • 安装Python 3.6
sudo apt-get install python3.6 python3.6-dev
  • 安装pip3.6
curl https://bootstrap.pypa.io/get-pip.py | sudo python3.6
  • 安装Apache
sudo apt-get install apache2 apache2-dev
  • 安装wsgi
pip3.6 install mod_wsgi
  • 获得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"
  • 将wsgi的配置文件写到 /etc/apache2/mods-available/wsgi.load (不会用nano的,control+X保存 然后yes退出)
nano /etc/apache2/mods-available/wsgi.load
  • 如果需要重启apache,那就重启apache(systemctl restart apache2或service apache2 restart)否则就
a2enmod wsgi
  • 安装flask
pip3.6 install Flask
  • 配置Apache configuration
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的文件夹的名字可以改成你想要的任何的名字。

  • enable the site
sudo a2ensite FlaskApp
  • 重启apache
service apache2 reload
  • 为项目写入wsgi配置?
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
  • 当前目录应为:/var/www/FlaskApp;其中FlaskApp.wsgi在该目录里;
  • 此时创建文件夹
mkdir FlaskApp
cd FlaskApp

创建结束之后,目录为/var/www/FlaskApp/FlaskApp;

  • 写入Dash项目
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)
  • 给项目提供www-data的权限,如果我们想要在这里存数据的话
chown -R www-data:www-data /var/www/FlaskApp
  • 重启apache
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

 

你可能感兴趣的:(python,Dash)