Changeset - 981584549895
[Not reviewed]
0 1 8
Branko Majic (branko) - 9 years ago 2015-05-10 00:48:54
branko@majic.rs
MAR-5: Adding the initial implementation of the WSGI website role.
9 files changed with 313 insertions and 1 deletions:
0 comments (0 inline, 0 general)
docs/rolereference.rst
Show inline comments
 
@@ -515,501 +515,631 @@ Here is an example configuration for setting-up LDAP server:
 
        - simpleSecurityObject
 
      userPassword: somepassword
 
      uid: john
 
      cn: John Doe
 
      sn: Doe
 

	
 

	
 
Prosody
 
-------
 

	
 
The ``prosody`` role can be used for setting-up Prosody, an XMPP server, on
 
destination machine.
 

	
 
The role implements the following:
 

	
 
* Sets-up the Prosody apt repository.
 
* Deploys XMPP TLS private key and certificate.
 
* Installs Prosody.
 
* Configures Prosody.
 
* Configures firewall to allow incoming connections to the XMPP server.
 

	
 
Prosody is configured as follows:
 

	
 
* Modules enabled: roster, saslauth, tls, dialback, posix, private, vcard,
 
  version, uptime, time, ping, pep, register, admin_adhoc, announce, legacyauth.
 
* Self-registration is not allowed.
 
* TLS is configured. Legacy TLS is available on port 5223.
 
* Client-to-server communication requires encryption (TLS).
 
* Authentication is done via LDAP. For setting the LDAP TLS truststore, see
 
  :ref:`LDAP Client <ldap_client>`.
 
* Internal storage is used.
 
* For each domain specified, a dedicated conference/multi-user chat (MUC)
 
  service is set-up, with FQDN set to ``conference.DOMAIN``.
 
* For each domain specified, a dedicated file proxy service will be set-up, with
 
  FQDN set to ``proxy.DOMAIN``.
 

	
 

	
 
Parameters
 
~~~~~~~~~~
 

	
 
**prosody_administrators** (list, mandatory)
 
  List of Prosody users that should be granted administrator privileges over
 
  Prosody. Each item is a string with value equal to XMPP user ID
 
  (i.e. ``john.doe@example.com``).
 

	
 
**prosody_tls_key** (string, mandatory)
 
  Path to file on Ansible host that contains the private key used for TLS for
 
  XMPP service. The file will be copied to directory ``/etc/ssl/private/``.
 

	
 
**prosody_tls_certificate** (string, mandatory)
 
  Path to file on Ansible host that contains the X.509 certificate used for TLS
 
  for SMTP service. The file will be copied to directory ``/etc/ssl/certs/``.
 

	
 
**prosody_domains** (list, mandatory)
 
  List of domains that are served by this Prosody instance. Each item is a
 
  string specifying a domain.
 

	
 
**prosody_ldap_server** (string, mandatory)
 
  Fully qualified domain name, hostname, or IP address of the LDAP server used
 
  for user authentication and listing.
 

	
 
**prosody_ldap_bind_dn** (string, mandatory)
 
  Distinguished name of LDAP user used for authenticating to the LDAP
 
  server. This user is used for looking-up the users available on the
 
  server. Users themselves authenticate via their own account.
 

	
 
**prosody_ldap_password** (string, mandatory)
 
  Password used for authenticating to the LDAP server.
 

	
 
**prosody_ldap_filter** (string, mandatory)
 
  LDAP filter used for obtaining a list of users available on the Prosody
 
  server. Two special strings can be used for specifying the user and domain,
 
  ``$user``, and ``$host`` within. These will be replaced with real values in
 
  the filter every time a user is looked-up.
 

	
 
**prosody_ldap_scope** (string, mandatory)
 
  Scope for performing the LDAP search for obtaining a list of users available
 
  on the Prosody server.
 

	
 
**prosody_ldap_tls** (boolean, mandatory)
 
  Specifies whether to use STARTTLS extension when connecting to the LDAP server
 
  or not.
 

	
 
**prosody_ldap_base** (string, mandatory)
 
  Base DN under which the lists of users available on the Prosody should be
 
  looked-up.
 

	
 

	
 
Examples
 
~~~~~~~~
 

	
 
Here is an example configuration for setting-up XMPP server using Prosody:
 

	
 
