matomo-org / matomo-org/docker

`PHP_MEMORY_LIMIT` environment variable is not carried over to internally spawned `console` processes

Open
#382 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Shell
Stars
1k
Forks
384
PR merge metrics
No merged PRs in 30d

Description

# Issue

The docker image uses a `PHP_MEMORY_LIMIT` environment variable. It is used in the `/usr/local/etc/php/conf.d/php-matomo.ini` file in the line:

https://github.com/matomo-org/docker/blob/82fa89f13413cae2cc185416dce5ce4a6baeb618/apache/php.ini#L4

While this environment variable is being honored for general calls through the apache web server, it is not carried over when the API spawns another php process, e.g. when archiving with: `/usr/local/bin/php -q /var/www/html/console climulti:request -q --matomo-domain=mydomain.com --superuser module=API&method=CoreAdminHome.archiveReports&idSite=1&period=week&date=2025-01-13&format=json&segment=...` (a command generated internally by the Matomo API).

Since the variable is not set in the `php console ...` process, `memory_limit` is now set to an empty string, so PHP falls back to its default value (or maybe minimal value, since in my case, the warning appears when using more than 2 MiB). And for archiving processes, this might not be enough, resulting in warning/error messages like the following:

```
PHP Warning: Failed to set memory limit to 0 bytes (Current memory usage is 2097152 bytes) in Unknown on line 0
{"idarchives":[1700],"nb_visits":1411}
```

These are then returned as part of a JSON payload, leading to errors when deserializing:

```
Error unserializing the following response from ?module=API&method=CoreAdminHome.archiveReports&idSite=1&period=day&date=2024-08-01&format=json&trigger=archivephp: 'PHP Warning: Failed to set memory limit to 0 bytes (Current memory usage is 2097152 bytes) in Unknown on line 0 {"idarchives":[1700],"nb_visits":1411}'

Uncaught exception: /var/www/html/core/CronArchive.php(733): 859 total errors during this script execution, please investigate and try and fix these errors. [Query: , CLI mode: 1].
```

# Workaround

Use a customized dockerfile that sets the `memory_limit` setting explicitly to the desired value and ignore the `PHP_MEMORY_LIMIT` environment variable all together.

```dockerfile
FROM matomo:latest

# An explicit `memory_limit = 1` is currently needed and must be the last ini file read (thus the `zzz-` prefix),
# since some internal commands (like archiving) are not using the normal `index.php` environment, but a custom
# command line environment in which the `console` php script is called. And since matomo uses an ini file
# `php-matomo.ini` that contains the line `memory_limit = ${PHP_MEMORY_LIMIT}`, but the environment variable
# `PHP_MEMORY_LIMIT` is not being copied over to the custom command line environment, the `memory_limit`
# setting is set to an empty string, which then falls back to its default value (usually either `128M` or `2M`).
RUN echo 'memory_limit = -1' > /usr/local/etc/php/conf.d/zzz-php-matomo-custom.ini
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the referenced apache/php.ini file and inspect how the Docker image configures PHP for web requests versus the internally spawned /var/www/html/console process. Reproduce the provided archive command and check whether PHP_MEMORY_LIMIT reaches that process; done means the configured memory limit is honored and the warning no longer contaminates the JSON response.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, php
Domain
devops, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.