Skip to content

Commit 7bbbd64

Browse files
committed
Switch to custom Matomo image
So that we can customize it with plugins we need etc.
1 parent d01d5cf commit 7bbbd64

File tree

12 files changed

+254
-18
lines changed

12 files changed

+254
-18
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
name: "Build Nginx image and push it to registry"
3+
4+
on:
5+
push:
6+
branches:
7+
- 'main'
8+
paths:
9+
- '.github/workflows/build-push-image-matomo.yml'
10+
- 'docker/images/matomo/**'
11+
workflow_dispatch:
12+
13+
jobs:
14+
push-image:
15+
if: github.repository == 'utrechtuniversity/matomo-ansible'
16+
runs-on: ubuntu-22.04
17+
permissions:
18+
contents: read
19+
packages: write
20+
steps:
21+
- name: Extract branch name
22+
shell: bash
23+
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
24+
id: extract_branch
25+
26+
- name: Check out Matomo Ansible repository
27+
uses: actions/checkout@v6
28+
with:
29+
path: matomo-ansible
30+
repository: UtrechtUniversity/matomo-ansible
31+
ref: ${{ steps.extract_branch.outputs.branch }}
32+
33+
- name: Authenticate to the container registry
34+
uses: docker/login-action@v3
35+
with:
36+
registry: ghcr.io
37+
username: ${{ github.actor }}
38+
password: ${{ secrets.GITHUB_TOKEN }}
39+
40+
- name: Build and push Docker image
41+
uses: docker/build-push-action@v6
42+
with:
43+
context: matomo-ansible/docker/images/matomo
44+
file: matomo-ansible/docker/images/matomo/Dockerfile
45+
push: true
46+
tags: ghcr.io/utrechtuniversity/matomo-app:latest

docker/.env

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# Configuration of Matomo
22

33
# Application configuration
4-
MATOMO_HOST=www.matomo.test
4+
MATOMO_HOST="www.matomo.test"
55
MATOMO_HOST_IP=127.0.0.1
66
MATOMO_HOST_PORT=443
77

8+
MATOMO_FIRST_SITE_NAME=test
9+
MATOMO_FIRST_SITE_URL=https://www.matomo.test
10+
MATOMO_FIRST_USER_NAME=uuadmin
11+
MATOMO_FIRST_USER_EMAIL=yoda@uu.nl
12+
MATOMO_FIRST_USER_PASSWORD=admin
13+
814
# Database configuration
915
MARIADB_PASSWORD=testtest
1016
MARIADB_ROOT_PASSWORD=testtest

docker/build-local-images.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ set -e
33

44
cd images
55

6-
for image in nginx mta
6+
for image in nginx mta matomo
77
do cd "$image"
88
echo "Building image $image ..."
9-
./build.sh
9+
./build.sh $*
1010
cd ..
1111
done
1212

docker/docker-compose.yml

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
---
2+
3+
volumes:
4+
matomo_config:
5+
matomo_db_data:
6+
nginx_config:
7+
28
services:
39
database:
10+
container_name: database
411
image: mariadb:12
512
command: --max-allowed-packet=64MB
613
restart: always
714
volumes:
8-
- database:/var/lib/mysql:Z
15+
- matomo_db_data:/var/lib/mysql:Z
916
environment:
1017
- MARIADB_AUTO_UPGRADE=1
1118
- MARIADB_DATABASE=matomo
@@ -16,19 +23,26 @@ services:
1623
- MARIADB_USER=matomo
1724

1825
matomo:
19-
image: matomo:5.4.0
26+
container_name: matomo
27+
image: ghcr.io/utrechtuniversity/matomo-app:latest
2028
restart: always
2129
depends_on:
2230
- database
2331
volumes:
24-
- matomo:/var/www/html:z
32+
- matomo_config:/var/www/html/matomo/config:z
2533
environment:
2634
- MATOMO_DATABASE_ADAPTER=mysql
2735
- MATOMO_DATABASE_DBNAME=matomo
2836
- MATOMO_DATABASE_HOST=database
2937
- MATOMO_DATABASE_PASSWORD=${MARIADB_PASSWORD}
3038
- MATOMO_DATABASE_TABLES_PREFIX=matomo_
3139
- MATOMO_DATABASE_USERNAME=matomo
40+
- MATOMO_HOST=${MATOMO_HOST}
41+
- MATOMO_FIRST_SITE_NAME=${MATOMO_FIRST_SITE_NAME}
42+
- MATOMO_FIRST_SITE_URL=${MATOMO_FIRST_SITE_URL}
43+
- MATOMO_FIRST_USER_NAME=${MATOMO_FIRST_USER_NAME}
44+
- MATOMO_FIRST_USER_EMAIL=${MATOMO_FIRST_USER_EMAIL}
45+
- MATOMO_FIRST_USER_PASSWORD=${MATOMO_FIRST_USER_PASSWORD}
3246
- PHP_MEMORY_LIMIT=2048M
3347

