NGINX Unit
v. 1.23.0

Quart§

To run apps built with the Quart web framework using Unit:

  1. Install Unit with a Python 3.5+ language module.

  2. Create a virtual environment to install Quart’s PIP package:

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

    Warning

    Create your virtual environment with a Python version that matches the language module from Step 1 up to the minor number (3.Y in this example). Also, the app type in Step 5 must resolve to a similarly matching version; Unit doesn’t infer it from the environment.

  3. Let’s try a WebSocket-enabled version of a tutorial app, saving it as /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. Run the following command for each directory containing app code or data so Unit can access it:

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

    Note

    The unit:unit user-group pair is available only with official packages, Docker images, and some third-party repos. Otherwise, account names may differ; run the ps aux | grep unitd command to be sure.

    For further details, including permissions, see the security checklist.

  5. Next, prepare the Quart configuration for Unit (use real values for type, home, and path):

    {
        "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. Upload the updated configuration. Assuming the JSON above was added to config.json:

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

    Note

    The control socket path may vary; run unitd --help or see Startup and Shutdown for details.

    After a successful update, your app should be available on the listener’s IP address and port:

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