Skip to content

Instantly share code, notes, and snippets.

@E-VANCE
Last active July 12, 2019 13:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save E-VANCE/6f4187e9373046583f7c443161723315 to your computer and use it in GitHub Desktop.
Save E-VANCE/6f4187e9373046583f7c443161723315 to your computer and use it in GitHub Desktop.
Adding Matomo to Trellis, credit / source https://discourse.roots.io/t/install-piwik-with-trellis/10743/4
# group_vars/{staging & production}/wordpress_sites.yml
[...]
wordpress_sites:
example.com:
site_hosts: [...]
[...]
matomo:
db:
user: matomo
host: localhost
# group_vars/{staging & production}/vault.yml
[...]
vault_wordpress_sites:
example.com:
env:
[...]
matomo:
db:
password: XXXXXX
# server.yml
[...]
- { role: matomo, tags: [matomo] }
# roles/matomo/defaults/main.yml
combined_wordpress_sites: "{{ wordpress_sites|combine(vault_wordpress_sites, recursive=True) }}"
addons_dir: "{{ www_root }}/{{ item.key }}/addons"
matomo_db_name: "matomo_{{ env }}"
# roles/matomo/tasks/main.yml
- name: Create addons folder of sites
file:
path: "{{ addons_dir }}"
owner: "{{ web_user }}"
group: "{{ web_group }}"
mode: 0755
state: directory
with_dict: "{{ wordpress_sites }}"
- name: Install Matomo
composer:
command: require
arguments: piwik/piwik
working_dir: "{{ addons_dir }}"
with_dict: "{{ wordpress_sites }}"
tags: matomo
- name: Create databases for Matomo
mysql_db:
name: "{{ matomo_db_name }}"
state: present
login_host: "{{ site_env.db_host }}"
login_user: "{{ mysql_root_user }}"
login_password: "{{ mysql_root_password }}"
with_dict: "{{ wordpress_sites }}"
tags: matomo
- name: Create/assign database user to db and grant permissions
mysql_user:
name: "{{ item.value.matomo.db.user }}"
password: "{{ item.value.matomo.db.password }}"
host: "{{ site_env.db_user_host }}"
append_privs: yes
priv: "{{ matomo_db_name }}.*:ALL"
state: present
login_host: "{{ site_env.db_host }}"
login_user: "{{ mysql_root_user }}"
login_password: "{{ mysql_root_password }}"
with_dict: "{{ combined_wordpress_sites }}"
tags: matomo
- name: Change addons owner to web_user
file:
path: "{{ addons_dir }}"
owner: "{{ web_user }}"
group: "{{ web_group }}"
state: directory
recurse: yes
with_dict: "{{ wordpress_sites }}"
tags: matomo
- name: Explain next steps
debug:
msg: |
If necessary, set up Matomo as follows.
1) Deploy
2) Point your browser to {{ wordpress_env_defaults.wp_home }}/analytics
3) Proceed with the form using following credentials:
Host: {{ site_env.db_user_host }}
Database User: {{ item.value.matomo.db.user }}
Database Password: {{ item.value.matomo.db.password }}
Database: {{ matomo_db_name }}
Table Prefix: none
Set up your admin user.
4) Log in to Matomo.
5) Under Administration/System/Geolocation, download and activate the GeoIP 2 database.
with_dict: "{{ combined_wordpress_sites }}"
tags: matomo
# roles/deploy/defaults/main.yml
[...]
addons_dir: "{{ project_root }}/addons"
[...]
deploy_share_after:
- "{{ playbook_dir }}/deploy-hooks/share-after.yml"
# deploy-hooks/share-after.yml
- name: Create symlink to Matomo
file:
path: "{{ deploy_helper.new_release_path }}/web/analytics"
src: "{{ addons_dir }}/vendor/piwik/piwik"
state: link
with_items: "{{ wordpress_sites }}"
@E-VANCE
Copy link
Author

E-VANCE commented Jun 21, 2019

IMPORTANT

The current Matomo build doesn't seem to play nice with regards to the composer setup, had to revert to v3.8.0 and install the JShrink dependency explicitely beforehand...

# roles/matomo/tasks/main.yml

- name: Install JShrink dependency (Matomo installation fails otherwise...)
  composer:
    command: require
    arguments: tedivm/jshrink ~0.5.1
    working_dir: "{{ addons_dir }}"
  with_dict: "{{ wordpress_sites }}"

- name: Install Matomo
  composer:
    command: require
    arguments: piwik/piwik 3.8.0
    working_dir: "{{ addons_dir }}"
  with_dict: "{{ wordpress_sites }}"
  tags: matomo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment