Install Piwik with Trellis

A client wants to get Piwik installed for his website.

I was wondering if it would be possible to have Piwik running on the same server as the wp installations, just on a subdomain like piwik.site1.com.

So folder structure would be

project.com

  • trellis
  • site1
  • piwik

My question now, does that even make sense? And if, how could I achieve that? Since trellis always installs wordpress for a new configured site. Could that be somehow bypassed?

Thanks for any tips and hints.

1 Like

There’s some existing roles to install Pwik using Ansible like https://github.com/ICTO/ansible-piwik.

I suggest just trying one of those. Add it to Trellis like other Galaxy roles we have in requirements.yml. Then add the role to server.yml and customize the variables.

1 Like

I was looking at this the other week. Do you need Piwik to be installed on a subdomain?

My thoughts were to just have it install via Composer into the web folder

1 Like

I ended up installing it on a separate instance for now. But will try your approach next time :slight_smile:

Hi all,

I added a few small roles to Trellis which does most of the job.

# group_vars/{staging & production}/wordpress_sites.yml
...
matomo:
  db:
    user: matomo
    host: localhost

# group_vars/{staging & production}/vault.yml
...
matomo:
  db:
    password: 

# 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: 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:          {{ matomo_db_name }}
         Database User:     {{ item.value.matomo.db.user }}
         Database Password: {{ item.value.matomo.db.password }}
         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
...
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 }}"

Install & deploy.
Connect to web and follow the installation assistant. This step is only necessary after the first install. It could be automated, but too much work for what it is.

5 Likes

Awesome :tada: Thanks so much for sharing, I’m not into Ansible (yet) and needed guidance, installs fine…!

I needed to create the addons_dir first though – how / where did you set that one up? I am now establishing the folder in roles/matomo/tasks/main.yml (btw: I changed the order of the DB details in the install instructions there to match Matomo’s input fields) and then I also needed to reference the directory in roles/deploy/defaults/main.yml explicitly otherwise the deploy will fail…

Created this gist for reference.

Thanks again!

1 Like

Well spotted, I forgot about that (different file on my setup).

# 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 }}"

Great idea for the gist!

You are right again! I have it in there as well.

Out of curiosity, how does the compatibility with Trellis work using the ICTO/ansible-piwik role?
For deb-style dependencies (Ubuntu), php5 is used, while Trellis uses php7,

Matomo (ex Piwik) does work with PHP 7 just as well as with 5.
ICTO/ansible-piwik seems out of date. Matomo moved to a new format for the geoip databases also.
I’d recommend building your own task based on the above comments.

2 Likes

What about this ? Lakim repository

Apparently it assumes an apache based setup:

Yes, but this repo is a good starting point (incl. php71 and etc)

It is outdated as well, for the same reasons as put forward in post 9.

That is not needed if using Trellis, since Trellis already installs PHP.