Serves an arbitrary Flask app, which runs on localhost using uWSGI, using Nginx as a reverse proxy. The Flask app is installed either from a public git repository, or from PyPi. The role allows easily enabling authentication for your webapp, using SRAM and Single Sign-On.
The role allows installing the app in the different ways:
- From a public git repo
- From PyPi (specifying package name and version)
- From a requirements file on the target machine. The main advantages of this mode is that it allows you to specify specific dependencies, and that by committing a requirements file to your playbook's repo, you can use something like Dependabot to keep updating to the latest available version of your app.
- Note: when using this mode, you must still set the
flask_app_pip_pkgvariable to the name of the package that contains your app, so the role knows which of the dependencies in the requirements file is the app you want to run!
- Note: when using this mode, you must still set the
- Debian-like OS
- Nginx web server (installed via the SRC-Nginx component)
The user can define:
- Which Flask app to install, by either:
- setting a URL to the appropriate git repo.
- setting a PyPi package name
- Which version of Python to use.
- The location of dependency files, such as
requirements.txtorpyproject.toml, relative to the root of the git repo. These will automatically be installed - Whether authentication should be enabled for the webapp.
- A number of more advanced settings described below.
The role installs uv to ensure the appropriate Python version for the app is present, and to install the dependencies.
Serving Flask apps with uWSGI is usually done in one of two major ways:
- Set the
uWSGIwsgi-fileoption to the location ofapp.py, which contains a callableapp. - Set the
UWSGImoduleoption towsgi:app, indicating that the filewsgi.pyin the project contains anappcallable.
This role will assume the first method is to be used, except when flask_app_module_path is wsgi.py -- in that case, it will automatically set the module config option to wsgi:app.
The user can always modify the uWSGI config by setting the flask_app_uwsgi_config option (see below).
Many of the variables below are to configure the uWSGI settings. While individual settings have their own variable, note that you can always use either flask_app_uwsgi_config (pass in a dict) or flask_app_uwsgi_config_block (pass in a string) to configure arbitrary settings.
flask_app_name: String. Name of your application. This is descriptive: it determines, for instance, the directory under which files will be saved (e.g./var/www/myappname). Default:FlaskApp.flask_app_pip_pkg: String. Name of the PyPi package that contains the app. You must set either this variable, orflask_app_repo(but not both).flask_app_repo: String. Url to a public git repository containing your app. You must set either this variable, orflask_app_pip_pkg(but not both).flask_app_version: String. Git tag or version to use.flask_app_module_path: String. Path to the main.pyfile for your Flask app (oftenapp.pyorwsgi.py), relative to the repository root.flask_app_pip_requirements: String. Comma-separated list of paths to requirements file (.txt or .toml), the contents of which will be installed in the venv. When usingflask_app_pip_pkg, this will be assumed to be a file on the target machine. When usingflask_app_repo, it will be assumed to be a path inside the specified repository.flask_app_uwsgi_env: String. Environment variables to be added to the environment in which the app is run, e.g.FOO=bar BAZ=qux. Default:''.flask_app_python_version: String. The version of Python to serve the app with. Default:3.10.flask_app_num_workers: Integer. Number of processesuWSGIshould spawn to handle requests for the app. Default:2.flask_app_uwsgi_config: Dict. Settings to be added to theuWSGI.iniconfig file, according tokey=value.flask_app_uwsgi_config_block: String. Additional multiline.inistyle configuration foruWSGI. SeeuWSGIdocs for more info, and see the default.initemplate in the uWSGI role.flask_app_chdir: Boolean. Iftrue, the app will be executed from the directory containing the app's code.flask_app_proxy_auth: String. Set tosramto enable SRAM authorization / Single-Sign On authentication,basicfor HTTP basic auth, or omit for no authentication. Default:sram.flask_app_proxy_location: String. Nginx location definition determining which URL to serve the app at. Default:/, meaning the app is served athttp://yourworkspacefqdn. Can also be set to e.g./myappto serve athttp://yourworkspacefqdn/myapp.flask_app_proxy_config: Dict. Settings passed on to the nginx_reverse_proxy location definition. By default it contains standard settings for authentication, based on theflask_app_proxy_authvariable. You can override these settings using theauthandhtpasswdkeys in this dict. You can also add other directives that will end up in the nginx location definition: see the variables for nginx_reverse_proxy).flask_app_http_username: String. Optional, used ifflask_app_proxy_auth_basicis enabled to set the username for authentication.flask_app_http_password: String. Optional, used ifflask_app_proxy_auth_basicis enabled to set the password for authentication.
- Playbook flask_app
- Role nginx_reverse_proxy
- Role nginx_uwsgi
- Role require_src_nginx
2024-2025 Written by Dawa Ometto (Utrecht University)