.. code-block:: yaml
 

	
 
  ---
 

	
 
  prosody_administrators:
 
    - john.doe@example.com
 
  # These are default key and certificate that generated during Prosody
 
  # installation.
 
  prosody_tls_key: /etc/prosody/certs/localhost.key
 
  prosody_tls_certificate: /etc/prosody/certs/localhost.crt
 
  prosody_domains:
 
    - example.com
 
  prosody_ldap_server: ldap.example.com
 
  prosody_ldap_bind_dn: cn=xmpp,ou=services,dc=example,dc=com
 
  prosody_ldap_password: xmpp
 
  # This would require that the memberof overlay is available on LDAP server
 
  # side.
 
  prosody_ldap_filter: '(&(memberOf=cn=xmpp,ou=groups,dc=example,dc=com)(mail=$user@$host))'
 
  prosody_ldap_scope: "onelevel"
 
  prosody_ldap_tls: "true"
 
  prosody_ldap_base: "ou=people,dc=example,dc=com"
 

	
 

	
 
Mail Server
 
-----------
 

	
 
The ``mail_server`` role can be used for setting-up a complete mail server
 
solution, which includes both SMTP and IMAP service, on destination machine.
 

	
 
Postfix is used SMTP, while Dovecot is used for IMAP.
 

	
 
The role implements the following:
 

	
 
* Installs rsync.
 
* Deploys IMAP/SMTP TLS private keys and certificates.
 
* Installs and configures Dovecot, Postfix, ClamAV, and ClamAV Milter.
 
* Purges Exim4 configuration (just in case).
 
* Installs SWAKS (utility for testing SMTP servers).
 
* Sets-up the necessary directories and files under Postfix chroot.
 
* Configures firewall to allow incoming connections to the mail server. This
 
  includes set-up of redirection from TCP port 26 to TCP port 25 (alternate SMTP
 
  to work around common network blocks).
 

	
 
Deployed services are configured as follows:
 

	
 
* Both Postfix and Dovecot look-up available domains, users, and aliases in
 
  LDAP.
 
* Incoming and outgoing mail is scanned with ClamAV (via ClamAV
 
  Milter). Infected mails are rejected.
 
* Mail is stored in directory ``/var/MAIL_USER/DOMAIN/USER``, using ``Maildir``
 
  format.
 
* TLS is required for user log-ins for both SMTP and IMAP.
 
* RBL's are used for combating spam (if any is specified in configuration, see
 
  below).
 

	
 
Both Postfix and Dovecot expect a specific directory structure in LDAP when
 
doing look-ups:
 

	
 
* Postfix will log-in to LDAP as user
 
  ``cn=postfix,ou=services,MAIL_LDAP_ROOT_DN``.
 
* Dovecot will log-in to LDAP as user
 
  ``cn=dovecot,ou=services,MAIL_LDAP_ROOT_DN``.
 
* Domain entries need to be available as
 
  ``dc=DOMAIN,ou=domains,ou=mail,ou=services,MAIL_LDAP_ROOT_DN``.
 
* Alias entries need to be available as
 
  ``cn=ALIAS,ou=aliases,ou=mail,ou=services,MAIL_LDAP_ROOT_DN``.
 
* User entries are read from sub-tree (first-level only)
 
  ``ou=people,MAIL_LDAP_ROOT_DN``. Query filter used for finding users is
 
  ``(&(mail=%s)(memberOf=cn=mail,ou=groups,MAIL_LDAP_ROOT_DN))``. This allows
 
  group-based granting of mail services to users.
 

	
 

	
 
Parameters
 
~~~~~~~~~~
 

	
 
**mail_ldap_url** (string, mandatory)
 
  LDAP URL that should be used for connecting to the LDAP server for doing
 
  domain/user look-ups.
 

	
 
**mail_ldap_tls_truststore** (string, mandatory)
 
  Path to TLS truststore used for verifying the LDAP certificate. Should be in
 
  PEM format.
 

	
 
**mail_ldap_root_dn** (string, mandatory)
 
  Root DN in LDAP under where the entries (domains, users, aliases) can be
 
  found.
 

	
 
**mail_ldap_postfix_password** (string, mandatory)
 
  Password for authenticating the Postfix LDAP user.
 

	
 
**mail_ldap_dovecot_password** (string, mandatory)
 
  Password for authenticating the Dovecot LDAP user.
 

	
 
**mail_user** (string, mandatory)
 
  Name of the user that owns all the mail files.
 

	
 
