Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/_build-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -709,12 +709,14 @@ jobs:
# OS security patches) but never cross a tooling major — pin them for
# stable behavior without CVE rot. Exact reproducibility = SHA tags.
# ─────────────────────────────────────────────────────────────────────────
- name: Checkout versions.json and CVE baseline
- name: Checkout versions.json, CVE baseline and smoke-test scenario
uses: actions/checkout@v7
with:
sparse-checkout: |
versions.json
.trivyignore
tests/e2e/lib/
tests/e2e/scenarios/test-memory-limits.sh
sparse-checkout-cone-mode: false

- name: Resolve release channel
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/build-php-fpm-nginx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ jobs:
! docker exec test-slim php -m | grep -q imagick || echo "Warning: imagick found in slim tier"
! docker exec test-slim node --version 2>/dev/null || echo "Warning: node found in slim tier"
docker stop test-slim && docker rm test-slim
# php-fpm must start under a memory limit, where autotune sizes the pool
# small - /healthz is nginx-only and cannot see a dead php-fpm.
bash tests/e2e/scenarios/test-memory-limits.sh
secrets: inherit

build-slim-rootless-matrix:
Expand Down Expand Up @@ -137,6 +140,9 @@ jobs:
docker exec test-slim-rootless curl -sf http://127.0.0.1:8080/healthz || { docker logs test-slim-rootless && exit 1; }
docker exec test-slim-rootless php -v | grep "PHP $PHP_VERSION" || exit 1
docker stop test-slim-rootless && docker rm test-slim-rootless
# php-fpm must start under a memory limit, where autotune sizes the pool
# small - /healthz is nginx-only and cannot see a dead php-fpm.
bash tests/e2e/scenarios/test-memory-limits.sh
secrets: inherit

# =====================================================================
Expand Down Expand Up @@ -196,6 +202,9 @@ jobs:
docker exec test-multi composer --version || exit 1
docker exec test-multi node --version || exit 1
docker stop test-multi && docker rm test-multi
# php-fpm must start under a memory limit, where autotune sizes the pool
# small - /healthz is nginx-only and cannot see a dead php-fpm.
bash tests/e2e/scenarios/test-memory-limits.sh
secrets: inherit

build-rootless-matrix:
Expand Down Expand Up @@ -253,6 +262,9 @@ jobs:
docker exec test-rootless composer --version || exit 1
docker exec test-rootless node --version || exit 1
docker stop test-rootless && docker rm test-rootless
# php-fpm must start under a memory limit, where autotune sizes the pool
# small - /healthz is nginx-only and cannot see a dead php-fpm.
bash tests/e2e/scenarios/test-memory-limits.sh
secrets: inherit

# =====================================================================
Expand Down Expand Up @@ -308,6 +320,9 @@ jobs:
# Chromium tier should have chromium
docker exec test-chromium which chromium-browser || docker exec test-chromium which chromium
docker stop test-chromium && docker rm test-chromium
# php-fpm must start under a memory limit, where autotune sizes the pool
# small - /healthz is nginx-only and cannot see a dead php-fpm.
bash tests/e2e/scenarios/test-memory-limits.sh
secrets: inherit

build-chromium-rootless-matrix:
Expand Down Expand Up @@ -362,6 +377,9 @@ jobs:
# Chromium check
docker exec test-chromium-rootless which chromium-browser || docker exec test-chromium-rootless which chromium
docker stop test-chromium-rootless && docker rm test-chromium-rootless
# php-fpm must start under a memory limit, where autotune sizes the pool
# small - /healthz is nginx-only and cannot see a dead php-fpm.
bash tests/e2e/scenarios/test-memory-limits.sh
secrets: inherit

# =====================================================================
Expand Down Expand Up @@ -418,6 +436,9 @@ jobs:
# Dev tier should have chromium
docker exec test-dev which chromium-browser || docker exec test-dev which chromium
docker stop test-dev && docker rm test-dev
# php-fpm must start under a memory limit, where autotune sizes the pool
# small - /healthz is nginx-only and cannot see a dead php-fpm.
bash tests/e2e/scenarios/test-memory-limits.sh
secrets: inherit

build-dev-rootless-matrix:
Expand Down Expand Up @@ -473,6 +494,9 @@ jobs:
# Chromium check
docker exec test-dev-rootless which chromium-browser || docker exec test-dev-rootless which chromium
docker stop test-dev-rootless && docker rm test-dev-rootless
# php-fpm must start under a memory limit, where autotune sizes the pool
# small - /healthz is nginx-only and cannot see a dead php-fpm.
bash tests/e2e/scenarios/test-memory-limits.sh
secrets: inherit

# =====================================================================
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ on:
- 'health-checks'
- 'image-formats'
- 'app-processes'
- 'memory-limits'
- 'nginx-template'

