Pyramid§
To run apps built with the Pyramid web framework using Unit:
Install Unit with a Python 3 language module.
Create a virtual environment to install Pyramid’s PIP package:
$ cd /path/to/app/ $ python3 --version Python 3.Y.Z $ python3 -m venv venv $ source venv/bin/activate $ pip install pyramid $ 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 apptype
in Step 5 must resolve to a similarly matching version; Unit doesn’t infer it from the environment.Note
Here,
$VENV
isn’t set because Unit picks up the virtual environment fromhome
in Step 5.Let’s see how the apps from the Pyramid tutorial run on Unit.
We modify the 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') app = config.make_wsgi_app() # serve(app, host='0.0.0.0', port=6543)
Note that we’ve dropped the server code; also, mind that Unit imports the module, so the
if __name__ == '__main__'
idiom would be irrelevant.To load the configuration, we place a
wsgi.py
file next todevelopment.ini
in/path/to/app/
:from pyramid.paster import get_app, setup_logging app = get_app('development.ini') setup_logging('development.ini')
This sets up the WSGI application for Unit; if the
.ini
’s pathname is relative, provide the appropriateworking_directory
in Unit configuration.Run the following command so Unit can access the application directory:
# 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.
Next, prepare the Pyramid configuration for Unit (use real values for
type
,home
, andpath
):{ "listeners": { "*:80": { "pass": "applications/pyramid" } }, "applications": { "pyramid": { "type": "python 3.Y", "working_directory": "/path/to/app/", "path": "/path/to/app/", "home": "/path/to/app/venv/", "module": "wsgi", "callable": "app" } } }
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 -h 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>