**mail_user_uid** (integer, mandatory)
 
  UID of the user that owns all the mail files.
 

	
 
**mail_user_gid** (integer, mandatory)
 
  GID of the user that owns all the mail files.
 

	
 
**imap_tls_certificate** (string, mandatory)
 
  Path to file on Ansible host that contains the X.509 certificate used for TLS
 
  for IMAP and ManageSieve services. The file will be copied to directory
 
  ``/etc/ssl/certs/``.
 

	
 
**imap_tls_key** (string, mandatory)
 
  Path to file on Ansible host that contains the private key used for TLS for
 
  IMAP and ManageSieve services. The file will be copied to directory
 
  ``/etc/ssl/private/``.
 

	
 
**smtp_tls_certificate** (string, mandatory)
 
  Path to file on Ansible host that contains the X.509 certificate used for TLS
 
  for SMTP service. The file will be copied to directory ``/etc/ssl/certs/``.
 

	
 
**smtp_tls_key** (string, mandatory)
 
  Path to file on Ansible host that contains the private key used for TLS for
 
  SMTP service. The file will be copied to directory ``/etc/ssl/private/``.
 

	
 
**imap_folder_separator** (string, mandatory)
 
  Character used for separating the IMAP folders when clients are requesting
 
  listing from the server. Usually either slash(``/``) or dot(``.``).
 

	
 
**smtp_rbl** (list, mandatory)
 
  List of RBLs to use for detecting servers which send out spam. Each item is a
 
  string resembling the RBL domain.
 

	
 
**mail_postmaster** (string, mandatory)
 
  Mail address to use for the postmaster account in Dovecot.
 

	
 
**smtp_allow_relay_from** (list, mandatory)
 
  List of networks from which mail relaying is allowed even without
 
  authentication. Each item in the list is a string defining a network. The
 
  format must be compatible with Postfix ``mynetworks`` setting (for example:
 
  ``192.168.1.0/24``, ``myhost.example.com`` etc).
 

	
 

	
 
Examples
 
~~~~~~~~
 

	
 
Here is an example configuration for setting-up XMPP server using Prosody:
 

	
 
.. code-block:: yaml
 

	
 
  ---
 

	
 
  mail_ldap_url: ldap://ldap.example.com/
 
  mail_ldap_tls_truststore: /etc/ssl/certs/truststore.pem
 
  mail_ldap_root_dn: dc=example,dc=com
 
  mail_ldap_postfix_password: postfix
 
  mail_ldap_dovecot_password: dovecot
 

	
 
  mail_user: vmail
 
  mail_user_uid: 5000
 
  mail_user_gid: 5000
 

	
 
  imap_tls_certificate: ~/tls/mail.example.com_imap.pem
 
  imap_tls_key: ~/tls/mail.example.com_imap.key
 
  smtp_tls_certificate: ~/tls/mail.example.com_smtp.pem
 
  smtp_tls_key: ~/tls/mail.example.com_smtp.key
 
  imap_folder_separator: /
 
  smtp_rbl:
 
    - bl.spamcop.net
 
    - zen.spamhaus.org
 
  mail_postmaster: postmaster@example.com
 

	
 
  smtp_allow_relay_from:
 
    - ldap.example.com
 
    - xmpp.example.com
 

	
 

	
 
Mail Forwarder
 
--------------
 

	
 
The ``mail_forwarder`` role can be used for setting-up a local SMTP server for
 
sending out mails and receiving mails for local users. The SMTP server is
 
provided by Postfix.
 

	
 
SMTP service on server set-up this way is not meant to be exposed to the
 
Internet directly, and should receive delivery failures from the relay server
 
instead.
 

	
 
The role implements the following:
 

	
 
* Installs and configures Postfix.
 
* Purges Exim4 configuration (just in case).
 
* Sets-up aliases for the local recipients.
 
* Installs SWAKS (utility for testing SMTP servers).
 

	
 
Postfix is configured as follows:
 

	
 
* Local destinations are set-up.
 
* A relay host is set.
 
* TLS is enforced for relaying mails, with configurable truststore for server
 
  certificate verification.
 

	
 

	
 
Parameters
 
~~~~~~~~~~
 

	
 
**local_mail_aliases** (dictionary, mandatory)
 
  Dictionary defining the local aliases. Aliases defined this way will either be
 
  appended to default aliases on the server, or replace the existing entries (if
 
  the alias/recipient is already present). Keys in the dictionary are the local
 
  recipients/aliases, while the value provided should be a space-separated list
 
  of mail addresses (or local users) where the mails should be forwarded.
 

	
 
