NGINX Unit

Quart§

要使用 Unit 运行使用 Quart Web 框架构建的应用

  1. 使用 Python 3.5+ 语言模块安装 Unit

  2. 创建一个虚拟环境来安装 Quart 的 PIP 包

    $ cd /path/to/app/
    $ python3 --version
          Python 3.Y.Z
    $ python3 -m venv venv
    $ source venv/bin/activate
    $ pip install quart
    $ deactivate
    

    警告

    使用与步骤 1 中的语言模块匹配的 Python 版本(在此示例中为 3.Y)创建虚拟环境,且步骤 5 中的应用 类型 必须 解析 为类似的匹配版本;Unit 不会从环境中推断它。

  3. 让我们尝试一个启用了 WebSocket 的 教程应用 版本,将其另存为 /path/to/app/asgi.py

    from quart import Quart, websocket
    
    app = Quart(__name__)
    
    @app.route('/')
    async def hello():
        return '<body><h1>Hello, World!</h1></body>'
    
    # Let's add WebSocket support to the app as well
    @app.websocket('/ws')
    async def ws():
        while True:
            await websocket.send('Hello, World!')
    
  4. 运行以下命令,以便 Unit 可以访问 应用程序目录

    # chown -R unit:unit /path/to/app/
    

    注意

    unit:unit 用户组对仅适用于 官方包、Docker 镜像 和一些 第三方存储库。否则,帐户名称可能有所不同;运行 ps aux | grep unitd 命令以确保。

    有关包括权限在内的更多详细信息,请参阅安全检查清单

  5. 接下来,准备Unit的Quart配置(对类型主页路径使用实际值)

    {
        "listeners": {
            "*:80": {
                "pass": "applications/quart"
            }
        },
    
        "applications": {
            "quart": {
                "type": "python 3.Y",
                "path": "/path/to/app/",
                "home": "/path/to/app/venv/",
                "module": "asgi",
                "callable": "app"
            }
        }
    }
    
  6. 上传更新的配置。假设上述JSON已添加到config.json

    # curl -X PUT --data-binary @config.json --unix-socket \
           /path/to/control.unit.sock http://localhost/config/
    

    注意

    控制套接字路径可能有所不同;运行unitd -h或参阅启动和关闭以了解详细信息。

    成功更新后,您的应用应可在侦听器的IP地址和端口上使用

    $ curl http://localhost
    
          <body><h1>Hello, World!</h1></body>
    
    $ wscat -c ws://localhost/ws
    
          < Hello, World!
          < Hello, World!
          < Hello, World!
          ...