In today’s digital age, web analytics have become an essential tool for businesses to analyze and understand their online presence. Matomo, an open-source web analytics software, provides valuable insights into website visits, user behavior, and audience analysis. If you’re looking for an alternative to popular analytics services like Google Analytics, Matomo is a powerful solution that you can install and host on your own server.
In this comprehensive guide, we will walk you through the process of setting up and securing Matomo web analytics on a Ubuntu 22.04 server. By the end of this tutorial, you will have a self-hosted Matomo installation that allows you to track and monitor traffic on your websites, empowering you with valuable data to make informed business decisions.
Prerequisites
Before we dive into the installation process, make sure you have the following prerequisites in place:
- Ubuntu 22.04 server: This tutorial assumes that you have a Ubuntu server up and running, with a hostname assigned (e.g.,
matomo-server
). - Non-root user with sudo privileges: It is recommended to create a non-root user with sudo privileges to perform the installation and configuration steps.
- Domain name: You will need a domain name pointed to your server’s IP address. For this guide, we will use the sub-domain
analytics.example.io
as an example.
Once you have met these requirements, you are ready to proceed with the installation and configuration of Matomo web analytics.
Step 1: Installing and Configuring MariaDB Server
Matomo requires a database backend to store and retrieve analytics data. In this guide, we will use MariaDB, a popular and open-source relational database management system. To get started, follow these steps:
- Update your package index by running the following command:
sudo apt update
- Install MariaDB server by running the following command:
sudo apt install mariadb-server
- Verify that the MariaDB server is running by executing the following command:
sudo systemctl status mariadb
- Secure your MariaDB installation by running the
mysql_secure_installation
script:
sudo mysql_secure_installation
This script will guide you through the process of setting a root password, removing anonymous users, disallowing remote root login, and more.
- Log in to the MariaDB shell as the root user by running the following command:
sudo mysql -u root -p
- Create a new database and user for Matomo by executing the following queries:
CREATE DATABASE matomo; CREATE USER 'matomo'@'localhost' IDENTIFIED BY 'p4ssw0rddb'; GRANT ALL PRIVILEGES ON matomo.* TO 'matomo'@'localhost'; FLUSH PRIVILEGES;
- Exit the MariaDB shell by typing
quit
and pressing Enter.
With these steps, you have installed and configured MariaDB as the database backend for your Matomo installation. Next, we will proceed with setting up the Apache2 web server and PHP.
Step 2: Installing Apache2 Web Server and PHP
To run Matomo, you need a web server that can handle PHP scripts. In this guide, we will use Apache2 as the web server and PHP 8.1 as the scripting language. Follow these steps to install and configure Apache2 and PHP:
- Install Apache2 and PHP 8.1 packages by running the following command:
sudo apt install apache2 php php-cli libapache2-mod-php php-common php-curl php-gd php-mbstring php-mysql php-xml php-intl php-zip wget unzip
- Verify that the Apache2 service is running by executing the following command:
sudo systemctl status apache2
- Open the
php.ini
file for Apache2 in a text editor:
sudo nano /etc/php/8.1/apache2/php.ini
- Modify the following settings in the
php.ini
file:
- Set the
date.timezone
to your desired timezone (e.g.,Europe/Stockholm
). - Adjust the
memory_limit
value according to your server’s resources. - Optionally, modify other settings such as
upload_max_filesize
andmax_execution_time
if needed.
- Save the changes and exit the text editor.
- Restart the Apache2 service to apply the new PHP configuration:
sudo systemctl restart apache2
- Verify the PHP version and enabled extensions by running the following commands:
sudo php --version sudo php -m
With Apache2 and PHP installed and configured, you are now ready to download the Matomo source code and set up the necessary permissions.
Step 3: Downloading Matomo Source Code
To install Matomo, you need to download its source code and configure the appropriate permissions. Follow these steps to download and set up Matomo:
- Change your current working directory to
/var/www/
:
cd /var/www/
- Download the Matomo source code using the following command:
sudo wget https://builds.matomo.org/matomo.zip
- Extract the downloaded zip file using the following command:
sudo unzip matomo.zip
- Set the ownership of the Matomo installation directory to the
www-data
user and group:
sudo chown -R www-data:www-data /var/www/matomo
With the Matomo source code downloaded and the permissions set correctly, we can now proceed to configure the virtual host for Matomo.
Step 4: Setting Up Virtual Host for Matomo
To access Matomo via a web browser, you need to set up a virtual host that maps the domain name to the Matomo installation directory. Follow these steps to configure the virtual host:
- Create a new Apache2 virtual host configuration file:
sudo nano /etc/apache2/sites-available/matomo.conf
- Add the following configuration to the file, replacing
analytics.example.io
with your desired domain name:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName analytics.example.io
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 the file and exit the text editor.
- Enable the virtual host configuration by running the following command:
sudo a2ensite matomo.conf
- Verify the Apache2 configuration for any syntax errors:
sudo apachectl configtest
- Restart the Apache2 service to apply the changes:
sudo systemctl restart apache2
With the virtual host configured, you can now access Matomo through your domain name. However, we still need to secure the installation with SSL/TLS certificates.
Step 5: Securing Matomo with SSL/TLS Certificates
To ensure secure communication between your users and the Matomo installation, we will use Certbot and Let’s Encrypt to generate SSL/TLS certificates. Follow these steps to secure your Matomo installation:
- Install Certbot and the Certbot plugin for Apache2:
sudo apt install certbot python3-certbot-apache2
- Generate SSL/TLS certificates for your domain name by running the following command:
sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email test@example.com -d analytics.example.io
- Follow the on-screen instructions to complete the certificate generation process.
With SSL/TLS certificates in place, your Matomo installation is now accessible over a secure HTTPS connection. You can proceed to the final step of the installation process.
Step 6: Installing Matomo Web Analytics
In this final step, we will install Matomo web analytics and perform the initial setup. Follow these steps to complete the installation:
- Open your web browser and visit your Matomo domain name (e.g.,
https://analytics.example.io
). - On the welcome page, click “Next” to start the installation process.
- Matomo will check the server environment and requirements. Ensure that all requirements are met and properly configured.
- Set up the MariaDB database details by providing the correct database username and password.
- Matomo will automatically create the necessary database tables if the database details are correct.
- Create a new admin user for Matomo by providing a strong password.
- Configure the first website tracking by entering the domain name and selecting the timezone.
- Matomo will generate JavaScript code that you need to add to your website(s) for tracking.
- After completing the installation, click “CONTINUE TO MATOMO” to access the Matomo administration interface.
- Log in with your Matomo admin username and password.
- From the administration interface, you can manage your websites, view tracking data, and access various analytics reports.
Congratulations! You have successfully installed Matomo web analytics on your Ubuntu 22.04 server. You can now track and analyze website traffic using your self-hosted Matomo installation.
Conclusion
In this article, we have covered the step-by-step process of setting up and securing Matomo web analytics on a Ubuntu 22.04 server. By following this guide, you have created a powerful analytics platform that provides valuable insights into your website’s performance and user behavior.
With Matomo installed, you can integrate it with popular content management systems (CMS) like WordPress, TYPO3, Joomla, Drupal, and eCommerce applications such as Magento, PrestaShop, WooCommerce, and OpenCart. Matomo’s flexibility and self-hosted nature give you complete control over your analytics data, ensuring privacy and security.
If you’re looking for reliable and scalable cloud hosting solutions, consider Shape.host. They offer Cloud VPS services that provide top-notch performance and security for your web applications. With Shape.host, you can focus on your business while they handle the infrastructure. Visit Shape.host today to learn more.