**smtp_relay_host** (string, mandatory)
 
  SMTP server via which the mails are sent out for non-local recipients.
 

	
 
**smtp_relay_truststore** (string, mandatory)
 
  Path to the file containing full X.509 CA certificate chain used for
 
  validating the server certificate presented by the relay server.
 

	
 

	
 
Examples
 
~~~~~~~~
 

	
 
Here is an example configuration for setting-up the mail forwarder:
 

	
 
.. code-block:: yaml
 

	
 
  ---
 

	
 
  # All mails sent to local user root will be forwarded to external account as
 
  # well.
 
  local_mail_aliases:
 
    root: "root john.doe@example.com"
 

	
 
  smtp_relay_host: mail.example.com
 

	
 
  smtp_relay_truststore: /etc/ssl/certs/example_ca_chain.pem
 

	
 

	
 
Web Server
 
----------
 

	
 
The ``web_server`` role can be used for setting-up a web server on destination
 
machine.
 

	
 
The role is supposed to be very lightweight, providing a basis for deployment of
 
web applications.
 

	
 
The role implements the following:
 

	
 
* Installs and configures nginx with a single, default vhost with a small static
 
  index page.
 
* Deploys the HTTPS TLS private key and certificate (for default vhost).
 
* Configures firewall to allow incoming connections to the web server.
 
* Installs and configures virtualenv and virtualenvwrapper as a common base for
 
  Python apps.
 
* Installs and configures PHP FPM as a common base for PHP apps.
 

	
 

	
 
Parameters
 
~~~~~~~~~~
 

	
 
**https_tls_key** (string, mandatory)
 
  Path to file on Ansible host that contains the private key used for TLS for
 
  HTTPS service. The file will be copied to directory ``/etc/ssl/private/``.
 

	
 
**https_tls_certificate** (string, mandatory)
 
  Path to file on Ansible host that contains the X.509 certificate used for TLS
 
  for HTTPS service. The file will be copied to directory ``/etc/ssl/certs/``.
 

	
 
**web_default_title** (string, mandatory)
 
  Title for the default web page shown to users (if no other vhosts were matched).
 

	
 
**web_default_message** (string, mandatory)
 
  Message for the default web page shown to users (if no other vhosts were
 
  matched).
 

	
 

	
 
Examples
 
~~~~~~~~
 

	
 
Here is an example configuration for setting-up web server:
 

	
 
.. code-block:: yaml
 

	
 
  ---
 

	
 
  https_tls_key: "{{ inventory_dir }}/tls/web.example.com_https.key"
 
  https_tls_certificate: "{{ inventory_dir }}/tls/web.example.com_https.pem"
 

	
 
  web_default_title: "Welcome to Example Inc."
 
  web_default_message: "You are attempting to access the web server using a wrong name or an IP address. Please check your URL."
 

	
 

	
 
PHP Website
 
--------
 
-----------
 

	
 
The ``php_website`` role can be used for setting-up a website powered by PHP on
 
destination machine.
 

	
 
This role is normally not supposed to be used directly, but should instead serve
 
as the basis for writing website-specific roles. Therefore the role is written
 
in quite generic way, allowing the integrator to write his/her own logic for
 
deploying the necessary PHP applications, while still reusing a common base and
 
reducing the workload.
 

	
 
The role implements the following:
 

	
 
* Creates a dedicated user/group for running the PHP scripts.
 
* Creates a base directory where the website-specific code and data should be
 
  stored at.
 
* Adds nginx to website's group, so nginx could read the necessary files.
 
* Adds website administrator to website's group, so administrator could manage
 
  the code and data.
 
* Installs additional packages required for running the role (as configured).
 
* Configures PHP FPM and nginx to serve the website.
 

	
 
The role is implemented with the following layout/logic in mind:
 

	
 
* Website users are named after the ``FQDN`` (fully qualified domain name) of
 
  website, in format of ``web-ESCAPEDFQDN``, where ``ESCAPEDFQDN`` is equal to
 
  ``FQDN`` where dots have been replaced by underscores (for example,
 
  ``web-cloud_example_com``).
 
* All websites reside within a dedicated sub-directory in ``/var/www``. The
 
  sub-directory name is equal to the ``FQDN`` used for accessing the
 
  website. Owner of the directory is set to be the application administrator,
 
  while group is set to be the website group. Additionally, ``SGID`` bit is set
 
  on the directory. This allows admin, with correct umask, to create necessary
 
  files and directories that should be readable (and eventually writeable) by
 
  the website user (running the PHP scripts) without having to become root.
 
* All files placed in the website directory should be either created there
 
  directly, or copied to the directory in order to make sure the ``SGID`` gets
 
  honored. **Do not move the files, the permissions will not be set correctly.**
 
* Within the website directory, nginx/php5-fpm will expect to find the relevant
 
  files within the htdocs sub-directory (this can be symlink too).
 
* nginx communicates with PHP FPM over a dedicated Unix socket for each website.
 

	
 

	
 
Parameters
 
~~~~~~~~~~
 

	
 
**admin** (string, mandatory)
 
  Name of the operating system user in charge of maintaining the website. This
 
  user is capable of making modifications to website configuration anda data
 
  stored within the website directory.
 

	
 
**deny_files_regex** (list, optional)
 
  List of regular expressions for matching files/locations to which the web
 
  server should deny access. This is useful to block access to any sensitive
 
  files that should not be served directly by the web server. The format must be
 
  compatible with regular expressions used by ``nginx`` for ``location ~``
 
  syntax.
 

	
 
**fqdn** (string, mandatory)
 
  Fully-qualified domain name where the website is reachable. This value is used
 
  for calculating the user/group name for dedicated website user, as well as
 
  home directory of the website user (where data/code should be stored at).
 

	
 
**php_rewrite_url** (string, optional)
 
  If implementing some form of clean URL schema, this parameter can be used for
 
  defining how the clean URLs should be mapped onto actual PHP scripts. When
 
  specifying this parameter, one special variable is available - ``$suburi``
 
  (which is the URI requested by HTTP client, usually in clean URL form). This
 
  is in addition to any other variables provided out of the box by ``nginx``
 
  (like ``$args`` and such).
 

	
 
**rewrites** (list, optional)
 
  A list of rewrite rules that are applied to incoming requests. Each element of
 
  the list should be a string value compatible with the format of ``nginx``
 
  option ``rewrite``. The keyword ``rewrite`` itself should be omitted, as well
 
  as trailing semi-colon (``;``).
 

	
 
**packages** (list, optional)
 
  A list of additional packages to install for this particular PHP
 
  appliction. This is usually going to be different PHP extensions.
 

	
 
**uid** (integer, mandatory)
 
  UID/GID (they are set-up to be the same) of the dedicated website
 
  user/group.
 

	
 

	
 
Examples
 
~~~~~~~~
 

	
 
Here is an example configuration for setting-up a (base) PHP website (for running
 
``ownCloud`` application):
 

	
 
.. code-block:: yaml
 

	
 
    ---
 

	
 
    - role: php_website
 
      fqdn: cloud.example.com
 
      uid: 2001
 
      admin: admin
 
      php_rewrite_url: /index.php
 
      rewrites:
 
        - ^/\.well-known/host-meta /public.php?service=host-meta
 
        - ^/\.well-known/host-meta\.json /public.php?service=host-meta-json
 
        - ^/\.well-known/carddav /remote.php/carddav/ redirect
 
        - ^/\.well-known/caldav /remote.php/caldav/ redirect
 
        - ^/apps/calendar/caldav\.php /remote.php/caldav/
 
        - ^/apps/contacts/carddav\.php /remote.php/carddav/
 
        - ^/remote/(.*) /remote.php
 
      deny_files_regex:
 
        - ^(\.|autotest|occ|issue|indie|db_|console|build/|tests/|config/|lib/|3rdparty/|templates/).*
 
      packages:
 
        # For ownCloud
 
        - php5-gd
 
        - php5-json
 
        - php5-mysql
 
        - php5-curl
 

	
 

	
 
WSGI Website
 
------------
 

	
 
The ``wsgi_website`` role can be used for setting-up a website powered by Python
 
on destination machine. The website needs to use the WSGI specification for
 
making the Python web application(s) available.
 

	
 
This role is normally not supposed to be used directly, but should instead serve
 
as the basis for writing website-specific roles. Therefore the role is written
 
in quite generic way, allowing the integrator to write his/her own logic for
 
deploying the necessary Python applications/packages, while still reusing a
 
common base and reducing the workload.
 

	
 
