zoukankan      html  css  js  c++  java
  • nginx+uWSGI+virtualenv+supervisor部署Flask程序

    前提准备

    1 首先先去了解一下WSGI相关的概念

    https://www.cnblogs.com/sunch/p/10711267.html
    https://segmentfault.com/a/1190000011365430#articleHeader6

    2 环境准备

    ubuntu16.0.4        # linux 发行版本, 详情见google
    python3                # python3环境
    nginx                # web服务器, 是客户端与uWSGI中间的代理(桥梁)            
                 # 更加安全,更好的处理处理静态资源,缓存功能,负载均衡,因此nginx的强劲性能,配合uWSGI服务器会更加安全,性能有保障。
    wWSGI # web服务器, 是nginx与flask程序中间的代理(桥梁) # 实现了WSGI协议、uwsgi协议、http协议等。 virtualenv # 虚拟环境, 为了将程序的运行环境与全局的环境隔离开 supervisor # 是基于 python 的任务管理工具, 用来自动运行各种后台任务, 这里用来运行uWSGI(而uWSGI用来代理运行flask程序) flask # 基于flask框架编写出来的web程序 (flask项目通过 python manage.py runserver 启动 # 请使用flask-script)

    流程

    1 Web浏览器发送HTTP请求给Nginx
    2 Nginx转发给uWSGI
    3 uWSGI将其按照WSGI规范进行处理
    4 uWSGI将HTTP请求给到Flask应用进行处理
    5 Flask应用对HTTP请求进行处理
      - 比如查询数据库获取信息
      - 比如将信息拼装成HTML网页

    6 Flask产生HTTP应答,按照WSGI规范给到uWSGI
    7 uWSGI将HTTP应答以TCP方式发给Nginx
    8 Nginx将HTTP应答以TCP方式发给Web浏览器

    一 安装Python环境

    1 先安装基础库,再安装python3

    sudo apt-get install -y python3-dev build-essential libssl-dev libffi-dev libxml2 libxml2-dev libxslt1-dev zlib1g-dev libcurl4-openssl-dev
    sudo apt-get install -y python3

    2 还需要安装pip3

    sudo apt-get install -y python3-pip

    二 上传项目

    1 先创建文件夹  /home/www

    mkdir /home/www

    2 先进到/home/www/

    cd /home/www

    3 把项目上传到此目录

    可以使用ftp工具, lrzsz 

    4 解压项目文件

    apt install unzip
    unzip movie_top.zip

    5 进入项目目录

    cd movie_top

    三 创建虚拟环境venv

    0 先安装virtualenv

    pip3 install virtualenv

    1 创建虚拟环境venv

    ### 指定python3版本

    sudo virtualenv -p /usr/bin/python3 venv

    2 启动虚拟环境

    source /home/www/movie_top/venv/bin/activate

    3 安装项目依赖

    pip3 install -r requirements.txt

    四 使用uWSGI对接Flask

    1 进入虚拟环境后安装uwsgi

    # 在虚拟环境下不需要使用 sudo ,因为virtualenv 是没有权限要求的。

    pip3 install uwsgi

    2 配置uwsgi

    vi wsgi.ini
    [uwsgi]
    
    # uwsgi 启动时所使用的地址与端口
    socket = 127.0.0.1:5000
    
    # 指向网站目录
    chdir = /home/www/movie_top
    
    # python 启动程序文件
    wsgi-file = manage.py 
    
    # python 程序内用以启动的 application 变量名
    callable = app 
    
    # 处理器数
    processes = 4
    
    # 线程数
    threads = 2
    
    #状态检测地址
    stats = 127.0.0.1:9191
    wsgi.ini

    五 安装和配置nginx

    1 安装nginx

    sudo apt-get install nginx

    2 配置 nginx

    a,先看一下nginx的配置文件在哪

    /usr/sbin/nginx -t

    b,覆盖默认配置文件

    vi /etc/nginx/sites-available/default
    server {
      listen  80;
      server_name  localhost;
    
      location / {
        include      uwsgi_params;
        uwsgi_pass   127.0.0.1:5000;                              # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
        uwsgi_param UWSGI_PYHOME /home/www/movie_top/venv;         # 指向虚拟环境目录
        uwsgi_param UWSGI_CHDIR  /home/www/movie_top;             # 指向网站根目录
        uwsgi_param UWSGI_SCRIPT manage:app;                     # 指定启动程序
      }
    }
    default

    3 重启 nginx

    随便选择一种方式重启

    方式一

    /usr/sbin/nginx -t           # 先测试一下配置文件有没有问题
    sudo service nginx restart # 重启   等同 --> systemctl restart nginx

    方式二

    /usr/sbin/nginx -t            # 先测试一下配置文件有没有问题
    systemctl stop nginx          # 先停止nginx
    systemctl start nginx          # 再启动nginx

    六 启动Flask程序

    方式一: 使用uwsgi启动程序

    方式一:

    uwsgi --ini /home/www/movie_top/wsgi.ini        # 相当于启动flask项目

    方式二:

    升级版:  后台运行uwsgi # 推荐使用supervisor方式启动项目, 同样可以在后台运行, 但supervisor支持高可用

    uwsgi -d --ini /home/www/movie_top/wsgi.ini

    方式二: 使用supervisor启动程序 (推荐)

    supervisor 是基于 python 的任务管理工具,用来自动运行各种后台任务,
    当然你也能直接利用 nohup 命令使任务自动后台运行,但如果要重启任务,每次都自己手动 kill 掉任务进程,这样很繁琐,而且一旦程序错误导致进程退出的话,系统也无法自动重载任务。

    1 安装supervior

    sudo apt-get install supervisor

    # 配置文件路径

    /etc/supervisor/supervisor.conf

    正常情况下我们并不需要去对其作出任何的改动,只需要添加一个新的配置文件放在下面的目录下即可

    /etc/supervisor/conf.d/

    2 配置supervisor

    下面我们建立一个用于启动uwsgi(其中flask程序又由uwsgi启动)的supervisor配置(命名为uwsgi_supervisor.conf)

    vi /etc/supervisor/conf.d/uwsgi_supervisor.conf
    [program:movie_top]
    # 启动命令入口
    command=/home/www/movie_top/venv/bin/uwsgi /home/www/movie_top/wsgi.ini
    
    # 命令程序所在目录
    directory=/home/www/movie_top
    
    #运行命令的用户名
    user=root
    
    # 高可用配置
    autostart=true
    autorestart=true
    
    #日志地址
    stdout_logfile=/home/www/movie_top/logs/uwsgi_supervisor.log     
    uwsgi_supervisor.conf

    3 启动服务

    3.1 启动前先创建日志文件

    不创建,好像会报错

    cd /etc/supervisor/conf.d/
    vi uwsgi_supervisor.conf
    mkdir /home/www/movie_top/logs
    vi /home/www/movie_top/logs/uwsgi_supervisor.log                # 随便写点内容

    3.2 启动supervisor

    方式一: (推荐吧)

    启动服务
    sudo service supervisor start
    
    # 终止服务
    sudo service supervisor stop

    方式二:

    supervisord -c /etc/supervisor/supervisord.conf
    
    # supervisorctl  进入交互模式
    
        start movie_top       # movie_top就是那个配置文件的项目名称
        stop  movie_top

    # 其他的使用

    一、添加好配置文件后
    
    二、更新新的配置到supervisord    
        supervisorctl update
    
    三、重新启动配置中的所有程序
        supervisorctl reload
    
    四、启动某个进程(program_name=你配置中写的程序名称)
        supervisorctl start program_name
    
    五、查看正在守候的进程
        supervisorctl
        
    六、停止某一进程 (program_name=你配置中写的程序名称)
        pervisorctl stop program_name
        
    七、重启某一进程 (program_name=你配置中写的程序名称)
        supervisorctl restart program_name
        
    八、停止全部进程
        supervisorctl stop all
        
    注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。
    其他命令
  • 相关阅读:
    广域网(ppp协议、HDLC协议)
    0120. Triangle (M)
    0589. N-ary Tree Preorder Traversal (E)
    0377. Combination Sum IV (M)
    1074. Number of Submatrices That Sum to Target (H)
    1209. Remove All Adjacent Duplicates in String II (M)
    0509. Fibonacci Number (E)
    0086. Partition List (M)
    0667. Beautiful Arrangement II (M)
    1302. Deepest Leaves Sum (M)
  • 原文地址:https://www.cnblogs.com/sunch/p/10711589.html
Copyright © 2011-2022 走看看