yeziruo

利欲驱人万火牛 江湖浪迹一沙鸥

安装

这里直接使用包管理器提供的版本,不过建议大家使用pip来安装,会少一些坑:

(Debian/Ubuntu) apt-get install uwsgi uwsgi-plugin-python3

或使用pip安装

pip3 install uwsgi

试试看

[demo.py]
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

终端执行:

uwsgi --http-socket 0.0.0.0:8088 --manage-script-name --mount /=demo:app --plugin python3
(如果你是通过pip安装的,可不加 --plugin python3)
uwsgi --http-socket 0.0.0.0:8088 --manage-script-name --mount /[path]=demo:app --plugin python3

访问:

http://localhost:8088/[path]

部署

通常情况下我们的应用是按照工厂模式所编写的,使用我们需要新建一个文件来暴露出app对象:

from application import create_app
app = create_app()
if __name__ == "__main__":
    app.run()

下面来编写一个配置文件,便于配置修改:

[config.ini]

[uwsgi]
# 使用http协议
# http = 0.0.0.0:8081
# 指定工作用户(组)
uid = www-data
gid = www-data
# 主进程,由本进程派生子进程
master = true
# 工作目录
chdir = /var/application
# 插件(使用pip安装的可省略)
plugins = python3
# 入口文件
wsgi-file = app.py
# 指定入口文件的Flask对象
callable = app
# 指定uwsgi的socket路径
socket = /tmp/application.sock
# 进程数
processes = 2
# 线程数
threads = 4
# 缓冲区大小
buffer-size = 32768

配置Nginx

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    # location = /[path] { rewrite ^ /[path]/; }
    location / { try_files $uri @uwsgi; }
    location @uwsgi {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/application.sock;
    }
}

uwsgi后台运行,你可以直接在命令后加-d,但我这里是新建了个服务:

[/etc/systemd/system/uwsgi.service]

[Unit]
Description=uwsgi application

[Service]
User=www-data
Group=www-data
Type=simple
WorkingDirectory=/var/application
ExecStart=/usr/bin/uwsgi /var/application/config.ini

[Install]
WantedBy=multi-user.target

接下来就是设置开机启动了:

(sudo) systemctl enable uwsgi
(启动)
(sudo) systemctl start uwsgi

常见问题

  1. Nginx报5XX错误
    检查你的uwsgi的运行用户,务必保证你创建的socketNginx有权限读写的。
  2. uwsgino app loaded. going in full dynamic mode
    这个用pip安装的不会出现,需要添加python3插件

更多

https://uwsgi-docs.readthedocs.io/en/latest/
http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html

暂无评论

正在回复ID为 ??? 的评论

© 2013-2022 yeziruo. All Rights Reserved. Start5 theme by yeziruo.

CC BY-NC-SA 3.0 CN