PHP 7.4 backport guide: security-patching an end-of-life PHP
Find a source backport for PHP 7.4, apply it to the 7.4 tree, rebuild the official Docker image with the same settings, and prove it in CI. Step by step.
PHP 7.4 reached end of life in November 2022. When a new vulnerability lands, there will be no 7.4.34. Yet plenty of applications still run on 7.4 and cannot be migrated quickly. This guide is the full method I use to protect such an application without touching its code: find the patch, apply it to 7.4, rebuild the image, and prove the patch is really inside.
For the shorter, incident-focused version: Closing a PHP vulnerability without code changes.
This is a bridge. The real fix is moving to a supported PHP version. A backport is how you close the hole until the migration is done.
1. Backport or migrate?
| Situation | Path |
|---|---|
| Critical hole, app can move to 8.x within weeks | Interim mitigation + fast migration |
| Critical hole, migration takes months | Backport image + migration plan |
| Low-impact hole in an unused extension | Disable the extension, no backport needed |
Measure first: which container runs which PHP version and SAPI? If the hole only affects a specific SAPI or extension, the list often gets shorter.
kubectl get pods -A --no-headers -o custom-columns=NS:.metadata.namespace,POD:.metadata.name |
while read -r ns pod; do
v=$(kubectl exec -n "$ns" "$pod" -- php -r 'echo PHP_VERSION," ",PHP_SAPI;' 2>/dev/null) && echo "$ns/$pod $v"
done | sort -k2
2. Finding the patch
Places to look, in order:
- The PHP security announcement. For severe holes the PHP security team may publish "minimal source backports" for end-of-life branches. The announcement usually names a separate branch or commit in php-src.
- The fix commit in php-src. Find the fix on a supported branch (8.2, 8.3…). On GitHub, appending
.patchto a commit URL returns it as plain text. - Distribution patches. Some Linux distributions and extended-support vendors prepare 7.4 backports for their own packages. Folders such as
debian/patches/in their source packages are a good reference.
Take only the fix commit. The version-bump and CI commits on the same branch are not needed.
mkdir -p patches/7.4
curl -fsSL https://github.com/php/php-src/commit/<commit>.patch -o patches/7.4/0001-fix.patch
cd patches/7.4 && sha256sum *.patch > SHA256SUMS
3. Does the patch fit the 7.4 tree?
Do a dry run against the source inside the official image:
docker run --rm -v "$PWD/patches/7.4:/p:ro" php:7.4.33-fpm-bullseye sh -c '
docker-php-source extract && cd /usr/src/php &&
for p in /p/*.patch; do patch -p1 --dry-run --fuzz=0 < "$p" || exit 1; done && echo CLEAN'
- If it prints CLEAN, carry on.
- If it does not fit, the patch has to be adapted by hand. That is now a code change: function names and structures may differ in 7.4. A second person should review the adapted patch, and the relevant
php-srctests should be run (see step 6). Raising--fuzzand calling it "close enough" is dangerous: the patch may land on the wrong lines.
4. Dockerfile: same image, patched PHP
The idea: take the official php:7.4.33-fpm image, apply the patch to the source it ships, rebuild PHP with the original configure options, and install it in the same place. Directory layout, the docker-php-ext-* scripts, conf.d and php-fpm.d stay identical to the official image. On the application side only the FROM line changes.
# syntax=docker/dockerfile:1
ARG BASE_IMAGE=php:7.4.33-fpm-bullseye
FROM ${BASE_IMAGE}
ARG BASE_IMAGE
ARG PATCH_DIR=patches/7.4
ARG BACKPORT_ID=backport-1
COPY ${PATCH_DIR}/ /tmp/patches/
RUN set -eux; \
# Stop if there is no patch: never publish an unpatched image by accident
ls /tmp/patches/*.patch >/dev/null; \
( cd /tmp/patches && sha256sum -c SHA256SUMS ); \
php -m | sort > /tmp/modules.before; \
CONFIGURE_OPTS="$(php-config --configure-options)"; \
\
# Debian 11 repos moved to the archive; use archive.debian.org for apt
if grep -q 'VERSION_CODENAME=bullseye' /etc/os-release; then \
printf '%s\n' \
'deb http://archive.debian.org/debian bullseye main' \
'deb http://archive.debian.org/debian-security bullseye-security main' > /etc/apt/sources.list; \
rm -f /etc/apt/sources.list.d/*; \
echo 'Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/99archive; \
fi; \
\
savedAptMark="$(apt-mark showmanual)"; \
apt-get update; \
apt-get install -y --no-install-recommends $PHPIZE_DEPS bison patch xz-utils \
libargon2-dev libcurl4-openssl-dev libonig-dev libreadline-dev libsodium-dev \
libsqlite3-dev libssl-dev libxml2-dev zlib1g-dev; \
\
docker-php-source extract; cd /usr/src/php; \
for p in /tmp/patches/*.patch; do patch -p1 --forward --fuzz=0 < "$p"; done; \
eval "./configure $CONFIGURE_OPTS"; \
make -j"$(nproc)"; make install; make clean; \
\
# Extensions built later with docker-php-ext-install must use the patched source too
cd /usr/src && tar -cJf php.tar.xz php && rm -f php.tar.xz.asc && docker-php-source delete; \
\
# Remove build dependencies, keep runtime libraries
apt-mark auto '.*' >/dev/null; [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; \
find /usr/local -type f -executable -exec ldd '{}' ';' \
| awk '/=>/ { so=$(NF-1); if (index(so,"/usr/local/")==1) next; gsub("^/(usr/)?","",so); printf "*%s\n", so }' \
| sort -u | xargs -r dpkg-query --search | cut -d: -f1 | sort -u | xargs -r apt-mark manual; \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
rm -rf /var/lib/apt/lists/*; \
\
# Proof: the module list must not change, the FPM config must be valid
php -m | sort | diff -u /tmp/modules.before -; \
php-fpm -t; \
{ echo "id=${BACKPORT_ID}"; echo "base=${BASE_IMAGE}"; ( cd /tmp/patches && sha256sum *.patch ); } \
> /usr/local/etc/php/BACKPORT; \
rm -rf /tmp/patches /tmp/modules.before
ENV PHP_BACKPORT=${BACKPORT_ID}
LABEL org.opencontainers.image.description="php ${BASE_IMAGE} + source backport ${BACKPORT_ID}"
Why these details matter:
--fuzz=0and--forward: if the patch does not apply exactly, the build fails, and the same patch is never applied twice.- Original configure options:
php-config --configure-optionsreturns the official image's build settings. Building with different options can silently change extensions. - Module list comparison: a missing extension breaks the app in production.
difffails the build on any difference. php.tar.xzis repacked: if application images run steps likedocker-php-ext-install gd, those use the patched source too.- Marker:
PHP_VERSIONstill says7.4.33. The answer to "is this image patched?" lives in thePHP_BACKPORTvariable and the patch hashes in theBACKPORTfile.
Debian 11 note: the official 7.4 image is based on bullseye, and bullseye's LTS support ended in August 2026. Bullseye repos on
deb.debian.orgreturn 404; packages can still be installed fromarchive.debian.org, but the operating-system packages no longer receive security updates. Patching PHP does not fix that. One more reason the migration is urgent.
5. CI: no image without proof
stages: [verify, build]
variables:
BACKPORT_ID: "backport-1"
BASE_74: "php:7.4.33-fpm-bullseye" # pin with @sha256:... if you can
verify_patches:
stage: verify
image: alpine:3.20
script:
- ls patches/7.4/*.patch
- cd patches/7.4 && sha256sum -c SHA256SUMS
build_php74:
stage: build
image: quay.io/buildah/stable
script:
- buildah login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
- buildah bud --pull=always --layers=false
--build-arg BASE_IMAGE="$BASE_74" --build-arg PATCH_DIR=patches/7.4
--build-arg BACKPORT_ID="$BACKPORT_ID" -t "$CI_REGISTRY_IMAGE:7.4-fpm-candidate" .
# Smoke test
- ctr=$(buildah from "$CI_REGISTRY_IMAGE:7.4-fpm-candidate")
- buildah run "$ctr" -- php -v
- buildah run "$ctr" -- php-fpm -t
- buildah run "$ctr" -- sh -c 'test "$PHP_BACKPORT" = "'"$BACKPORT_ID"'" && grep -q "\.patch" /usr/local/etc/php/BACKPORT'
- buildah rm "$ctr"
# Push only on the default branch and on scheduled runs
- |
if [ "$CI_COMMIT_BRANCH" = "$CI_DEFAULT_BRANCH" ] || [ "$CI_PIPELINE_SOURCE" = "schedule" ]; then
for t in 7.4-fpm "7.4.33-fpm-$BACKPORT_ID"; do
buildah tag "$CI_REGISTRY_IMAGE:7.4-fpm-candidate" "$CI_REGISTRY_IMAGE:$t"
buildah push "$CI_REGISTRY_IMAGE:$t"
done
fi
- Two tags: the moving
7.4-fpmtag is what applications use; the fixed7.4.33-fpm-<id>tag is for rollback. - Weekly scheduled run: this also picks up the base image's other updates regularly. A base image that is only rebuilt when its Dockerfile changes never gets updated.
6. Testing the patch
A passing build does not prove the hole is closed. Depending on what you have:
- php-src tests: the fix commit often includes a
.phpttest. Run it in the build directory:
cd /usr/src/php && make test TESTS="Zend/tests/<relevant-test>.phpt" NO_INTERACTION=1
- Unpatched vs. patched: the same test should fail on the official image and pass on yours. If it passes only on the patched one, the patch is really inside.
- Application regression: in the test environment first, with the app's own tests and a few real flows.
7. Rolling it out to applications
-FROM php:7.4-fpm
+FROM registry.example.com/base-images/php-backport:7.4-fpm
Order: test environment → passive production → active production, with a separate approval at each step. Verify in every environment:
kubectl -n <ns> exec deploy/<app> -- sh -c 'echo "$PHP_BACKPORT"; cat /usr/local/etc/php/BACKPORT; php -v'
Check that application pipelines are re-triggered when the base image changes. Rules such as only: changes can leave applications on the old image even after the base image is updated.
8. What not to do
- Do not pull a ready-made patched image from the internet. Running a PHP binary when you do not know who built it, with which patch, can be a bigger risk than the hole itself. Take the recipe and build the image yourself.
- Do not force it with
--fuzz. A patch that does not fit is adapted by hand and reviewed. - Do not treat the backport as permanent. The same work repeats with every new hole, and the OS layer keeps ageing.
FAQ
Does PHP 7.4 still get security updates? Not officially. Support ended in November 2022. Some distributions and paid extended-support vendors publish patches for their own packages. For very severe holes the PHP security team may, as an exception, share a source backport.
If PHP_VERSION doesn't change, how will scanners see the patch?
They won't. Security scanners often look only at the version number and will still flag the image as vulnerable. That is why the BACKPORT file and labels matter: you close the scanner finding with evidence.
Does the same method work for 8.0 or 8.1?
Yes. BASE_IMAGE and PATCH_DIR change; the Dockerfile stays the same.
Checklist
- [ ] Were the affected containers identified by measurement?
- [ ] Does the patch come from an official source, with its URL and hash recorded?
- [ ] Does the patch apply cleanly with
--fuzz=0? - [ ] Is the module list the same before and after patching?
- [ ] Does the relevant test fail on the unpatched image and pass on the patched one?
- [ ] Does the image carry
PHP_BACKPORTand the patch hashes? - [ ] Were the application pipelines rebuilt on the new base image?
- [ ] Is there a migration plan with a date?