3448
nginx:
@@ -64,9 +78,3 @@ services:
6478
- POSTFIX_ORIGIN=${POSTFIX_ORIGIN}
6579
sysctls:
6680
net.ipv4.ip_unprivileged_port_start: 0
67-
68-
69-
volumes:
70-
database:
71-
matomo:
72-
nginx_config:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<VirtualHost *:80>
2+
# The ServerName directive sets the request scheme, hostname and port that
3+
# the server uses to identify itself. This is used when creating
4+
# redirection URLs. In the context of virtual hosts, the ServerName
5+
# specifies what hostname must appear in the request's Host: header to
6+
# match this virtual host. For the default virtual host (this file) this
7+
# value is not decisive as it is used as a last resort host regardless.
8+
# However, you must set it for any further virtual host explicitly.
9+
ServerName SERVERNAME
10+
11+
DocumentRoot /var/www/html/matomo
12+
13+
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
14+
# error, crit, alert, emerg.
15+
# It is also possible to configure the loglevel for particular
16+
# modules, e.g.
17+
#LogLevel info ssl:warn
18+
19+
ErrorLog ${APACHE_LOG_DIR}/error.log
20+
CustomLog ${APACHE_LOG_DIR}/access.log combined
21+
22+
# For most configuration files from conf-available/, which are
23+
# enabled or disabled at a global level, it is possible to
24+
# include a line for only one particular virtual host. For example the
25+
# following line enables the CGI configuration for this host only
26+
# after it has been globally disabled with "a2disconf".
27+
#Include conf-available/serve-cgi-bin.conf
28+
</VirtualHost>
29+

docker/images/matomo/Dockerfile

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
FROM ubuntu:24.04
2+
LABEL maintainer="Yoda team <yoda@uu.nl>"
3+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
4+
5+
# Application settings
6+
ENV MATOMO_VERSION="5.6.1"
7+
ENV MATOMO_EXTRATOOLS_PLUGIN_VERSION="5.1.5"
8+
ENV MATOMO_CLASSICCOUNTER_PLUGIN_VERSION="0.4.0"
9+
10+
# Network settings
11+
EXPOSE 80
12+
13+
# Update packages
14+
RUN apt-get update
15+
16+
# Install common tools
17+
# hadolint ignore=DL3033
18+
RUN apt-get install -y wget git vim sudo nmap crudini
19+
20+
## Install Apache
21+
# hadolint ignore=DL3033
22+
RUN apt-get install -y apache2 apache2-dev libapache2-mod-php && \
23+
a2enmod headers && \
24+
a2enmod rewrite
25+
COPY 001-matomo.conf /etc/apache2/sites-available/001-matomo.conf
26+
RUN a2dissite 000-default.conf && \
27+
a2ensite 001-matomo.conf
28+
29+
## Install PHP dependencies
30+
RUN sudo apt-get install -y php php-curl php-gd php-cli php-mysql php-xml php-mbstring
31+
32+
## Install Matomo
33+
RUN cd /tmp && \
34+
wget "https://builds.matomo.org/matomo-${MATOMO_VERSION}.tar.gz" && \
35+
cd /var/www/html && \
36+
tar xvfz "/tmp/matomo-${MATOMO_VERSION}.tar.gz" && \
37+
rm "/tmp/matomo-${MATOMO_VERSION}.tar.gz"
38+
39+
## Install Matomo plugins
40+
RUN cd /var/www/html/matomo/plugins && \
41+
git clone --branch "$MATOMO_EXTRATOOLS_PLUGIN_VERSION" https://github.com/Digitalist-Open-Cloud/Matomo-Plugin-ExtraTools.git ExtraTools && \
42+
git clone --branch "$MATOMO_CLASSICCOUNTER_PLUGIN_VERSION" https://github.com/Findus23/plugin-ClassicCounter.git Counter && \
43+
echo "[General]" > /var/www/html/matomo/config/common.config.ini.php && \
44+
echo "always_load_commands_from_plugin=ExtraTools" >> /var/www/html/matomo/config/common.config.ini.php && \
45+
chown -R www-data:www-data /var/www/html/matomo &&\
46+
find /var/www/html/matomo/tmp -type f -exec chmod 644 {} \; &&\
47+
find /var/www/html/matomo/tmp -type d -exec chmod 755 {} \;
48+
49+
# Install init script
50+
# hadolint ignore=DL3033
51+
COPY matomo-init.sh /matomo-init.sh
52+
RUN chmod 0755 /matomo-init.sh
53+
54+
VOLUME [ "/sys/fs/cgroup" ]
55+
CMD exec /matomo-init.sh

