04.Python Dash网页开发:ubuntu服务器部署DASH网站(uWSGI+nginx)

Dash官网只有付费的部署方式❌ 我的简单理解,uWSGI去运行dash app并且与nginx通讯;nginx处理浏览器传来的请求并把需求给uWSGI

Python enviroment

代码语言:Python
复制
mkdir bioquest
vi ~/bioquest/dash.yaml
micromamba env create -n dash --file dash.yaml
micromamba activate dash
代码语言:YAML
复制
# vi ~/bioquest/dash.yaml
channels:
 - conda-forge
dependencies:
 - python=3.10
 - dash-bootstrap-components
 - numpy
 - pandas
 - plotly
 - dash-bootstrap-templates
 - scikit-learn
 - matplotlib
 - seaborn
 - uwsgi

directory

app还是上个推文的 03.Python Dash网页开发:多页面网站制作

把app全部文件目录复制到~/bioquest文件夹下

并且需要再app.py文件最后一行加上,因为wsgi从app.py中导入并运行的是server

代码语言:Python
复制
server = app.server

Create wsgi.py

代码语言:Python
复制
# vi ~/bioquest/wsgi.py
from app import server as application
if __name__ == '__main__':
    application.run()

Create index.ini

代码语言:Python
复制
# vi ~/bioquest/index.ini
[uwsgi]
module = wsgi
master = true
processes = 2
socket = index.scok
chmod-socket = 770
vacuum = true
die-on-term = true
py-autoreload = 1

Create index.service

代码语言:Python
复制
# sudo vi /etc/systemd/system/index.service
[Unit]
Description=uWSGI instance to serve index
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/bioquest
ExecStart=/home/ubuntu/micromamba/envs/dash/bin/uwsgi --force-cwd /home/ubuntu/bioquest --ini index.ini
StandardError=syslog
[Install]
WantedBy = multi-user.target

start and check

代码语言:Python
复制
sudo systemctl restart index.service
sudo systemctl status index.service

现在的~/bioquest文件夹

代码语言:YAML
复制
app.py  
index.ini  
index.sock  
pages  
__pycache__  
static  
wsgi.py
dash.yaml

Configure Nginx

修改Nginx默认配置文件default。腾讯云服务器绑定域名需要备案,比较麻烦,所以暂时还是不搞吧。

需要在腾讯云服务器开一个新端口1314,如果用80或433应该就不需要新开端口了,因为一般都会默认开通。

代码语言:shell
复制
sudo apt install nginx
sudo vi /etc/nginx/sites-available/default
代码语言:shell
复制
server {
    listen 1314;
    server_name 111.230.57.251;
    location / {
            include uwsgi_params; 
            uwsgi_pass unix:/home/ubuntu/bioquest/index.sock;          
     }
}

修改Nginx配置,把user改为启动用户即root

代码语言:YAML
复制
sudo vi /etc/nginx/nginx.conf
代码语言:shell
复制
user root;

启动所有服务

代码语言:shell
复制
sudo systemctl daemon-reload
sudo systemctl start index.service
sudo systemctl enable index
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status index
sudo service nginx restart

#访问和错误日志
#/var/log/nginx/access.log
#/var/log/nginx/error.log

现在就可以在浏览器中访问到DASH网站了http://111.230.57.251/

References