The role implements the following:
 

	
 
* Creates a dedicated user/group for running the WSGI application.
 
* Creates a base directory where the website-specific code and data should be
 
  stored at.
 
* Adds nginx to website's group, so nginx could read the necessary files.
 
* Adds website administrator to website's group, so administrator could manage
 
  the code and data.
 
* Installs additional packages required for running the role (as configured).
 
* Sets-up a dedicated Python virtual environment for website.
 
* Install Gunicorn in Python virtual environment.
 
* Installs additional packages required for running the role in Python virtual
 
  environment (as configured).
 
* Configures systemd to run the website code (using Gunicorn)
 
* Configures nginx to serve the website (static files served directly, requests
 
  passed on to Gunicorn).
 

	
 
The role is implemented with the following layout/logic in mind:
 

	
 
* Website users are named after the ``FQDN`` (fully qualified domain name) of
 
  website, in format of ``web-ESCAPEDFQDN``, where ``ESCAPEDFQDN`` is equal to
 
  ``FQDN`` where dots have been replaced by underscores (for example,
 
  ``web-wiki_example_com``).
 
* All websites reside within a dedicated sub-directory in ``/var/www``. The
 
  sub-directory name is equal to the ``FQDN`` used for accessing the
 
  website. Owner of the directory is set to be the application administrator,
 
  while group is set to be the website group. Additionally, ``SGID`` bit is set
 
  on the directory. This allows admin, with correct umask, to create necessary
 
  files and directories that should be readable (and eventually writeable) by
 
  the website user (running the WSGI application) without having to become root.
 
* All files placed in the website directory should be either created there
 
  directly, or copied to the directory in order to make sure the ``SGID`` gets
 
  honored. **Do not move the files, the permissions will not be set correctly.**
 
* Within the website directory, Python virtual environment can be found within
 
  the ``virtualenv`` sub-directory. The virtual environment is also symlinked to
 
  website admin's ``~/.virtualenvs/`` directory for easier access (and
 
  auto-completion with virtualenvwrapper).
 
* Within the website directory, nginx will expect to find the static files
 
  within the ``htdocs`` sub-directory (this can be symlink too). Locations/aliases
 
  can be configured for static file serving.
 
* Within the website directory, systemd service will expect to find the website
 
  code within the ``code`` sub-directory (this can be symlink too).
 
* nginx communicates with WSGI server over a dedicated Unix socket for each
 
  website.
 

	
 

	
 
Parameters
 
~~~~~~~~~~
 

	
 
**admin** (string, mandatory)
 
  Name of the operating system user in charge of maintaining the website. This
 
  user is capable of making modifications to website configuration anda data
 
  stored within the website directory.
 

	
 
**fqdn** (string, mandatory)
 
  Fully-qualified domain name where the website is reachable. This value is used
 
  for calculating the user/group name for dedicated website user, as well as
 
  home directory of the website user (where data/code should be stored at).
 

	
 
**packages** (list, optional)
 
  A list of additional packages to install for this particular WSGI
 
  website. This is usually going to be development libraries for building Python
 
  packages.
 

	
 
**rewrites** (list, optional)
 
  A list of rewrite rules that are applied to incoming requests. Each element of
 
  the list should be a string value compatible with the format of ``nginx``
 
  option ``rewrite``. The keyword ``rewrite`` itself should be omitted, as well
 
  as trailing semi-colon (``;``).
 

	
 
**static_locations** (list, optional)
 
  List of locations that should be treated as static-only, and not processed by
 
  the WSGI application at all. This is normally used for designating serving of
 
  static/media files by Nginx (for example, in case of Django projects for
 
  ``/static/`` and ``/media/``).
 

	
 
**uid** (integer, mandatory)
 
  UID/GID (they are set-up to be the same) of the dedicated website
 
  user/group.
 

	
 
**use_paste** (boolean, optional)
 
  Tell Gunicorn to assume that the passed-in ``wsgi_application`` value is a
 
  filename of a Python Paste ``ini`` file instead of WSGI application.
 

	
 
**virtuaelnv_packages** (list, optional)
 
  A list of additional packages to install for this particular PHP
 
  appliction. This is usually going to be different PHP extensions.
 

	
 
