• docker部署flask项目


    一、环境准备
    docker中安装uwsgi报错:ERROR: Command errored out with exit status 1,需要安装以下组件
    ubuntu: apt install build-essential   #安装gcc
    ubuntu: apt install python3-dev
    ubuntu:apt install libssl-dev
    centos: yum install python3-devel
    centos: yum install gcc
    centos8:dnf install python38-devel.x86_64  #centos8安装python3.8
    centos8:dnf install openssl-devel

    二、安装uwsgi
    pip3 install uwsgi
    使用uwsgi运行flask websocket如报you need to build uWSGI with SSL support to use the websocket handshake api function !!!错误,需要使用以下命令安装uwsgi

    CFLAGS="-I/usr/local/opt/openssl/include" LDFLAGS="-L/usr/local/opt/openssl/lib" UWSGI_PROFILE_OVERRIDE=ssl=true pip3 install uwsgi -Iv

    三、flask.ini文件内容
    root@Ubuntu:~/southpark# cat flask.ini

    [uwsgi]
    master = true
    processes = 1
    workers = 2
    #threads = 2
    pidfile = /run/uwsgi.pid
    #http = 172.17.0.2:5000
    socket = 172.17.0.2:5000
    wsgi-file = southrun.py
    callable = app
    chmod-socket = 666
    touch-reload = /run/uwreload   #在/run目录下touch一个uwreload文件重启uwsgi
    vacuum = true
    http-websockets = 1
    gevent = 1000
    async = 30
    buffer-size = 32768
    daemonize = /var/log/uwsgi.log   #指定日志文件路径
    log-maxsize = 10485760   #指定日志文件大小
    reload-mercy = 1
    worker-reload-mercy = 1
    py-autoreload = 1   #修改py文件后uwsgi自动重启

    四、启动flask项目
    root@Ubuntu:~/southpark# uwsgi --ini flask.ini     #启动后日志在终端显示,配置文件中指定了日志文件路径,日志不在终端显示
    root@Ubuntu:~/southpark# uwsgi --ini flask.ini --daemonize /var/log/uwsgi.log      #后台运行,指定日志输出到uwsgi.log文件中

    五、nginx配置
    root@nginx:/etc/nginx/sites-enabled# cat uwsgi

    server {
        listen 80;
        listen [::]:80;
        server_name test.com www.test.com;
        rewrite ^(.*)$ https://$http_host$1 permanent;
    }
    
    server {
        listen 443 ssl;
        ssl_certificate /etc/nginx/crt/server.crt;
        ssl_certificate_key /etc/nginx/crt/server.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        client_max_body_size 500m;
    
        server_name test.com www.test.com;
        
        location / {
            #使用uwsgi socket连接
            #include uwsgi_params;;
            #uwsgi的ip与端口
            #uwsgi_pass 172.17.0.3:5000;
            #作为反向代理服务器时不要关闭代理重定向
    #        proxy_redirect off;    
            proxy_connect_timeout 600s;
            proxy_send_timeout   600;
            proxy_read_timeout   600;
            proxy_buffer_size    64k;
            proxy_buffers     16 32k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
            proxy_pass http://172.17.0.3:5000;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
        }
     
        #配置nginx支持websocket
        location /websocket {
            proxy_connect_timeout 600s;
            proxy_send_timeout   600;
            proxy_buffer_size    64k;
            proxy_buffers     16 32k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
            proxy_pass http://172.17.0.3:5000;
    #        proxy_redirect off;
            proxy_http_version 1.1;
            proxy_read_timeout 3600s;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }
    }

    六、uwsgi重启方式
    #使用简单选项 --reload
    uwsgi --reload /run/uwsgi.pid
    #使用选项touch-reload=/run/uwreload重启
    touch /run/uwreload       #在/run目录下touch一个uwreload文件重启uwsgi

    参考链接:
         https://www.cnblogs.com/lph-shares/p/8708786.html

  • 相关阅读:
    ajax原理及封装
    javascript 递归调用
    CSS的五种定位方式
    vue中iframe结合window.postMessage实现父子页面间的通信
    vue将文件流的形式的图形验证码转换成图片
    路由跳转打开新窗口,传递的参数不显示在地址栏
    js判断是否是ie浏览器、360浏览器兼容模式、QQ浏览器兼容模式、搜狗浏览器兼容模式,弹出提示使用别的浏览器打开
    vue项目中使用iframe嵌入静态页面,动态获取静态页面的高度赋值给iframe的高度
    vue项目中使用iview组件中的table插件实现表头文字居中,内容文字居左
    vue前端开发实现微信支付和支付宝支付
  • 原文地址:https://www.cnblogs.com/xwupiaomiao/p/15597323.html
Copyright © 2020-2023  润新知