env:
Expand Down Expand Up @@ -233,6 +234,16 @@ jobs:
# queue worker runs as or where it starts.
tests/e2e/run-e2e-tests.sh "$IMAGE" app-processes

- name: Run memory-limits E2E test
env:
IMAGE: test-image:local
VERBOSE: "true"
run: |
# php-fpm under -m 256m/512m, where autotune sizes the pool below
# the old spare defaults. Plain `docker run` has no limit, so every
# other test here sizes a pool large enough to hide that.
tests/e2e/run-e2e-tests.sh "$IMAGE" memory-limits

# Full matrix test (manual trigger)
full-matrix:
if: github.event_name == 'workflow_dispatch'
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to Cbox PHP Base Images.
- **php-base installs every extension through PIE instead of `pecl`** ([#23](https://github.com/cboxdk/php-baseimages/issues/23)) - the `pecl` binary does not exist in `php:8.6` images (`--with-pear` dropped, PECL formally deprecated, docker-library/php#1682) and the PECL network service has a stated shelf life, so this had to happen whether or not 8.6 is added. Package names are not mechanical - pecl `redis` is PIE `phpredis/phpredis` - and each was verified against Packagist before the switch. PIE is delivered as a pinned, sha256-verified phar through `scripts/download-pie.sh`, the same pattern as cbox-init and fpm-exporter, rather than bind-mounted from a floating registry tag as upstream's examples do: a floating tag is neither reproducible nor dependably available (ghcr's blob CDN returned 502 for both `php/pie` tags for twenty minutes while this was written). The phar is bind-mounted from the build context, so none of its 8.6 MB lands in a layer. All three tiers verified by build: slim, standard and dev carry the same extensions at the same pinned versions as before, opentelemetry still lands in `conf.d-otel/` rather than the scan dir, and the dev tier's own xdebug/spx/pcov ini files still overwrite what PIE wrote - PIE uses the same `docker-php-ext-<name>.ini` filename `docker-php-ext-enable` did, which was measured before anything was migrated

### Fixed
- **php-fpm crash-looped in any container whose memory limit autotuned the pool below 6 workers** - a 512Mi Kubernetes pod (Reverb on `php-fpm-nginx:8.4-bookworm`) failed with `pm.min_spare_servers(1) and pm.max_spare_servers(6) cannot be greater than pm.max_children(4)` / `FPM initialization failed`, exit 78, and `docker run -m 256m` or `-m 512m` reproduces it on the published images. cbox-init's boot autotune was right: it sized the pool to 4 children with spare counts 2/1/2. But it runs AFTER the entrypoint, which had already written the pre-tune defaults (start 2, min 1, max 6) into `zz-pm-mode.conf` as literal numbers, while `pm.max_children` in the pool file stayed an `${PHP_FPM_MAX_CHILDREN}` placeholder that FPM expands from cbox-init's environment. So `max_children` was tuned and the spare counts were not. The dynamic drop-in is now written with placeholders, the same as the baked one, so FPM takes all four from the tuned environment. Manual sizing had the same trap from a different side: `PHP_FPM_MAX_CHILDREN=4` turns autotune off and kept `max_spare` at 6, so the entrypoint now clamps the spare counts to fit `max_children` and logs a `WARN` with the values it used. Nothing caught it because every smoke test ran without a memory limit, where the medium profile lands above 6 workers, and probed `/healthz`, which nginx answers without asking php-fpm. New `test-memory-limits.sh` starts the image under `-m 256m` and `-m 512m` and with `PHP_FPM_MAX_CHILDREN=2`, and passes only when `/fpm-ping` answers and FPM never refused its config. It runs in the e2e smoke test and before every php-fpm-nginx tier is promoted: 3 failures on the published 8.4 image, 0 after
- **`SSL_MODE=on` made the container fail to start at all** ([#55](https://github.com/cboxdk/php-baseimages/issues/55)) - the generated HTTPS server used the standalone `http2 on;` directive, which nginx only learned in 1.25.1, while the image ships Debian bookworm's nginx 1.22.1. The result was not a degraded TLS setup but `[emerg] unknown directive "http2"` and a container that never booted, on every tier, whenever TLS was enabled. Nothing caught it because every other e2e scenario runs plain HTTP - TLS had no test at all. The entrypoint now reads `nginx -v` and picks the form that binary understands: `listen ... ssl http2` below 1.25.1, `http2 on;` from 1.25.1 up. Deliberately version-detected rather than reverted to the old form, because the trixie move ([#52](https://github.com/cboxdk/php-baseimages/issues/52)) brings nginx 1.26+ where the listen parameter is deprecated and the directive is correct - this way that migration needs no second edit here. `tests/e2e/scenarios/test-ssl.sh` boots with `SSL_MODE=on` and `=full`, fetches a page over TLS, asserts the HTTP-to-HTTPS redirect, and checks that the rendered config matches the running nginx in both directions; it also asserts that no TLS server is rendered when `SSL_MODE` is unset. That test immediately found a second, latent bug: `SSL_MODE=full` writes an HTTP-to-HTTPS redirect server that nginx never reaches, because `conf.d` loads alphabetically and `default.conf`'s HTTP server is the first-defined default server for the port. Filed as [#58](https://github.com/cboxdk/php-baseimages/issues/58) rather than fixed here, because the obvious remedies all shadow `/healthz`, which the container healthcheck reaches over plaintext on loopback - that needs a decision, not a patch at the end of an unrelated PR. Nobody can have depended on the redirect: until this release `SSL_MODE=on` and `=full` both aborted startup, so no bookworm container ever ran with TLS at all
- **Laravel storage on a symlinked shared directory: the scaffold stayed root-owned while shared files were rewritten on every start** ([#54](https://github.com/cboxdk/php-baseimages/issues/54)) - on a release layout, `releases/<id>/storage` is a symlink to `shared/storage`, and GNU `chown -R` and `chmod -R` disagree about a symlink named on the command line. Measured in bookworm rather than assumed: `chown -R www-data release/storage` left a just-scaffolded `storage/app/public` as `root:root`, because `chown -R` defaults to `-P` and will not traverse it, so the app user could not write there; `chmod -R 775 release/storage` *did* follow the symlink and took an existing 644 file in shared storage to 775, on every single container start - sessions, logs, caches and uploads included. One operation did nothing and the other did far too much, and both were invisible without a symlink in the picture, which is precisely why every other Laravel test uses a plain directory. The startup now resolves the path with `realpath` once so the scaffold, the chown and the mode fix all act on the same real directory; skips the recursive walk when the tree is already owned by the app user, the same shortcut `setup_user_permissions` uses for mounted volumes; and applies 775 to directories and 664 to files via `find` instead of marking every log file executable. `tests/e2e/scenarios/test-laravel-storage.sh` builds a real symlinked release tree and asserts the scaffolded directory's owner through the symlink, that a pre-existing 644 file keeps its mode, that directories are 775, that php-fpm's user can write what the scaffold created, and that a second start skips the walk. `docs/guides/symlink-releases.md` documents the resolved behaviour next to the layout that triggers it
- **`cgi.fix_pathinfo=0` made every PHP request 403 on the default template** ([#49](https://github.com/cboxdk/php-baseimages/issues/49)) - the template sent `PATH_TRANSLATED` unconditionally as the script root plus `$fastcgi_path_info`. That path info is empty on every request here, because the PHP location is `~ \.php$` and so cannot match a URI with anything after the `.php` - frameworks are reached through `try_files` as `/index.php` with a query string instead. The value was therefore the web root DIRECTORY, and with `cgi.fix_pathinfo=0` PHP-FPM takes the script from `PATH_TRANSLATED` rather than `SCRIPT_FILENAME`, finds a directory, and `security.limit_extensions` refuses it: `Access to the script '<webroot>' has been denied`, 403 on everything. `cgi.fix_pathinfo=0` is a common hardening and is not set by the image's own ini, which is why nothing caught it; it predates the `$realpath_root` change and was equally broken with `$document_root`. The param is now built through a map and sent `if_not_empty`, so it is omitted when there is no path info and still correct if that location pattern is ever widened. Fixed in the template and in both baked fallback configs; `test-nginx-template.sh` grew a case that mounts `cgi.fix_pathinfo=0` and asserts a 200, no denial in the php-fpm log, and that the param is absent rather than empty - 3 failures on the published image, 0 after
Expand Down
55 changes: 48 additions & 7 deletions common/lib/entrypoint-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,62 @@ fix_symfony_permissions() {
# belong to another mode. Called by both the php-fpm and php-fpm-nginx
# entrypoints after sizing envs are resolved.
###########################################

# FPM refuses to start a dynamic pool unless
# 1 <= min_spare <= start_servers <= max_spare <= max_children
# and the spare defaults (start 2, min 1, max 6) do not fit a small
# max_children: PHP_FPM_MAX_CHILDREN=4 alone was enough to crash-loop it.
# Clamp the exported values into that order, keeping max_children - the
# number somebody actually chose - and saying what changed. Non-numeric values
# are left alone for FPM to name. cbox-init's boot autotune overwrites all four
# with values it keeps in the same order, so this covers manual sizing.
clamp_fpm_spare_servers() {
local max="${PHP_FPM_MAX_CHILDREN:-}" start="$PHP_FPM_START_SERVERS"
local min="$PHP_FPM_MIN_SPARE" spare="$PHP_FPM_MAX_SPARE" v
for v in "$max" "$start" "$min" "$spare"; do
case "$v" in ''|*[!0-9]*) return 0 ;; esac
done
[ "$max" -ge 1 ] || return 0

[ "$min" -lt 1 ] && min=1
[ "$min" -gt "$max" ] && min=$max
[ "$spare" -lt "$min" ] && spare=$min
[ "$spare" -gt "$max" ] && spare=$max
[ "$start" -lt "$min" ] && start=$min
[ "$start" -gt "$spare" ] && start=$spare

if [ "$start/$min/$spare" != "$PHP_FPM_START_SERVERS/$PHP_FPM_MIN_SPARE/$PHP_FPM_MAX_SPARE" ]; then
log_warn "PHP-FPM pool: start/min_spare/max_spare $PHP_FPM_START_SERVERS/$PHP_FPM_MIN_SPARE/$PHP_FPM_MAX_SPARE do not fit pm.max_children=$max - using $start/$min/$spare"
fi
export PHP_FPM_START_SERVERS="$start" PHP_FPM_MIN_SPARE="$min" PHP_FPM_MAX_SPARE="$spare"
}

write_pm_mode_dropin() {
local mode="${PHP_FPM_PM:-dynamic}"
local dropin="/usr/local/etc/php-fpm.d/zz-pm-mode.conf"
case "$mode" in
dynamic)
# The image BAKES a dynamic drop-in with env placeholders, so an
# unwritable config dir (read-only rootfs) is fine for the default
# mode: FPM expands the exported PHP_FPM_* itself.
export PHP_FPM_START_SERVERS="${PHP_FPM_START_SERVERS:-2}"
export PHP_FPM_MIN_SPARE="${PHP_FPM_MIN_SPARE:-1}"
export PHP_FPM_MAX_SPARE="${PHP_FPM_MAX_SPARE:-6}"
export PHP_FPM_MAX_SPAWN_RATE="${PHP_FPM_MAX_SPAWN_RATE:-32}"
clamp_fpm_spare_servers
# PLACEHOLDERS, never the values: FPM expands ${PHP_FPM_*} from its
# own environment at start. cbox-init's boot autotune runs AFTER this
# entrypoint and exports the tuned max_children AND spare counts;
# pool.conf expands max_children from that env, so literals here
# froze the spare counts at the pre-tune defaults (max_spare 6) and
# a 512Mi container tuned to 4 children crash-looped with "cannot be
# greater than pm.max_children". Same content as the BAKED drop-in
# (pm-mode-dynamic.conf), so a read-only rootfs behaves the same.
# shellcheck disable=SC2016
printf '%s\n' \
"; Auto-generated: dynamic warm floor (PHP_FPM_PM=dynamic)" \
"[www]" \
"pm.start_servers = ${PHP_FPM_START_SERVERS:-2}" \
"pm.min_spare_servers = ${PHP_FPM_MIN_SPARE:-1}" \
"pm.max_spare_servers = ${PHP_FPM_MAX_SPARE:-6}" \
"pm.max_spawn_rate = ${PHP_FPM_MAX_SPAWN_RATE:-32}" \
'pm.start_servers = ${PHP_FPM_START_SERVERS}' \
'pm.min_spare_servers = ${PHP_FPM_MIN_SPARE}' \
'pm.max_spare_servers = ${PHP_FPM_MAX_SPARE}' \
'pm.max_spawn_rate = ${PHP_FPM_MAX_SPAWN_RATE}' \
> "$dropin" 2>/dev/null || log_info "Config dir not writable - the baked dynamic drop-in stands (env-expanded by FPM)"
;;
ondemand)
Expand Down
6 changes: 6 additions & 0 deletions docs/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ or disable with `PHP_FPM_AUTOTUNE_PROFILE=""` and set the pool values explicitly
| `PHP_FPM_MAX_REQUESTS` | `500` | Requests per child before recycling (0 = unlimited) |
| `PHP_FPM_REQUEST_TERMINATE_TIMEOUT` | `60s` | Max request execution time before kill |

In `dynamic` mode the spare counts are clamped to fit `pm.max_children`
(`1 ≤ min_spare ≤ start_servers ≤ max_spare ≤ max_children`), with a `WARN`
naming the values used, so `PHP_FPM_MAX_CHILDREN=4` on its own boots
`2/1/4` instead of FPM refusing `max_spare 6 > max_children 4`. With autotune
on, cbox-init sets all four itself and keeps the same order.

### Runtime PHP-FPM tuning (fpm-tune, cbox-init 3.1+)

The boot profile above is a **seed** — it sizes the pool once, before traffic.
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/run-all-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ QUICK_TESTS=(
"test-health-checks.sh"
"test-env-config.sh"
"test-app-processes.sh"
"test-memory-limits.sh"
"test-nginx-template.sh"
)

Expand Down
Loading
Loading