**wsgi_application** (string, mandatory)
 
  WSGI application that should be started by Gunicorn. The format should be
 
  conformant to what the ``gunicorn`` command-line tool accepts. If the
 
  ``use_paste`` option is enabled, the value should be equal to filename of the
 
  Python Paste ini file, located in the ``code`` sub-directory.
 

	
 

	
 
Examples
 
~~~~~~~~
 

	
 
Here is an example configuration for setting-up a (base) WSGI website (for
 
running a bare Django project):
 

	
 
.. code-block:: yaml
 

	
 
    ---
 

	
 
    - role: wsgi_website
 
      admin: admin
 
      fqdn: django.example.com
 
      static_locations:
 
        - /static
 
        - /media
 
      uid: 2004
 
      virtualenv_packages:
 
        - django
 
      wsgi_application: django_example_com.wsgi:application
roles/wsgi_website/defaults/main.yml
Show inline comments
 
new file 100644
 
---
 

	
 
packages: []
 
rewrites: []
 
static_locations: []
 
use_paste: False
 
virtualenv_packages: []
 
\ No newline at end of file
roles/wsgi_website/handlers/main.yml
Show inline comments
 
new file 100644
 
---
 

	
 
- name: Restart website
 
  service: name="{{ fqdn }}" state=restarted
roles/wsgi_website/tasks/main.yml
Show inline comments
 
new file 100644
 
---
 

	
 
- set_fact:
 
    user: "web-{{ fqdn | replace('.', '_') }}"
 
    home: "/var/www/{{ fqdn }}"
 

	
 
- name: Create WSGI website group
 
  group: name="{{ user }}" gid="{{ uid }}" state=present
 

	
 
- name: Create home directory for the user (avoid populating with skeleton)
 
  file: path="{{ home }}" state=directory
 
        owner="{{ admin }}" group="{{ user }}" mode=2750
 

	
 
- name: Create WSGI website user
 
  user: name="{{ user }}" uid="{{ uid }}" group="{{ user }}"
 
        system=yes createhome=no state=present
 

	
 
- name: Add nginx user to website group
 
  user: name="www-data" groups="{{ user }}" append="yes"
 
  notify:
 
    - Restart nginx
 

	
 
- name: Add admin to website group
 
  user: name="{{ admin }}" groups="{{ user }}" append="yes"
 

	
 
- name: Create directory for storing socket file
 
  file: path="/var/run/wsgi/{{ fqdn }}" state="directory"
 
        owner="{{ user }}" group="www-data" mode="750"
 

	
 
- name: Install extra packages for website
 
  apt: name="{{ item }}" state=present
 
  with_items: packages
 

	
 
- name: Create directory for storing the Python virtual environment
 
  file: path="{{ home }}/virtualenv" state=directory
 
        owner="{{ admin }}" group="{{ user }}" mode="2750"
 

	
 
- name: Create Python virtual environment
 
  sudo_user: "{{ admin }}"
 
  command: /usr/bin/virtualenv "{{ home }}/virtualenv" creates="{{ home }}/virtualenv/bin/activate"
 

	
 
- name: Create directory where virtualenvs will be symlinked to
 
  sudo_user: "{{ admin }}"
 
  file: path="~/.virtualenvs" state=directory mode=750
 

	
 
- name: Create convenience symlink for Python virtual environment wrapper utility
 
  sudo_user: "{{ admin }}"
 
  file: src="{{ home }}/virtualenv" dest="~/.virtualenvs/{{ fqdn }}" state=link
 

	
 
- name: Deploy virtualenv wrapper
 
  template: src="venv_exec.j2" dest="{{ home }}/virtualenv/bin/exec"
 
            owner="{{ admin }}" group="{{ user }}" mode="750"
 

	
 
- name: Install Gunicorn in Python virtual environment
 
  sudo_user: "{{ admin }}"
 
  pip: name=gunicorn state=present virtualenv="{{ home }}/virtualenv"
 

	
 
- name: Install additional packages in Python virtual environment
 
  sudo_user: "{{ admin }}"
 
  pip: name="{{ item }}" state=present virtualenv="{{ home }}/virtualenv"
 
  with_items: virtualenv_packages
 

	
 
- name: Deploy systemd socket configuration for website
 
  template: src="systemd_wsgi_website.socket.j2" dest="/etc/systemd/system/{{ fqdn }}.socket"
 
            owner=root group=root mode=644
 
  notify:
 
    - Reload systemd
 
    - Restart website
 

	
 
