Matomo(Analytics) Production Setup on Azure Cloud using PaaS Database
Tags: Matomo, Analytics, Google Analytics, DevOps, Azure

Matomo (earlier piwik) is a free and open source web analytics application that runs on PHP/MySQL webserver. This article aims on how to setup Matomo on Azure using Load balanced VM scale sets and Azure Database for MySQL.

Operation System: Ubuntu 18.04, Webserver: Apache

Create a VM scale set on Azure using portal — Approx 70$/month D2s_v3

1.1. Enter basic details, use premium disk for OS for high performance.

1.2. Select Ubuntu Server 18.04 Image

1.3. Keep the initial instance count to 1, later you may add auto scaling rules.

Azure VMSS creation

1.4. Under networking tab, select Yes for Standard Load balancer and leave other details as default.

LB

Enable below NSG Rules for connectivity

Network Security Group created during VMSS creation

2. Install Apache, PHP and MySQL client

2.1. Connect to VMSS using Load balancer public IP. Port can be 50000* something which can bee seen in “Inbound NAT rules” of LB.

2.2. Run below commands.

sudo apt update

sudo apt upgrade

sudo apt install -y apache2 apache2-utils

Verify using below command

systemctl status apache2

If it is not running use systemctl to start it.

sudo systemctl start apache2

To enable apache2 on system reboot

sudo systemctl enable apache2

Now we need to set www-data (Apache user) as the owner of document root (otherwise known as web root). By default it’s owned by the root user.

sudo chown www-data:www-data /var/www/html/ -R

Load webpage using Load balancer IP address and you should see apache page as below.

Now let’s install PHP 7.2 using command below

sudo apt install php7.2 libapache2-mod-php7.2 php7.2-mysql php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline

Enable the Apache php7.2 module then restart Apache Web server

sudo a2enmod php7.2

sudo systemctl restart apache2

To install mysql client:

sudo apt install mysql-client-core-5.7

We need this client to connect to Azure Database for MySQL.

3. Create Azure Database for MySQL (Single server) using Azure portal Approx 250$/month

Use version 5.7 and 4 vCores for a decent performance.

You can select storage as per the size of your database. For Production deployment it is better to use double the size of storage initially for better IOPS. The storage is automatically scaled and once increased it cannot be scaled down.

Locally redundant storage can be used unless you want to backup in a different region.

Note down servername and server admin login name.

To connect from VMSS allow public IP of load balancer in MySQL DB “Connection security” as below.

You can also add Vnet of your VMSS in “Vnet Rules” and connect using private IP which is more secure. See below

4. Matomo installation

Connect to VMSS using SSH

Run commands below to download and install latest version of Matomo

wget https://builds.matomo.org/matomo-latest.zip

sudo apt install unzip

sudo unzip matomo-latest.zip -d /var/www/

The -d option specifies the target directory. Matomo web files will be extracted to /var/www/matomo/.

Then we need to change the owner of this directory to www-data so that the web server can write to this directory.

sudo chown www-data:www-data /var/www/matomo/ -R

Create Database and User in mysql

To connect to Azure Mysql DB using below command. You should allow the IP in the database before executing this.

mysql — host ******.mysql.database.azure.com — user ****@**** -p

Enter the password you have put for database admin user during creation.

create database matomo;

create user matomouser@localhost identified by ‘matomopassword’;

grant all privileges on matomo.* to matomouser@localhost identified by ‘matomopassword’;

flush privileges;

exit;

Create Apache configuration file

Create a virtual host configuration file in /etc/apache2/sites-available/ directory.

sudo nano /etc/apache2/sites-available/matomo.conf

Put the following text into the file. Replace the blod text with your actual URL OR Load balancer IP address. Don’t forget to set A record for the domain name if used.

<VirtualHost *:80>

ServerAdmin webmaster@localhost

ServerName analytics.example.com

DocumentRoot /var/www/matomo/

<Directory /var/www/matomo>

DirectoryIndex index.php

Options FollowSymLinks

AllowOverride All

Require all granted

</Directory>

<Files “console”>

Options None

Require all denied

</Files>

<Directory /var/www/matomo/misc/user>

Options None

Require all granted

</Directory>

<Directory /var/www/matomo/misc>

Options None

Require all denied

</Directory>

<Directory /var/www/matomo/vendor>

Options None

Require all denied

</Directory>

ErrorLog ${APACHE_LOG_DIR}/matomo_error.log

CustomLog ${APACHE_LOG_DIR}/matomo_access.log combined

</VirtualHost>

Save and close the file. Then enable this virtual host.

sudo a2ensite matomo.conf

Reload Apache web server for the change to take effect.

sudo systemctl reload apache2

Install and Enable PHP Modules

Run the following commands to install PHP modules required or recommended by Matomo.

sudo apt install php-imagick php7.2-common php7.2-gd php7.2-json php7.2-curl php7.2-zip php7.2-xml php7.2-mbstring php7.2-bz2 php7.2-intl

If you are using Apache web server, you need to reload it to make it run with these PHP modules.

sudo systemctl reload apache2

Bingo, You’re done !!

Now browse the URL OR IP address using http://analytics.example.com OR

http://IPAddress and you should see the Matomo sign page as below.

Now follow the setup prompts and provide the information of Database and You should below page once all checks are green.

Total Cost: 345$/month Approx (added 25$/month for Load balancer)

Note: All the prices are as of Dec 2020 (East US)

To enable SSL follow next article, coming soon..