docker/images/matomo/build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
docker build -t ghcr.io/utrechtuniversity/matomo-app:latest . $*
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
3+
set -o pipefail
4+
set -u
5+
6+
function start_service {
7+
apache2ctl -D FOREGROUND || true
8+
echo "Error: Apache either terminated or would not start. Keeping container running for troubleshooting purposes."
9+
sleep infinity
10+
}
11+
12+
function before_update {
13+
echo -e "[...] ${1}"
14+
}
15+
16+
function progress_update {
17+
GREEN='\033[0;32m'
18+
RESET='\033[0m'
19+
echo -e "[ ${GREEN}\xE2\x9C\x94${RESET} ] ${1}"
20+
}
21+
22+
check_port() {
23+
local host="$1"
24+
local port="$2"
25+
26+
echo "Checking service availability at ${host}:${port}..."
27+
28+
while true; do
29+
# Use nmap to scan the specified port on the given host
30+
nmap -p "${port}" "${host}" | grep -q "open"
31+
32+
# If the port is open, exit the loop
33+
if [ $? -eq 0 ]; then
34+
echo "Server is available at ${host}:${port}"
35+
break
36+
else
37+
echo "Server at ${host}:${port} is not available. Retrying in 1 seconds..."
38+
sleep 1
39+
fi
40+
done
41+
}
42+
43+
## Check DB up
44+
check_port database 3306
45+
46+
set -e
47+
48+
if ! [[ -f /container_initialized ]]
49+
then before_update "Setting up Apache vhost"
50+
perl -pi -e '$servername=$ENV{MATOMO_HOST}; s/SERVERNAME/$servername/' /etc/apache2/sites-available/001-matomo.conf
51+
before_update "Initializing Matomo."
52+
cd /var/www/html/matomo
53+
./console matomo:install --no-interaction \
54+
--db-username="$MATOMO_DATABASE_USERNAME" \
55+
--db-pass="MATOMO_DATABASE_PASSWORD" \
56+
--db-host="$MATOMO_DATABASE_HOST" \
57+
--db-port=3306 \
58+
--db-name="$MATOMO_DATABASE_DBNAME" \
59+
--first-site-name="$MATOMO_FIRST_SITE_NAME" \
60+
--first-site-url="$MATOMO_FIRST_SITE_URL" \
61+
--first-user="$MATOMO_FIRST_USER_NAME" \
62+
--first-user-email="$MATOMO_FIRST_USER_EMAIL" \
63+
--first-user-pass="$MATOMO_FIRST_USER_PASSWORD"
64+
crudini --set /var/www/html/matomo/config/config.ini.php General force_ssl 1
65+
crudini --set /var/www/html/matomo/config/config.ini.php General assume_secure_protocol 1
66+
crudini --set /var/www/html/matomo/config/config.ini.php General proxy_client_headers[] HTTP_X_FORWARDED_FOR
67+
crudini --set /var/www/html/matomo/config/config.ini.php General proxy_host_headers[] HTTP_X_FORWARDED_HOST
68+
chmod 0644 /var/www/html/matomo/config/config.ini.php
69+
chown www-data:www-data /var/www/html/matomo/config/config.ini.php
70+
touch /container_initialized
71+
fi
72+
73+
before_update "Initialization complete. Starting Apache"
74+
start_service

docker/images/mta/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ RUN dpkg-reconfigure --frontend=noninteractive locales
2323
RUN update-locale LANG=${LC_ALL}
2424

2525
# Install common dependencies
26-
RUN apt -y install curl golang git && \
26+
RUN apt -y install curl golang git iproute2 && \
2727
curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash && \
2828
source /root/.nvm/nvm.sh && \
2929
nvm install v18

docker/images/nginx/nginx.site.part1

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ server {
1212

1313
server {
1414

15-
listen 443 ssl http2;
16-
listen [::]:443 ssl http2;
15+
listen 443 ssl;
16+
listen [::]:443 ssl;
17+
http2 on;
1718

1819
ssl_certificate /etc/certificates/matomo.pem;
1920
ssl_certificate_key /etc/certificates/matomo.key;
@@ -43,9 +44,13 @@ server {
4344

4445
location / {
4546
proxy_pass http://matomo:80/;
46-
proxy_set_header Host PUT_HOST_HEADER_HERE;
47-
proxy_set_header X-Forwarded-Proto https;
48-
proxy_set_header Referer $http_referer;
47+
48+
proxy_set_header Host $host;
49+
proxy_set_header X-Forwarded-Host $host;
50+
proxy_set_header X-Forwarded-Proto $scheme;
51+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
52+
proxy_set_header X-Real-IP $remote_addr;
53+
4954
proxy_cache cache;
5055
proxy_cache_bypass $cookie_auth_tkt;
5156
proxy_no_cache $cookie_auth_tkt;

0 commit comments

Comments
 (0)