NGINX Unit

Guillotina§

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

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

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

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

    警告

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

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

    from guillotina import configure
    from guillotina import content
    from guillotina import schema
    from guillotina.factory import make_app
    from zope import interface
    
    
    class IMyType(interface.Interface):
        textline = schema.TextLine()
    
    
    @configure.contenttype(
        type_name="MyType",
        schema=IMyType,
        behaviors=["guillotina.behaviors.dublincore.IDublinCore"],
    )
    class MyType(content.Resource):
        pass
    
    
    @configure.service(
        context=IMyType,
        method="GET",
        permission="guillotina.ViewContent",
        name="@textline",
    )
    async def textline_service(context, request):
        return {"textline": context.textline}
    
    
    application = make_app(
            settings={
                "applications": ["__main__"],
                "root_user": {"password": "root"},
                "databases": {
                    "db": {"storage": "DUMMY_FILE", "filename": "dummy_file.db",}
                },
            }
        )
    

    请注意,已删除所有服务器调用和导入。

  4. 运行以下命令,以便 Unit 可以访问 应用程序目录

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

    注意

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

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

  5. 接下来,准备 Unit 的 Guillotina 配置(对 typehomepath 使用实际值)

    {
        "listeners": {
            "*:80": {
                "pass": "applications/guillotina"
            }
        },
    
        "applications": {
            "guillotina": {
                "type": "python 3.Y",
                "path": "/path/to/app/",
                "home": "/path/to/app/venv/",
                "module": "asgi",
                "protocol": "asgi"
            }
        }
    }
    
  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 -XPOST --user root:root http://localhost/db \
           -d '{ "@type": "Container", "id": "container" }'
    
          {"@type":"Container","id":"container","title":"container"}
    
    $ curl --user root:root http://localhost/db/container
    
          {
              "@id": "http://localhost/db/container",
              "@type": "Container",
              "@name": "container",
              "@uid": "84651300b2f14170b2b2e4a0f004b1a3",
              "@static_behaviors": [
              ],
              "parent": {
              },
              "is_folderish": true,
              "creation_date": "2020-10-16T14:07:35.002780+00:00",
              "modification_date": "2020-10-16T14:07:35.002780+00:00",
              "type_name": "Container",
              "title": "container",
              "uuid": "84651300b2f14170b2b2e4a0f004b1a3",
              "__behaviors__": [
              ],
              "items": [
              ],
              "length": 0
          }