On this page
- Why run Matomo yourself?
- Which packages does Matomo need?
- How do you prepare the database and files?
- How do you lock down Nginx for Matomo?
- How do you add HTTPS and run the installer?
- How do you add the tracking code to a site?
- How do you set up cron archiving?
- How do you enable geolocation, email and updates?
- Questions
Why run Matomo yourself?
Self-hosting Matomo keeps every page view, referrer and visitor log in a database you own, which simplifies privacy notices and removes third party data sharing. The tracker is a small JavaScript file plus a PHP endpoint, so the analytics server can be separate from the sites it measures or share a VPS with them.
Resource needs grow with traffic, not with the number of sites. A server with 2 GB of RAM tracks a handful of sites with a few thousand visits per day comfortably. Report archiving is the heavy part; if you track hundreds of thousands of actions per day, give the database more memory and NVMe storage. For a lighter tool with fewer features, compare Plausible or Umami.
Which packages does Matomo need?
Matomo runs on PHP 8 with the mysql, gd, curl, xml, mbstring and zip extensions plus a MariaDB or MySQL database.
Ubuntu and Debian
sudo apt update
sudo apt install -y nginx mariadb-server php-fpm php-mysql php-gd php-curl php-xml php-mbstring php-zip php-intl unzip
php -vRocky Linux 9 and AlmaLinux 9
sudo dnf module reset -y php
sudo dnf module enable -y php:8.2
sudo dnf install -y nginx mariadb-server php-fpm php-mysqlnd php-gd php-xml php-mbstring php-pecl-zip php-intl unzip
sudo systemctl enable --now nginx mariadb php-fpmRaise memory_limit to 512M in the FPM and CLI php.ini files; archiving large date ranges needs it. The full stack setup is covered in the LEMP stack guide.
How do you prepare the database and files?
Create a dedicated database, then download and unpack the latest build.
sudo mariadb-secure-installation
sudo mariadb -e "CREATE DATABASE matomo CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
sudo mariadb -e "CREATE USER 'matomo'@'localhost' IDENTIFIED BY 'matomo-db-secret';"
sudo mariadb -e "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON matomo.* TO 'matomo'@'localhost';"
cd /tmp
curl -LO https://builds.matomo.org/matomo.zip
sudo unzip -q matomo.zip -d /var/www
sudo chown -R www-data:www-data /var/www/matomoThe zip already contains a top level matomo folder, so it lands at /var/www/matomo. On the RHEL family use nginx:nginx for ownership. The grant list is the set of privileges Matomo uses day to day, which is narrower than ALL.
How do you lock down Nginx for Matomo?
Only the main interface, the tracker endpoint and static assets should be public; configuration, temporary files and libraries must stay hidden.
server {
listen 80;
listen [::]:80;
server_name stats.example.net;
root /var/www/matomo;
index index.php;
location ~ ^/(index|matomo|piwik|js/index)\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~* ^.+\.php$ { deny all; return 403; }
location ~ ^/(config|tmp|core|lang)/ { deny all; return 403; }
location ~ /\.ht { deny all; }
location ~ js/container_.*_preview\.js$ { expires off; add_header Cache-Control 'private, no-cache, no-store'; }
location ~ \.(gif|ico|jpg|png|svg|js|css|htm|html|mp3|mp4|wav|ogg|avi|ttf|eot|woff|woff2)$ {
allow all;
expires 1h;
add_header Pragma public;
add_header Cache-Control "public";
}
location / { try_files $uri $uri/ =404; }
}Enable the site, check with sudo nginx -t and reload. On Rocky and Alma swap the snippet for include fastcgi_params; plus a SCRIPT_FILENAME parameter and point at /run/php-fpm/www.sock.
How do you add HTTPS and run the installer?
Get the certificate before you open the wizard, so the first session and the stored site URL already use TLS. The details are in the Certbot guide.
sudo ufw allow 'Nginx Full'
sudo certbot --nginx -d stats.example.netUse firewall-cmd --permanent --add-service=http --add-service=https and a reload on Rocky and Alma. Now visit https://stats.example.net. The wizard runs a system check, asks for the database (host localhost, the user and database created above, table prefix matomo_), creates the super user and registers your first website. It ends by showing the JavaScript tracking snippet.
Add force_ssl = 1 under [General] in config/config.ini.php so the interface always redirects to HTTPS.
How do you add the tracking code to a site?
Paste the snippet from Administration, Websites, Tracking Code just before the closing head tag on every page you want to measure. It loads matomo.js from your stats subdomain and sends hits to matomo.php.
To check that it works, open the tracked site in a private window, then look at Visitors, Visits Log in Matomo; your visit should appear within seconds. If nothing shows up, open the browser developer tools on the tracked page, look for the request to matomo.php and confirm it returns 200 or 204. A 403 means the Nginx allow list above is missing an entry.
For sites that rely on consent banners, Matomo can run cookieless; enable it from Privacy, Anonymize data, and use the opt-out form on your privacy page.
How do you set up cron archiving?
Matomo builds reports either on page load in the dashboard or from the command line. The command line route is faster and avoids timeouts, so turn off browser archiving in System, General settings and add a cron job:
sudo tee /etc/cron.d/matomo-archive > /dev/null <<'EOF'
5 * * * * www-data /usr/bin/php /var/www/matomo/console core:archive --url=https://stats.example.net/ > /var/log/matomo-archive.log 2>&1
EOF
sudo touch /var/log/matomo-archive.log
sudo chown www-data /var/log/matomo-archive.log
sudo -u www-data php /var/www/matomo/console core:archive --url=https://stats.example.net/Use the nginx user on Rocky and Alma. The manual run at the end proves the job works and shows how long archiving takes on your data. Scheduling and cron syntax are explained in the cron guide.
How do you enable geolocation, email and updates?
Under System, Geolocation choose the DBIP or another free city database download; Matomo fetches and refreshes it automatically, and country and city reports start filling from then on.
Scheduled email reports and password resets need SMTP. Enter an authenticated relay on port 587 under System, General settings, as explained in SMTP relay for apps, and open a support ticket before you plan on port 25.
Updates appear as a banner in the interface. For a larger database run them from the shell to avoid web timeouts:
sudo -u www-data php /var/www/matomo/console core:update
sudo -u www-data php /var/www/matomo/console core:versionBack up the matomo database with mariadb-dump --single-transaction and copy config/config.ini.php; together they restore the whole installation.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Matomo web analytics
Does Matomo work with ad blockers?
Some blocklists include the default file names. Serving the tracker from your own subdomain helps, and server-side log import is an option for complete counts.
Can one Matomo server track many websites?
Yes. Add each site under Administration, Websites; every site gets its own ID in the tracking snippet.
Why do reports show zero visits for today?
Archiving has not run yet. Check the cron job and the log file, or run console core:archive by hand.
Can I import old server logs?
Yes. Matomo includes a log import script for Nginx and Apache access logs, useful for history before the tracker was added.
How much disk does Matomo use?
Raw visit logs grow with traffic. Set a deletion period for old raw data under Privacy so the database stays a predictable size.
More in Websites, CMS and shops
- WordPress on Nginx, PHP-FPM, MariaDB
- Fix common WordPress errors
- Back up WordPress (3-2-1 rule)
- WordPress caching with Redis
- WooCommerce shop setup
- Ghost publishing platform
- Moodle learning platform
- phpBB forum
- Flarum forum
- Discourse community forum
- MediaWiki on Nginx and MariaDB
- Magento Open Source with OpenSearch
- PrestaShop store on Nginx
- OpenCart shop on Nginx
- Static site with Nginx and rsync
- Joomla on Nginx and PHP-FPM
- Drupal with Composer and Drush
- Vanilla Forums open source