- name: Deploy systemd service configuration for website
 
  template: src="systemd_wsgi_website.service.j2" dest="/etc/systemd/system/{{ fqdn }}.service"
 
            owner=root group=root mode=644
 
  notify:
 
    - Reload systemd
 
    - Restart website
 

	
 
- name: Enable the website service
 
  service: name="{{ fqdn }}" enabled=yes state=started
 

	
 
- name: Create directory where static files can be served from
 
  file: path="{{ home }}/htdocs/" state=directory
 
        owner="{{ admin }}" group="{{ user }}" mode="2750"
 

	
 
- name: Deploy nginx configuration file for website
 
  template: src="nginx_site.j2" dest="/etc/nginx/sites-available/{{ fqdn }}"
 
            owner=root group=root mode=640 validate="/usr/local/bin/nginx_verify_site.sh -n '{{ fqdn }}' %s"
 
  notify:
 
    - Restart nginx
 

	
 
- name: Enable nginx website
 
  file: src="/etc/nginx/sites-available/{{ fqdn }}" dest="/etc/nginx/sites-enabled/{{ fqdn }}"
 
        state=link
 
  notify:
 
    - Restart nginx
roles/wsgi_website/templates/nginx_site.j2
Show inline comments
 
new file 100644
 
server {
 
    listen 80;
 

	
 
    root {{ home }}/htdocs/;
 

	
 
    server_name {{ fqdn }};
 

	
 
    # Site rewrites.
 
    {% for rewrite in rewrites -%}
 
    rewrite {{ rewrite }};
 
    {% endfor %}
 

	
 
    # Static locations
 
    {% for location in static_locations -%}
 
    location {{ location }} {
 
        try_files $uri $uri/ =404;
 
    }
 
    {% endfor %}
 

	
 
    # Pass remaining requests to the WSGI server.
 
    location / {
 
        try_files $uri @proxy_to_app;
 
    }
 

	
 
    location @proxy_to_app {
 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
        proxy_set_header Host $http_host;
 
        proxy_redirect off;
 

	
 
        proxy_pass http://unix:/run/wsgi/{{ fqdn }}.sock;
 
    }
 

	
 
    access_log /var/log/nginx/{{ fqdn }}-access.log;
 
    error_log /var/log/nginx/{{ fqdn }}-error.log;
 
}
roles/wsgi_website/templates/supervisor_site.conf.j2
Show inline comments
 
new file 100644
 
[program:{{ fqdn }}]
 
command={{ home }}/virtualenv/bin/gunicorn --bind unix:/var/run/wsgi/{{ fqdn }}/socket {% if use_paste %}--paste{% endif %} {{ wsgi_application }}
 
directory={{ home }}/code
 
user={{ user }}
 
group={{ user }}
 
autostart=true
 
autorestart=true
 
umask=0007
 
redirect_stderr=true
 
environment=HOME="{{ home }}"
roles/wsgi_website/templates/systemd_wsgi_website.service.j2
Show inline comments
 
new file 100644
 
[Unit]
 
Description=Website {{ fqdn }}
 
Requires={{ fqdn }}.socket
 
After=network.target
 

	
 
[Service]
 
User={{ user }}
 
Group={{ user }}
 
WorkingDirectory={{ home }}/code
 
ExecStart={{ home }}/virtualenv/bin/gunicorn --bind unix:/run/wsgi/{{ fqdn }}.sock {% if use_paste %}--paste {{home}}/code/{{ wsgi_application }}{% else %}{{ wsgi_application }}{% endif %}
 

	
 
ExecReload=/bin/kill -s HUP $MAINPID
 
ExecStop=/bin/kill -s TERM $MAINPID
 
PrivateTmp=true
 

	
 
[Install]
 
WantedBy=multi-user.target
 
\ No newline at end of file
roles/wsgi_website/templates/systemd_wsgi_website.socket.j2
Show inline comments
 
new file 100644
 
[Unit]
 
Description=Socket for website {{ fqdn }}
 

	
 
[Socket]
 
ListenStream=/run/wsgi/{{ fqdn }}.sock
 
SocketUser=www-data
 
SocketGroup=www-data
 
SocketMode=0660
 

	
 
[Install]
 
WantedBy=sockets.target
roles/wsgi_website/templates/venv_exec.j2
Show inline comments
 
new file 100644
 
#!/bin/bash
 

	
 
source {{ home }}/virtualenv/bin/activate
 
$@
0 comments (0 inline, 0 general)