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

项目结构设计

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

项目代码结构

  1. .
  2. ├── app.py
  3. ├── requirements.txt
  4. ├── server.py
  5. ├── config.py
  6. ├── assets
  7. └── chain_style.css
  8. ├── callbacks
  9. ├── china.py
  10. ├── data
  11. ├── crontab.sh
  12. ├── json
  13. ├── china_province.geojson
  14. ├── city.json
  15. └── province.json
  16. ├── nameMap.json
  17. └── tencent
  18. ├── data_today.json
  19. ├── global_data.json
  20. ├── hist_data.json
  21. └── last_data.json
  22. ├── models
  23. ├── data_dump.py
  24. ├── db.py
  25. ├── log.log
  26. ├── province_geojson.py
  27. └── tencent_data.py
  28. ├── utils
  29. ├── mapbox
  30. └── views
  31. ├── china.py

具体代码可去github](https://github.com/Flionay/Dash_Poltly_CovID/tree/online))

云服务器部署

生成环境依赖requirement.txt

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

Gunicorn

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

gunicorn 安装

  1. pip install gunicorn

gunicorn 启动

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

  1. server = app.server

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

  1. 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文件为如下内容:

  1. ##
  2. # You should look at the following URL's in order to grasp a solid understanding
  3. # of Nginx configuration files in order to fully unleash the power of Nginx.
  4. # https://www.nginx.com/resources/wiki/start/
  5. # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
  6. # https://wiki.debian.org/Nginx/DirectoryStructure
  7. #
  8. # In most cases, administrators will remove this file from sites-enabled/ and
  9. # leave it as reference inside of sites-available where it will continue to be
  10. # updated by the nginx packaging team.
  11. #
  12. # This file will automatically load configuration files provided by other
  13. # applications, such as Drupal or Wordpress. These applications will be made
  14. # available underneath a path with that package name, such as /drupal8.
  15. #
  16. # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
  17. ##
  18. # Default server configuration
  19. #
  20. # Default server configuration
  21. #
  22. server {
  23. listen 80 default_server;
  24. listen [::]:80 default_server;
  25. # SSL configuration
  26. #
  27. # listen 443 ssl default_server;
  28. # listen [::]:443 ssl default_server;
  29. #
  30. # Note: You should disable gzip for SSL traffic.
  31. # See: https://bugs.debian.org/773332
  32. #
  33. # Read up on ssl_ciphers to ensure a secure configuration.
  34. # See: https://bugs.debian.org/765782
  35. #
  36. # Self signed certs generated by the ssl-cert package
  37. # Don't use them in a production server!
  38. #
  39. # include snippets/snakeoil.conf;
  40. root /var/www/html;
  41. # Add index.php to the list if you are using PHP
  42. index index.html index.htm index.nginx-debian.html;
  43. server_name _;
  44. location / {
  45. # First attempt to serve request as file, then
  46. # as directory, then fall back to displaying a 404.
  47. # try_files $uri $uri/ =404;
  48. proxy_pass http://localhost:1919/;
  49. # proxy_redirect off;
  50. proxy_set_header Host $http_post;
  51. proxy_set_header X-Real-IP $remote_addr;
  52. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  53. }
  54. }

nginx常用命令

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

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

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

  1. sudo systemctl restart nginx

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

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

  1. sudo systemctl reload nginx

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

  1. sudo /usr/sbin/nginx -s reload

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

  1. sudo systemctl start nginx

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

  1. sudo systemctl stop nginx

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

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

重新启动Nginx服务:

  1. sudo service nginx restart

启动Nginx服务:

  1. sudo service nginx start

停止Nginx服务:

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

supervisor

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

supervisor管理gunicorn和nginx

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

  1. [program:gunicorn]
  2. command=/home/lighthouse/mambaforge-pypy3/bin/gunicorn -w 2 -b :1919 app:server
  3. directory=/home/lighthouse/CovId
  4. autostart=true
  5. autorestart=true
  6. user=lighthouse
  7. redirect_stderr=true

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

  1. [program:nginx]
  2. command=/usr/sbin/nginx -g 'daemon on;'
  3. autostart=true
  4. autorestart=true
  5. user=root
  6. redirect_stderr=true

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

  1. sudo /etc/init.d/supervisor restart

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

supervisor常用命令说明

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

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