NGINX Unit
v. 1.19.0

Pyramid§

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

  1. Install Unit with a Python 3 language module.

  2. Create a virtual environment to install Pyramid’s PIP package (note that there’s no need to set $VENV):

    $ cd /path/to/app/
    $ python3 -m venv venv
    $ source venv/bin/activate
    $ pip install pyramid
    $ deactivate
    
  3. Let’s try a modified version of a tutorial app, saving it as /path/to/app/wsgi.py:

    from pyramid.config import Configurator
    from pyramid.response import Response
    
    
    def hello_world(request):
        return Response('<body><h1>Hello World!</h1></body>')
    
    with Configurator() as config:
        config.add_route('hello', '/')
        config.add_view(hello_world, route_name='hello')
        application = config.make_wsgi_app()
    

    Note that we’ve dropped the server code and altered the callable name.

  4. Set installation path permissions to secure access, for example:

    # chown -R unit_user:unit_group /path/to/app/
    

    Note the values to be used in Unit configuration.

  5. Next, put together the Pyramid configuration for Unit:

    {
        "listeners": {
            "*:80": {
                "pass": "applications/pyramid_app"
            }
        },
    
        "applications": {
            "pyramid_app": {
                "type": "python 3",
                "user": "unit_user",
                "group": "unit_group",
                "path": "/path/to/app/",
                "home": "/path/to/app/venv/",
                "module": "wsgi"
            }
        }
    }
    
  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 for details.

  7. 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>