• 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

  • 相关阅读:
    第二次冲刺阶段第九天
    第二次冲刺阶段第八天
    第二次冲刺阶段第七天
    学习进度条(十三)
    第二次冲刺阶段第六天
    团队冲刺(二)个人工作总结3
    学习进度表第十四周
    团队冲刺(二)个人工作总结2
    团队冲刺(二)个人工作总结1
    买书折扣问题
  • 原文地址:https://www.cnblogs.com/xwupiaomiao/p/15597323.html
Copyright © 2020-2023  润新知