From 4de057cf19426a096104547cea0e48d250739c17 Mon Sep 17 00:00:00 2001 From: Mistral OZ - MIO Date: Tue, 3 Nov 2026 18:07:13 +0100 Subject: [PATCH] Detect the changes of the PHP_* variables in the php proxy without starting PHP --- CHANGELOG.md | 3 +++ Dockerfile.slim.apache | 4 ++-- Dockerfile.slim.cli | 4 ++-- Dockerfile.slim.fpm | 4 ++-- tests-suite/php-ini.sh | 6 +++++ utils/Dockerfile.slim.blueprint | 4 ++-- utils/check_php_env_var_changes.php | 34 ----------------------------- utils/docker-entrypoint-as-root.sh | 4 ++-- utils/php_env_fingerprint.sh | 7 ++++++ utils/php_env_var_cache.php | 3 --- utils/php_proxy.sh | 24 ++++++++++---------- 11 files changed, 38 insertions(+), 59 deletions(-) delete mode 100644 utils/check_php_env_var_changes.php create mode 100644 utils/php_env_fingerprint.sh delete mode 100644 utils/php_env_var_cache.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5d37fb..90c16afd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ ### Minor changes +* **2026-11-03** + * The `php` command is about 20 ms faster on each call: it detects the changes of the `PHP_*` environment variables without starting PHP a first time. The configuration is now also regenerated when a `PHP_*` variable is removed + * **2026-10-23** * Security: php2shell is fixed in the arm64 images too, now that the fixed PHP packages are published for arm64. The php2shell test fails again for a vulnerable image on every architecture * The tags of the PHP patch versions (e.g. `8.5.12-v5-cli`) are published again: they were no longer published since 2025-12-10. The `X.Y-v5-*` tags are not affected diff --git a/Dockerfile.slim.apache b/Dockerfile.slim.apache index 0a516ce8..f7cecd31 100644 --- a/Dockerfile.slim.apache +++ b/Dockerfile.slim.apache @@ -118,8 +118,8 @@ COPY utils/select_composer_version.sh /usr/local/bin/select_composer_version.sh COPY utils/utils.php /usr/local/bin/utils.php RUN mv /usr/bin/php /usr/bin/real_php COPY utils/php_proxy.sh /usr/bin/php -COPY utils/check_php_env_var_changes.php /usr/local/bin/check_php_env_var_changes.php -COPY utils/php_env_var_cache.php /opt/php_env_var_cache.php +COPY utils/php_env_fingerprint.sh /usr/local/bin/php_env_fingerprint.sh +RUN touch /opt/php_env_fingerprint COPY utils/generate_conf.php /usr/local/bin/generate_conf.php COPY utils/setup_extensions.php /usr/local/bin/setup_extensions.php diff --git a/Dockerfile.slim.cli b/Dockerfile.slim.cli index ae1d0737..92e66fca 100644 --- a/Dockerfile.slim.cli +++ b/Dockerfile.slim.cli @@ -118,8 +118,8 @@ COPY utils/select_composer_version.sh /usr/local/bin/select_composer_version.sh COPY utils/utils.php /usr/local/bin/utils.php RUN mv /usr/bin/php /usr/bin/real_php COPY utils/php_proxy.sh /usr/bin/php -COPY utils/check_php_env_var_changes.php /usr/local/bin/check_php_env_var_changes.php -COPY utils/php_env_var_cache.php /opt/php_env_var_cache.php +COPY utils/php_env_fingerprint.sh /usr/local/bin/php_env_fingerprint.sh +RUN touch /opt/php_env_fingerprint COPY utils/generate_conf.php /usr/local/bin/generate_conf.php COPY utils/setup_extensions.php /usr/local/bin/setup_extensions.php diff --git a/Dockerfile.slim.fpm b/Dockerfile.slim.fpm index 0c379670..619b8b56 100644 --- a/Dockerfile.slim.fpm +++ b/Dockerfile.slim.fpm @@ -118,8 +118,8 @@ COPY utils/select_composer_version.sh /usr/local/bin/select_composer_version.sh COPY utils/utils.php /usr/local/bin/utils.php RUN mv /usr/bin/php /usr/bin/real_php COPY utils/php_proxy.sh /usr/bin/php -COPY utils/check_php_env_var_changes.php /usr/local/bin/check_php_env_var_changes.php -COPY utils/php_env_var_cache.php /opt/php_env_var_cache.php +COPY utils/php_env_fingerprint.sh /usr/local/bin/php_env_fingerprint.sh +RUN touch /opt/php_env_fingerprint COPY utils/generate_conf.php /usr/local/bin/generate_conf.php COPY utils/setup_extensions.php /usr/local/bin/setup_extensions.php diff --git a/tests-suite/php-ini.sh b/tests-suite/php-ini.sh index 9929de7e..17075640 100755 --- a/tests-suite/php-ini.sh +++ b/tests-suite/php-ini.sh @@ -38,6 +38,12 @@ test_environmentErrorReporting() { [[ "$RESULT" == "error_reporting => 3 => 3" ]] assert_equals "error_reporting => 3 => 3" "$RESULT" "Wrong Environment error reporting" } +test_environmentChangedAfterStart() { + RESULT="$(docker run ${RUN_OPTIONS} --rm \ + "${REPO}:${TAG_PREFIX}${PHP_VERSION}-${BRANCH}-slim-${BRANCH_VARIANT}${ARCH_SUFFIX}" \ + bash -c 'PHP_INI_MEMORY_LIMIT=512M php -r "echo ini_get(\"memory_limit\"), \" \";"; php -r "echo ini_get(\"memory_limit\");"')" + assert_equals "512M -1" "$RESULT" "The configuration is not regenerated when a PHP_* variable is added or removed" +} ############################################################ ## PHP_INI_SESSION__SAVE_PATH ## Tests that environment variables with an equal sign are correctly handled diff --git a/utils/Dockerfile.slim.blueprint b/utils/Dockerfile.slim.blueprint index 0f4e75f5..a621a9e5 100644 --- a/utils/Dockerfile.slim.blueprint +++ b/utils/Dockerfile.slim.blueprint @@ -117,8 +117,8 @@ COPY utils/select_composer_version.sh /usr/local/bin/select_composer_version.sh COPY utils/utils.php /usr/local/bin/utils.php RUN mv /usr/bin/php /usr/bin/real_php COPY utils/php_proxy.sh /usr/bin/php -COPY utils/check_php_env_var_changes.php /usr/local/bin/check_php_env_var_changes.php -COPY utils/php_env_var_cache.php /opt/php_env_var_cache.php +COPY utils/php_env_fingerprint.sh /usr/local/bin/php_env_fingerprint.sh +RUN touch /opt/php_env_fingerprint COPY utils/generate_conf.php /usr/local/bin/generate_conf.php COPY utils/setup_extensions.php /usr/local/bin/setup_extensions.php diff --git a/utils/check_php_env_var_changes.php b/utils/check_php_env_var_changes.php deleted file mode 100644 index 058f3ebb..00000000 --- a/utils/check_php_env_var_changes.php +++ /dev/null @@ -1,34 +0,0 @@ - $value) { - if (substr($key, 0, strlen('PHP_')) === 'PHP_') { - if (!isset($phpEnvVarCache[$key])) { - // The env var does not exist in the cache. - $shouldGenerateConfig = true; - } else if ($phpEnvVarCache[$key] !== $value) { - // The value has changed. - $shouldGenerateConfig = true; - } - - $phpEnvVar[$key] = $value; - } -} - -if ($shouldGenerateConfig === false) { - echo "0"; - exit(0); -} - -$cacheFileContent = ' /dev/null +source /usr/local/bin/php_env_fingerprint.sh +php_env_fingerprint > /opt/php_env_fingerprint /usr/bin/real_php -d display_errors=stderr /usr/local/bin/generate_conf.php > /etc/php/${PHP_VERSION}/mods-available/generated_conf.ini PHP_VERSION="${PHP_VERSION}" /usr/bin/real_php -d display_errors=stderr /usr/local/bin/setup_extensions.php | sudo bash diff --git a/utils/php_env_fingerprint.sh b/utils/php_env_fingerprint.sh new file mode 100644 index 00000000..a59a5529 --- /dev/null +++ b/utils/php_env_fingerprint.sh @@ -0,0 +1,7 @@ +# Fingerprint of the PHP_* environment variables the PHP configuration is generated from +php_env_fingerprint() { + local name + for name in "${!PHP_@}"; do + printf '%s=%s\0' "$name" "${!name}" + done | md5sum +} diff --git a/utils/php_env_var_cache.php b/utils/php_env_var_cache.php deleted file mode 100644 index 0b67a5fe..00000000 --- a/utils/php_env_var_cache.php +++ /dev/null @@ -1,3 +0,0 @@ - /dev/null; then +source /usr/local/bin/php_env_fingerprint.sh + +# The configuration is regenerated only when the PHP_* environment variables changed since its last generation +FINGERPRINT=$(php_env_fingerprint) +read -r GENERATED_FINGERPRINT < /opt/php_env_fingerprint +if [[ "$FINGERPRINT" == "$GENERATED_FINGERPRINT" ]]; then exec /usr/bin/real_php "$@" fi -REGENERATE=$(/usr/bin/real_php -d display_errors=stderr /usr/local/bin/check_php_env_var_changes.php) - -if [[ "$REGENERATE" != "0" ]] && [[ "$REGENERATE" != "1" ]]; then - >&2 echo "Unexpected PHP proxy output:" - >&2 echo $REGENERATE - exit 1 +# Users that cannot use sudo run PHP with the current configuration +if ! sudo -n true 2> /dev/null; then + exec /usr/bin/real_php "$@" fi -if [[ "$REGENERATE" == "1" ]]; then - /usr/bin/real_php -d display_errors=stderr /usr/local/bin/generate_conf.php | sudo tee "/etc/php/${PHP_VERSION}/mods-available/generated_conf.ini" > /dev/null - PHP_VERSION="${PHP_VERSION}" /usr/bin/real_php -d display_errors=stderr /usr/local/bin/setup_extensions.php | sudo bash -fi +/usr/bin/real_php -d display_errors=stderr /usr/local/bin/generate_conf.php | sudo tee "/etc/php/${PHP_VERSION}/mods-available/generated_conf.ini" > /dev/null +PHP_VERSION="${PHP_VERSION}" /usr/bin/real_php -d display_errors=stderr /usr/local/bin/setup_extensions.php | sudo bash +echo "$FINGERPRINT" | sudo tee /opt/php_env_fingerprint > /dev/null exec /usr/bin/real_php "$@"