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文件或在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"; } }
# nginx配置wss代理 server { listen 443 ssl; server_name test.example.com; ssl_certificate /certs/cert.pem; ssl_certificate_key /certs/cert.key; location /api { proxy_pass http://127.0.0.1:8082; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location /ws { # 升级协议头websocket proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; # 转发到ws服务器 proxy_pass http://127.0.0.1:8082; # ------对请求头等的一些设置,可根据情况进行配置------ proxy_set_header Host $host; proxy_set_header X-Real_IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr:$remote_port; #关闭重定向 proxy_redirect off; client_max_body_size 50m; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; } }
六、uwsgi重启方式
#使用简单选项 --reload
uwsgi --reload /run/uwsgi.pid
#使用选项touch-reload = /run/uwreload重启,在/run目录下touch一个名为uwreload的文件或修改uwreload文件来重启uwsgi(只要uwreload文件大小有变化都会重新reload uwsgi)
touch /run/uwreload
参考链接:
     https://www.cnblogs.com/lph-shares/p/8708786.html
     https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WSGIquickstart.html
     https://zhuanlan.zhihu.com/p/88422780          # 使用gunicorn部署flask项目
     https://www.cnblogs.com/leiziv5/p/16630520.html           # FastAPI项目部署--nginx+gunicorn部署
     https://www.cnblogs.com/kadycui/p/16598342.html         # FastAPI+Gunicorn+Nginx部署(超详细)
     https://fastapi.tiangolo.com/zh/      # FastAPI框架
     https://www.runoob.com/flask/flask-tutorial.html      # flask教程
 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号