<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: altan</title>
    <description>The latest articles on DEV Community by altan (@altan).</description>
    <link>https://dev.to/altan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F275735%2F0c7aecad-4ff5-45ba-8cd3-fcd34324294f.jpeg</url>
      <title>DEV Community: altan</title>
      <link>https://dev.to/altan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/altan"/>
    <language>en</language>
    <item>
      <title>Self-hosted Matomo - an implementation guide for production</title>
      <dc:creator>altan</dc:creator>
      <pubDate>Mon, 24 Jul 2023 22:07:47 +0000</pubDate>
      <link>https://dev.to/altan/self-hosted-matomo-an-implementation-guide-for-production-2jcg</link>
      <guid>https://dev.to/altan/self-hosted-matomo-an-implementation-guide-for-production-2jcg</guid>
      <description>&lt;p&gt;In this article, I will show you how to self-host Matomo using Docker and Ansible. The final stack will feature:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A self-hosted Matomo instance on a ~ $10 VPS&lt;/li&gt;
&lt;li&gt;SSL (A+) certificate via Let's Encrypt and Certbot&lt;/li&gt;
&lt;li&gt;Zero-downtime deployments&lt;/li&gt;
&lt;li&gt;A Matomo sidecar container for cronjobs&lt;/li&gt;
&lt;li&gt;Version controlled Matomo plugins&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Since Google sunset Google Analytics v3 at the end of June 2023 a lot of people have been underwhelmed with the experience of transitioning to GA 4. On top of that, there is a growing demand for privacy friendly website analytics, especially cookie-less solutions to adhere to GDPR or California online privacy laws.&lt;/p&gt;

&lt;p&gt;There are numerous solutions out now that offer a cookie-less way to measure website interactions, one of which is Matomo. It's an open source solution (license here) that is similar to GA3 in its feature set. It has a plugin to import data from GA3 and even comes with its own tag manager (no more third parties for loading your container). While the overall look and feel may be lacking in certain areas, it's an actively developed product.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you're bored already you can jump ahead and &lt;a href="https://github.com/AltanS/matomo-docker-ansible"&gt;check out the code on GitHub&lt;/a&gt;, the README includes all steps necessary to get up and running, it assumes some familiarity with the technologies involved, however.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;p&gt;To follow along, you should have access to a terminal shell. MacOS and Linux users are good to go, &lt;a href="https://learn.microsoft.com/en-us/windows/wsl/install"&gt;for Windows users it'd be best to have WSL2 set-up&lt;/a&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  A server running Ubuntu 22.04
&lt;/h3&gt;

&lt;p&gt;This should be a fresh VPS instance with your SSH key added to the &lt;code&gt;authorized_keys&lt;/code&gt;. It's pretty straight forward to do this with Digital Ocean or Hetzner. Depending on your expected load, make sure to pick a more powerful instance.&lt;br&gt;
If you use my &lt;a href="https://m.do.co/c/8af1fe1acab5"&gt;Digital Ocean referral link&lt;/a&gt;, you will receive $200 for 60 days to test out this stack.&lt;/p&gt;

&lt;p&gt;If you don't know how to set up a VPS with your SSH key, please refer to this &lt;a href="https://docs.digitalocean.com/products/droplets/how-to/add-ssh-keys/"&gt;tutorial by Digital Ocean&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Git
&lt;/h3&gt;

&lt;p&gt;You will need a &lt;a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git"&gt;Git client installed&lt;/a&gt;, this should be pretty straight forward on most operating systems.&lt;/p&gt;
&lt;h3&gt;
  
  
  A GitHub Account with a public key configured
&lt;/h3&gt;

&lt;p&gt;In order to access the VPS we will set up in the next steps, it's easiest to add an SSH key to your GitHub account. &lt;a href="https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account"&gt;There is a tutorial provided by GitHub here&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Python and Pipenv
&lt;/h3&gt;

&lt;p&gt;Pipenv is a Python virtualenv management tool that, simply put, locks versions of the Python libraries a project requires. This is crucial to provide a similar working environment for developers. &lt;a href="https://pipenv.pypa.io/en/latest/installation/"&gt;Please refer to the official installation guide to install Pipenv&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you've installed the requirements, we're ready to clone the repository. Open your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone git@github.com:AltanS/matomo-docker-ansible.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Using the SSH approach to clone a repository requires your SSH key to be added to your GitHub account.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;matomo-docker-ansible
 
&lt;span class="c"&gt;# create the python environment for this project&lt;/span&gt;
pipenv shell
&lt;span class="c"&gt;# install python requirements from Pipfile and lock them&lt;/span&gt;
pipenv &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;span class="c"&gt;# install ansible requirements&lt;/span&gt;
ansible-galaxy &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.yml 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Domain setup for your custom Matomo domain
&lt;/h2&gt;

