Matomo On-Premise Setup on AWS EC2

Matomo On-Premise Setup on AWS EC2

Deploying your own analytics platform gives you full control over data and performance. In this post, we’ll walk through how we stood up Matomo (formerly Piwik) on a self-managed Ubuntu EC2 instance, covering everything from provisioning the server to verifying tracking and troubleshooting asset loading issues.


Overview

Matomo is a powerful, open-source web analytics suite you can host on your own infrastructure. By running Matomo on an AWS EC2 instance, you maintain total ownership of your analytics data and can scale resources as your traffic grows. We chose Ubuntu 22.04 LTS for stability and long-term support, paired with the familiar LAMP stack. Here’s what we’ll cover:

  1. EC2 instance provisioning
  2. System updates & LAMP component installation
  3. PHP 8.1 setup
  4. Downloading and deploying Matomo
  5. Apache configuration and troubleshooting
  6. Completing the web installer
  7. Verifying tracking
  8. Next steps and tips

1. EC2 Instance Provisioning

We began by launching an Ubuntu 22.04 LTS instance using the t3.small type:

  • Security Group

    • SSH (22) restricted to our office IP
    • HTTP (80) & HTTPS (443) open to the world
  • Key Pair: Existing SSH key for seamless login

  • Public IP: 13.232.147.173


2. System Updates & LAMP Components

First, ensure the system is up-to-date and install the core LAMP stack:

  1. Update & Upgrade

    sudo apt update && sudo apt upgrade -y
    
  2. Apache

    sudo apt install -y apache2
    sudo systemctl enable --now apache2
    
  3. MariaDB

    sudo apt install -y mariadb-server
    sudo mysql_secure_installation
    

    During mysql_secure_installation, we:

    • Switched to unix_socket auth
    • Removed anonymous users
    • Disabled remote root login
    • Dropped the test database

  1. Create Matomo Database & User

    sudo mysql -u root
    CREATE DATABASE matomo CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    CREATE USER 'matomo'@'localhost' IDENTIFIED BY 'openpecha_admin';
    GRANT ALL PRIVILEGES ON matomo.* TO 'matomo'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
    


3. PHP Installation

Matomo recommends at least PHP 7.4, but we opted for PHP 8.1 for better performance:

  1. Add Ondřej Surý’s PPA & Update

    sudo add-apt-repository ppa:ondrej/php
    sudo apt update
    
  2. Install PHP 8.1 + Extensions

    sudo apt install -y \
      php8.1 php8.1-cli php8.1-fpm php8.1-mysql php8.1-xml php8.1-gd \
      php8.1-curl php8.1-mbstring php8.1-zip php8.1-intl php8.1-bcmath
    
  3. Verify Version

    php -v
    

    You should see something like PHP 8.1.x (cli).


4. Matomo Download & Initial Deployment

Download the latest Matomo release and stage it for Apache:

cd /tmp
wget https://builds.matomo.org/matomo-latest.zip
sudo apt install -y unzip
unzip matomo-latest.zip

At this point, the Matomo files live in /tmp/matomo. We’ll move them into our web root in the Apache configuration step.


5. Initial Apache Configuration & Testing

Enable URL rewriting and set up a dedicated VirtualHost:

  1. Enable mod_rewrite

    sudo a2enmod rewrite
    sudo systemctl reload apache2
    
  2. Create /etc/apache2/sites-available/matomo.conf

    <VirtualHost *:80>
      ServerName 13.232.147.173
      DocumentRoot /var/www/html/matomo
      <Directory /var/www/html/matomo>
        AllowOverride All
        Require all granted
      </Directory>
      ErrorLog ${APACHE_LOG_DIR}/matomo_error.log
      CustomLog ${APACHE_LOG_DIR}/matomo_access.log combined
    </VirtualHost>
    
  3. Disable Default Site & Enable Matomo

    sudo a2dissite 000-default.conf
    sudo a2ensite matomo.conf
    sudo systemctl reload apache2
    
  4. Move Files & Test

    sudo mv /tmp/matomo /var/www/html/
    sudo chown -R www-data:www-data /var/www/html/matomo
    

    Visiting http://13.232.147.173/ now brings up the Matomo installer.

Troubleshooting: If CSS/JS assets show 404 errors or red messages, ensure your VirtualHost uses AllowOverride All (so .htaccess can rewrite URLs) and that the default site is disabled. Note, this is solved here ** 5.3 Disable Default Site & Enable Matomo**


6. Web Installer Completion

Proceed through the web-based installer:

  1. System Check: All PHP extensions and directory permissions passed

  2. Database Setup: Connected to matomo database with user matomo

  3. Superuser Account: Created admin user

  4. Site Configuration:

    • Site 1: Pecha.org
    • Site 2: Secondary tracking site (ID 2)

The installer confirms success and provides tracking snippets for each site.


7. Tracking Verification

After embedding the provided <script> snippet on our pages, we noticed Matomo’s matomo.js being requested—but it wasn’t loading. A quick check in the browser console revealed:

“Failed to load resource: net::ERR_BLOCKED_BY_CLIENT”

This turned out to be due to ad-blocker extensions. Disabling ad-blockers allowed Matomo’s script to load and data to flow into the dashboard.


Next Steps

  • Backups & Monitoring: Automate database snapshots and use CloudWatch to monitor EC2 health.
  • Scaling: Move the database onto RDS or a larger instance, and explore load-balanced front-ends if traffic spikes.