177 lines
6.1 KiB
Markdown
177 lines
6.1 KiB
Markdown
Ansible uwsgi-nginx-supervisor
|
|
==============================
|
|
|
|
An Ansible role to setup and manage a UWSGI app via supervisor, and served up on a NGINX web server. The goal of this
|
|
role is to make deployment of WSGI app as a simple and sane as possible. Additionally the role provides sane defaults
|
|
for logging and folder structure setup.
|
|
|
|
Requirements
|
|
------------
|
|
|
|
- aptitude or python-apt (required by apt tasks)
|
|
- python > 2.5 (required by ini_file tasks)
|
|
- Ansible >= 2.0
|
|
|
|
This role is designed to work against a modern Ubuntu system. (Tested on Ubuntu 14.04 and 16.04) It should
|
|
theoretically work on older versions of Ubuntu or Debian based systems.
|
|
|
|
This role relies on the [nginx-supervisor base role](https://bitbucket.org/dorianpula/ansible-nginx-supervisor).
|
|
|
|
Example Playbook
|
|
----------------
|
|
|
|
The simplest way to include the role in your playbook is to copy the below configuration. Remember to modify the
|
|
uwsgi_app_name, uwsgi_app_nginx_hostname and uwsgi_app_executable parameters especially.
|
|
|
|
- hosts: servers
|
|
sudo: yes
|
|
roles:
|
|
- { role: nginx-uwsgi-supervisor,
|
|
uwsgi_app_name: app,
|
|
uwsgi_app_nginx_hostname: app.domain.net,
|
|
uwsgi_app_port: 8080,
|
|
uwsgi_app_executable: "app.build:make_wsgi_app()" }
|
|
|
|
The role itself is very configurable. For exaple, if you prefer the location of the web application to refer to the
|
|
domain name rather than the default root path, then simply override the app_root_path variable with something like:
|
|
|
|
uwsgi_app_rooth_path: "{{ uwsgi_web_root_path }}/{{ uwsgi_app_nginx_hostname }}"
|
|
|
|
A comprehensive example can be found in the [Ansible rookeries role]
|
|
(https://bitbucket.org/dorianpula/ansible-rookeries) that uses this role as a base to deploy a Flask-based webapp.
|
|
|
|
Role Variables
|
|
--------------
|
|
|
|
There are a few crucial variables that you need to configure for your particular WSGI powered app. They are broken up
|
|
into sections and described below:
|
|
|
|
### App Settings
|
|
|
|
- uwsgi_app_name:
|
|
- The name of WSGI app to manage via this role.
|
|
- Used as a prefix through out the role.
|
|
- uwsgi_app_root_path:
|
|
- The path to the root folder of the app.
|
|
- Default: uwsgi_web_root_path/uwsgi_app_name_webapp
|
|
|
|
### NGINX
|
|
|
|
- uwsgi_app_nginx_hostname:
|
|
- The DNS hostname that the application serves.
|
|
- Default: localhost
|
|
- uwsgi_app_static_path:
|
|
- The path to the static elements of the site (templates, CSS, images, etc.)
|
|
- Default: uwsgi_app_root_path/uwsgi_app_name/static/
|
|
|
|
### UWSGI
|
|
|
|
- uwsgi_app_port:
|
|
- The port number that the WSGI runs on and accepts requests forwarded from NGINX.
|
|
- Default: 8001
|
|
- uwsgi_app_executable:
|
|
- The method executed by UWSGI to create a WSGI running app. Usually a WSGI factory method or module in the app.
|
|
- Default: app:make_wsgi_app()
|
|
- uwsgi_app_envs:
|
|
- (Optional) A dictionary of environment variables to pass into an app.
|
|
- Default: empty dictionary
|
|
|
|
### Virtual Environments
|
|
|
|
On occasion it is necessary to change the setup of the virtual environments on a system.
|
|
|
|
- uwsgi_virtualenv_root_path:
|
|
- Path to where the virtual environments are installed
|
|
- Default: uwsgi_web_root_path/virtualenvs
|
|
- uwsgi_app_venv:
|
|
- Path to the application's virtualenv.
|
|
- Default: uwsgi_virtualenv_root_path/uwsgi_app_name
|
|
|
|
### General Web
|
|
|
|
These variables are not as crucial to configure. They do give good defaults for configuring the system in a consistent,
|
|
POSIX/LSB-friendly and user-friendly manner. See the section on Default File Structure for more details.
|
|
|
|
- uwsgi_web_root_path:
|
|
- The root of the entire web app structure include configuration and logging.
|
|
- Default: /srv/www
|
|
- uwsgi_web_server_group:
|
|
- The user group responsible for starting, stopping and managing the web and app servers on the target machine.
|
|
- Default: www-data
|
|
|
|
Default File Structure
|
|
----------------------
|
|
|
|
By default the role will organize files in the following directory structure:
|
|
|
|
/srv/www
|
|
├── config
|
|
│ ├── nginx -> /etc/nginx
|
|
│ ├── supervisor -> /etc/supervisor
|
|
│ └── uwsgi
|
|
├── logs
|
|
│ ├── nginx -> /var/log/nginx
|
|
│ ├── supervisor -> /var/log/supervisor
|
|
│ └── uwsgi
|
|
├── uwsgi_app_webapp
|
|
│ ├── requirements.txt
|
|
│ └── app
|
|
└── virtualenvs
|
|
├── app
|
|
└── uwsgi
|
|
|
|
Internal Variables
|
|
------------------
|
|
|
|
The following variables are part of the internals of the role. However if you really want to, you can tweak them to
|
|
work with your setup:
|
|
|
|
- uwsgi_virtualenv_root_path:
|
|
- The common root directory of Python virtual environments associated with running the
|
|
UWSGI app + server.
|
|
- Default: /srv/www/virtualenvs/
|
|
- uwsgi_venv:
|
|
- The virtual environment where UWSGI is installed.
|
|
- Default: uwsgi_virtualenv_root/uwsgi
|
|
- uwsgi_app_venv:
|
|
- The virtual environment where the dependencies of the WSGI app is installed.
|
|
- Default: uwsgi_virtualenv_root/uwsgi_app_name
|
|
- uwsgi_app_nginx_conf:
|
|
- The filename of the NGINX configuration for the app.
|
|
- Default: uwsgi_app_name_nginx.conf
|
|
- uwsgi_config_path:
|
|
- The path to the UWSGI configurations.
|
|
- Default: /srv/www/config/uwsgi
|
|
- uwsgi_app_ini:
|
|
- The filename of the UWSGI INI configuration for the app.
|
|
- Default: app_name_uwsgi.ini
|
|
- uwsgi_service_name:
|
|
- The name of the UWSGI setup for the app according to supervisor.
|
|
- Default: app_name_uwsgi
|
|
|
|
License
|
|
-------
|
|
|
|
BSD
|
|
|
|
Author Information
|
|
------------------
|
|
|
|
Dorian Pula
|
|
|
|
- twitter: @dorianpula
|
|
- email: dorian.pula at amber-penguin.software.ca
|
|
- www: http://amber-penguin-software.ca
|
|
|
|
This role is a spin-off of the technology developed for the [Rookeries project] (http://rookeries.org/)
|
|
|
|
|
|
Repositories
|
|
------------
|
|
|
|
- Main: https://bitbucket.org/dorianpula/ansible-uwsgi-nginx-supervisor
|
|
- All development and issues are worked on this repo.
|
|
- Clone: https://github.com/dorianpula/ansible-nginx-uwsgi-supervisor
|
|
- A clone to work with Ansible Galaxy and Github
|
|
- nginx-supervisor base role: https://bitbucket.org/dorianpula/ansible-nginx-supervisor
|