&lt;p&gt;For your final Matomo instance to be available on the public internet, make sure you've configured an A-Record pointing a custom domain (or subdomain) to the IP of the server you've created earlier. For example, this could be &lt;code&gt;matomo.example.com&lt;/code&gt;. In this case, you would have to add an &lt;strong&gt;A-Record&lt;/strong&gt; with a Name (or Hostname) of &lt;code&gt;matomo&lt;/code&gt; and a &lt;strong&gt;Value&lt;/strong&gt; of your server IP. Once this is done, you can check that everything's working by trying to log in via SSH into your server using this newly configured domain name.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh root@matomo.example.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sometimes it can take a while for DNS entries to be populated, so a bit of patience might be necessary for this step.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Server Provisioning (Infrastructure as Code)
&lt;/h2&gt;

&lt;p&gt;Next we will provision the server using Ansible. Using Ansible we can define and configure the server as code in our repository, which makes running this stack in production much safer, as the stack is easily reproducible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ansible configuration
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Add your server IP to &lt;code&gt;hosts/production&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[production]
172.168.0.123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Copy &lt;code&gt;.env.example&lt;/code&gt; to &lt;code&gt;.env&lt;/code&gt;
You can do this in a terminal command or within your IDE. What's important is that you change the values for:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GITHUB_USER_KEYS&lt;/code&gt;: this is the full URL to your public SSH keys&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MATOMO_HOSTNAME&lt;/code&gt;: this is the domain name configured in the previous step&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LETSENCRYPT_ADMIN_EMAIL&lt;/code&gt;: this should be an email you have access to &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Provisioning the server
&lt;/h3&gt;

&lt;p&gt;Once all of these steps are complete, you can run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ansible-playbook provision.yml &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"target_host=production"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run the &lt;code&gt;provision.yml&lt;/code&gt; playbook with the &lt;code&gt;target_host&lt;/code&gt; set to &lt;code&gt;production&lt;/code&gt;. If you ever decide to add a staging environment, you'd have to change this target_host (and also add /hosts/staging).&lt;/p&gt;

&lt;p&gt;The provisioning can take a while, so please be patient. If any errors occur, make sure you've followed all steps correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment
&lt;/h2&gt;

&lt;p&gt;The deployment playbook (&lt;code&gt;deploy.yml&lt;/code&gt;) will set up and start docker compose for this stack. Docker compose is a way to describe docker containers in a YAML file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up secrets
&lt;/h3&gt;

&lt;p&gt;You can adjust the passwords in &lt;code&gt;group_vars/production/secrets.yml&lt;/code&gt; directly or make use of encrypted passwords via Ansible vaults. &lt;a href="https://github.com/AltanS/matomo-docker-ansible/tree/main#setup"&gt;Please refer to the repository on how to use &lt;code&gt;vault_pass.txt&lt;/code&gt; and Ansible vaults to encrypt your passwords.&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Running the deployment
&lt;/h3&gt;

&lt;p&gt;Once you've set your secrets, you're ready for deployment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ansible-playbook deploy.yml &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"target_host=production"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing Matomo
&lt;/h2&gt;

&lt;p&gt;Once the deployment tasks have finished running you should be able to access your Matomo instance via the domain you've configured.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;So what was just installed?&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NGINX on Ubuntu 22.04 via APT: handles requests to the &lt;code&gt;matomo_hostname&lt;/code&gt; and serves the SSL certificate. All requests to port 443 are proxy passed to the Docker Daemon running&lt;/li&gt;
&lt;li&gt;Docker: another NGINX instance, Matomo, MariaDB and another Matomo instance for archival tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installing plugins
&lt;/h2&gt;

&lt;p&gt;To install a Matomo plugin in this self-hosted instance, add the plugin folder to &lt;code&gt;matomo-plugins&lt;/code&gt; and then re-run the deployment playbook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ansible-playbook deploy.yml &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"target_host=production"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will copy the plugin folder to the remote host and mount it into the docker compose stack.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating Matomo
&lt;/h2&gt;

&lt;p&gt;You can update Matomo via the admin dashboard, do this at your own risk and make sure you have backed up your data before.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backups
&lt;/h2&gt;

&lt;p&gt;You can use any approach to back up a MySQL database. This is &lt;a href="https://gist.github.com/spalladino/6d981f7b33f6e0afe6bb"&gt;a Gist of what this could look like&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this helped someone out. If you find a bug or have an idea on how to improve, please let me know in the comments.&lt;/p&gt;

</description>
      <category>selfhost</category>
      <category>analytics</category>
      <category>devops</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
