avatar

AngYi

Aim for the stars, and beyond.

  • 首页
  • 分类
  • 标签
  • 归档
  • 相册
  • 关于我
Home 周末写了一个疫情可视化的项目
文章

周末写了一个疫情可视化的项目

Posted 2021-12-5 Updated 2023-10- 17
By AngYi
18~24 min read

项目预览

项目结构设计

项目结构

项目代码结构

.
├── app.py
├── requirements.txt
├── server.py
├── config.py
├── assets
│   └── chain_style.css
├── callbacks
│   ├── china.py
├── data
│   ├── crontab.sh
│   ├── json
│   │   ├── china_province.geojson
│   │   ├── city.json
│   │   └── province.json
│   ├── nameMap.json
│   └── tencent
│       ├── data_today.json
│       ├── global_data.json
│       ├── hist_data.json
│       └── last_data.json
├── models
│   ├── data_dump.py
│   ├── db.py
│   ├── log.log
│   ├── province_geojson.py
│   └── tencent_data.py
├── utils
│   ├── mapbox
└── views
    ├── china.py

具体代码可去[github](Flionay/Dash_Poltly_CovID at online (github.com))

云服务器部署

生成环境依赖requirement.txt

pip install pipreqs
pipreqs ./ --encoding=utf-8

Gunicorn

Gunicorn是一个unix上被广泛使用的高性能的Python WSGI UNIX HTTP Server。
和大多数的web框架兼容,并具有实现简单,轻量级,高性能等特点。

gunicorn 安装

pip install gunicorn

gunicorn 启动

Dash 应用启动,需要在APP中加入这一句,注明显式的server

server = app.server

然后利用命令行即可启动gunicorn多线程服务器:

gunicorn -w 8 -b 0.0.0.0:1919 app:server

Nginx

WSGI(Web Server Gateway Interface),即web服务器网关接口,是Web服务器和Web应用程序或框架之间的一种简单而通用的接口,它是一种协议,一种规范,专门用来解决众多Web服务器和Web应用程序或框架的兼容性问题。我们使用最常用的Gunicorn Server,和大多数的web框架兼容,并具有实现简单,轻量级,高性能等特点。

nginx是一个功能强大的反向代理服务器,我们使用nginx来转发gunicorn服务。为什么要在gunicorn之上再加层nginx呢?一方面nginx可以补充gunicorn在某些方面的不足,如SSL支持、高并发处理、负载均衡处理等,另一方面如果是做一个web网站,除了服务之外,肯定会有一些静态文件需要托管,这方面也是nginx的强项。

安装nginx使用sudo apt install即可。

使用nginx -t可查看nginx配置文件位置,以及检查nginx 配置是否正确。

修改/etc/nginx/sites-available\default文件为如下内容:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {    
		# First attempt to serve request as file, then    
		# as directory, then fall back to displaying a 404.    
		# try_files $uri $uri/ =404;    
		proxy_pass http://localhost:1919/;    
		# proxy_redirect off;    
		proxy_set_header Host $http_post;    
		proxy_set_header X-Real-IP $remote_addr;    
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		}

}	

nginx常用命令

使用systemctl启动,停止和重新启动Nginx

SystemD是最新的Ubuntu 18.04/16.04,CentOS 7/8和Debian 10/9发行版的系统和服务管理器。

每当您对Nginx配置进行更改时,都需要重新启动或重新加载Web服务器进程。执行以下命令重新启动Nginx服务:

sudo systemctl restart nginx

添加或编辑server配置文件代码片段时,与restart相比,更倾向于reload。仅当进行重大修改(例如更改端口或接口)时,才重新启动服务。在reload时,Nginx加载新配置,使用新配置启动新的工作进程,并正常关闭旧的工作进程。

运行以下命令以重新加载Nginx服务:

sudo systemctl reload nginx

Nginx也可以通过信号直接控制。例如,要重新加载服务,可以使用以下命令:

sudo /usr/sbin/nginx -s reload

要启动Nginx服务,请执行以下命令:

sudo systemctl start nginx

执行以下命令以停止Nginx服务:

sudo systemctl stop nginx

使用SysVinit启动,停止和重新启动Nginx

较早的(EOLed)版本的Ubuntu,CentOS和Debian使用init.d脚本来启动,停止和重新启动Nginx守护程序。

重新启动Nginx服务:

sudo service nginx restart

启动Nginx服务:

sudo service nginx start 

停止Nginx服务:

sudo service nginx stop
sudo /etc/init.d/nginx restart

supervisor

​ Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。

sudo apt install supervisor

supervisor管理gunicorn和nginx

首先创建/etc/supervisor/conf.d/gunicorn.conf文件,这是gunicorn服务的配置文件

[program:gunicorn]
command=/home/lighthouse/mambaforge-pypy3/bin/gunicorn -w 2 -b :1919 app:server
directory=/home/lighthouse/CovId
autostart=true
autorestart=true
user=lighthouse
redirect_stderr=true

接着还是同样的方法创建nginx的配置文件,/etc/supervisor/conf.d/nginx.conf,内容是

[program:nginx]
command=/usr/sbin/nginx -g 'daemon on;'
autostart=true
autorestart=true
user=root
redirect_stderr=true

nginx是需要root权限的,所以user应该设置成root。最后就可以重启supervisor了

sudo /etc/init.d/supervisor restart

重启成功后,我们来查看下gunicorn和nginx是否启动正常

supervisor常用命令说明

supervisorctl status        //查看所有进程的状态
supervisorctl stop nginx     //停止nginx
supervisorctl start nginx      //启动nginx
supervisorctl restart       //重启nginx
supervisorctl update        //配置文件修改后使用该命令加载新的配置
supervisorctl reload        //重新启动配置中的所有程序

注:把nginx换成all可以管理配置中的所有进程。直接输入supervisorctl进入supervisorctl的shell交互界面,此时上面的命令不带supervisorctl可直接使用。

项目
Dash
License:  CC BY 4.0
Share

Further Reading

Oct 15, 2022

Python算发开发项目模版

1. 前言这篇文章主要介绍如何编写有组织的、模块化的和可扩展的python代码。一个机器学习或者深度学习的工程项目,应该像软件开发一样,被视为一个整体。因此,这个项目应该具备这些东西:OOP,TypeChecking,Doc。模块之间遵循设计模式最根本的原则:低耦合高内聚。

May 16, 2022

利用FastAPI和Docker部署机器学习应用

Pexels 上的 Tom Fisk 拍摄的图片作为一名数据科学家,训练您的机器学习模型只是为客户提供解决方案的一部分。 除了生成和清理数据、选择和调整算法之外,您还需要交付和部署结果,以便在生产中使用。 这本身就是一个庞大的领域,具有不断发展的工具和标准。 在这篇文章中,我的目标是提供一份实用指南

Dec 5, 2021

周末写了一个疫情可视化的项目

一个Dash构建的疫情可视化网页。

OLDER

SSH远程连接服务器搭建Python环境全套解析

NEWER

【Flask开发】1. 安装与配置

Recently Updated

  • DeepSeek 创始人梁文峰采访:创新、人才与中国 AI 发展
  • 福州-厦门之行
  • 我有自己的摄影网站啦
  • 借助Ollama一键本地部署CodeGeex,让AI帮你打工
  • Dash 进阶技巧

Trending Tags

ssh linux matlab 感悟 读书 blog git python flask ML

Contents

©2025 AngYi. Some rights reserved.

Using the Halo theme Chirpy