Security Advisory
phpAfterShell - Published Nov. 20th - Revised Nov. 20th -
Web applications - PHP - Sessions and runtime

phpAfterShell: three critical-rated PHP flaws, one update

Advisory severity
CRITICAL
CVSS 4.0 (each)
9.2
Public PoC
CVE-2026-126714
Fix
8.2.36 / 8.3.37 / 8.4.28 / 8.5.13
CVEs CVE-2026-126714 (phpAfterShell), CVE-2026-129496, CVE-2026-129481
Fixed in PHP 8.2.36, 8.3.37, 8.4.28, 8.5.13, 8.6.0, or distribution packages from Nov. 19th onward that name the CVEs
Level 1 - For the CEO

A PHP flaw reachable on default settings, with public proof-of-concept code, is fixed in the Nov. 19th release: one PHP update closes it and two related flaws, so every PHP server updates and restarts this week.

Level 2 - In plain English

PHP keeps track of each visitor in a small session file on disk. The main flaw lets an outsider interrupt PHP while it rewrites that file, leaving half a file that PHP later reads back as instructions. It works on default settings through PHP's upload progress feature, and October's PHP2Shell patch does not cover it. The other two flaws are memory bugs that mostly crash PHP workers.

One update fixes all three. Where the update has to wait, one php.ini line closes the easiest route in.

Level 3 - For the team doing the work

Vulnerability breakdown

CVERoot causePractical risk
CVE-2026-126714
phpAfterShell
Race condition (TOCTOU) in file-based sessions. PHP rewrites existing session files non-atomically, so an interrupted write leaves attacker-controlled serialized objects in the file.Highest No authentication needed where session.upload_progress.enabled is on (the default before this release): a crafted multipart upload triggers the write before application code runs. Code execution needs an object gadget chain, which large frameworks and plugin ecosystems often contain. Public PoC for the core primitive. Not fixed by October's PHP2Shell patches.
CVE-2026-129496Use-after-free in the timezone cache. In non-thread-safe builds, a startup diagnostic written to a file-based error_log leaves a pointer to freed memory that an early request diagnostic then uses.High Unauthenticated requests reliably crash PHP-FPM, mod_php and CGI workers on affected configurations. Code execution needs known or leaked memory addresses.
CVE-2026-129481Use-after-free after an interrupted session shutdown. A fatal error during session teardown leaves a dangling pointer.Moderate to high Listed for PHP 8.3 and later. Crashes are the likely outcome; code execution has not been demonstrated. Prevented where zend.exception_ignore_args is On, as in php.ini-production. An incomplete fix for October's CVE-2026-109619.

The release also fixes four more critical-rated flaws, including PHP-FPM request smuggling (CVE-2026-129289). The update covers them; the stopgaps do not.

Distribution status as of Nov. 19th

SourceStatusFixed build
php.net, official Docker imagesFixed8.2.36, 8.3.37, 8.4.28, 8.5.13, 8.6.0
Debian 12 / 13Fixed8.2.36-1~deb12u1 / 8.4.28-1~deb13u1
Amazon Linux 2023FixedALAS2023-2026-3504, -3505 and php8.4 8.4.28
Amazon Linux 2Being releasedAWS bulletin 2026-190-AWS
Ubuntu (all releases)Needs triageNone released
RHEL, Rocky, AlmaNo fix yetNone released
PHP 8.1 and olderEnd of lifeVendor backports or commercial support only

Distributions backport without changing php -v. The package changelog naming the CVEs is the evidence.

Priority and remediation

0 of 6 steps taken
1Check version, session handler and upload progress

Read-only. CVE-2026-126714 targets session.save_handler = files. Redis, Memcached or database sessions are largely insulated from it but still exposed to the two use-after-free bugs. CLI and PHP-FPM can load different ini files, so check FPM where present.

Terminal
FPM=$(ls /usr/sbin/php-fpm* 2>/dev/null | head -1); ${FPM:-php} -v
${FPM:-php} -i 2>/dev/null | grep -E '^(session.save_handler|session.upload_progress.enabled|zend.exception_ignore_args|error_log) '
grep -rn 'upload_progress' /etc/php* /etc/php-fpm.d 2>/dev/null
2Update PHP and restart it (fixes all three)

Restart PHP-FPM, and Apache where PHP runs as mod_php. Rebuild and redeploy container images with a fresh base pull. The new releases switch upload progress off by default, but an explicit setting in php.ini or a pool file still wins; applications with upload progress bars need it set back to 1 after updating.

Debian / Ubuntu
sudo apt-get update && sudo apt-get install --only-upgrade 'php*'
sudo systemctl restart 'php*-fpm' 2>/dev/null; sudo systemctl restart apache2 2>/dev/null
RHEL family / Amazon Linux
sudo dnf upgrade 'php*' 2>/dev/null || sudo yum update 'php*'
sudo systemctl restart php-fpm 2>/dev/null; sudo systemctl restart httpd 2>/dev/null
Containers
docker build --pull -t IMAGE:TAG .
3If patching is delayed: turn upload progress off

Set it in php.ini or the FPM pool, then restart workers; ini_set() in application code runs too late. This blocks the unauthenticated upload route to CVE-2026-126714 only. Applications that write untrusted input into $_SESSION stay exposed until patched. Leave FastCGI connection reuse (nginx fastcgi_keep_conn, Apache enablereuse) at its default of off.

php.ini or FPM pool
session.upload_progress.enabled = 0
; FPM pool form: php_admin_flag[session.upload_progress.enabled] = off
4If patching is delayed: stopgaps for the two use-after-free bugs

exception_ignore_args prevents CVE-2026-129481. A valid date.timezone and no other startup warnings remove the trigger for CVE-2026-129496; the last command should print nothing. Restart PHP afterwards.

php.ini
zend.exception_ignore_args = On
date.timezone = America/Toronto
Terminal
php -d display_startup_errors=1 -d error_log= -r '' 2>&1 | head -5
5Confirm, and keep the evidence

Record the changelog entries and PHP restart time per server. Confirm October's PHP2Shell fixes (CVE-2026-109618, CVE-2026-109619) are present too.

Terminal
for f in /usr/share/doc/php*-common/changelog.Debian.gz; do zgrep -oE 'CVE-2026-(109618|109619|126714|129496|129481)' "$f" 2>/dev/null | sort -u; done
rpm -q --changelog $(rpm -qa 'php*common*' 2>/dev/null) 2>/dev/null | grep -oE 'CVE-2026-(109618|109619|126714|129496|129481)' | sort -u
systemctl show -p ActiveEnterTimestamp 'php*-fpm' php-fpm 2>/dev/null

In summary

October's patch closed one route into PHP sessions and November's closes another, so the servers still missing October's fix are where this month's work starts.

One-shot for Ubuntu servers

6Ubuntu with Ondrej Sury's PHP, PHP-FPM behind Apache: check, patch, verify in one paste

Paste the whole block as a user with sudo. It writes a checker to ~/phpfix.sh, moves the apt source from the Launchpad PPA (which Ondrej Sury is retiring) to packages.sury.org after confirming that repository serves this Ubuntu release, and upgrades PHP only if a newer build exists and a dry run shows nothing outside PHP would change. It then restarts PHP-FPM and Apache and runs the checker read-only, which prints each setting and a per-CVE verdict. The upload progress stopgap is not applied automatically: sudo ~/phpfix.sh --apply writes it as a drop-in, checks for applications that use upload progress first, verifies, reloads PHP-FPM and smoke-tests every Apache virtual host; --rollback removes it. Not suited to servers running two or more PHP-FPM versions, Ubuntu 20.04 (no packages.sury.org builds), mod_php, or nginx front ends.

Bash, Ubuntu 22.04 / 24.04 / 26.04
# ===== php-2026-11: write phpfix.sh, move to packages.sury.org, patch, verify =====
# Paste the whole block. Stops on its own if the dry run touches anything beyond PHP.

cat > ~/phpfix.sh <<'PHPFIX_EOF'
#!/usr/bin/env bash
# php-2026-11: CVE-2026-126714, CVE-2026-129481, CVE-2026-129496
# Target: Ubuntu, ondrej/php PPA, PHP 8.x under PHP-FPM (version auto-detected when only one FPM is installed)
#
#   sudo ~/phpfix.sh              read-only check (default, changes nothing)
#   sudo ~/phpfix.sh --apply      gates, drop-in, verify, graceful reload, smoke test
#   sudo ~/phpfix.sh --rollback   remove the drop-in and reload
#
# Env overrides: PHPV=<auto>  SMOKE_HOST=<one vhost, default all>  SMOKE_MAX=60  FORCE=0  WEBROOT=/var/www

set -uo pipefail

MODE="${1:---check}"
if [[ -z "${PHPV:-}" ]]; then
  mapfile -t _FPMS < <(ls /usr/sbin/php-fpm[0-9]* 2>/dev/null)
  if (( ${#_FPMS[@]} == 1 )); then PHPV="${_FPMS[0]#/usr/sbin/php-fpm}"; else PHPV=8.2; fi
fi
SMOKE_HOST="${SMOKE_HOST:-}"        # empty: test every vhost from apache2ctl -S
SMOKE_MAX="${SMOKE_MAX:-60}"
FORCE="${FORCE:-0}"              # 1: apply even if a gate hits (you have reviewed the hits)
WEBROOT="${WEBROOT:-/var/www}"
FPM="/usr/sbin/php-fpm${PHPV}"
SVC="php${PHPV}-fpm"
PKG="php${PHPV}-fpm"
FPMLOG="/var/log/php${PHPV}-fpm.log"
DROPIN="/etc/php/${PHPV}/fpm/conf.d/99-oncyber-php-2026-11.ini"
CHANGELOG="/usr/share/doc/${PKG}/changelog.Debian.gz"
CVES='CVE-2026-(126714|129481|129496)'

if [[ -t 1 ]]; then
  R=$'\e[31m'; G=$'\e[32m'; Y=$'\e[33m'; B=$'\e[34m'; C=$'\e[36m'; D=$'\e[2m'; BOLD=$'\e[1m'; N=$'\e[0m'
else
  R=''; G=''; Y=''; B=''; C=''; D=''; BOLD=''; N=''
fi

declare -a ROWS=()
GATE_FAIL=0; GATE_HITS=''; CREATED=0; PATCHED=0; REPORTED=''

row()    { ROWS+=("$1|$2|$3"); }
say()    { printf '%s\n' "${C}==>${N} ${BOLD}$*${N}"; }
colour() { case "$1" in PASS) printf '%s' "$G";; WARN) printf '%s' "$Y";; FAIL) printf '%s' "$R";; *) printf '%s' "$B";; esac; }
ini()    { "$FPM" -i 2>/dev/null | awk -F' => ' -v k="$1" '$1==k {print $2; exit}'; }

die() {
  printf '%s\n' "${R}ABORT:${N} $*"
  [[ -n "${VER:-}" && -z "$REPORTED" ]] && report_state
  summary
  exit 1
}

collect() {
  VER=$(dpkg-query -W -f='${Version}' "$PKG" 2>/dev/null || echo "not installed")
  CAND=$(apt-cache policy "$PKG" 2>/dev/null | awk '/Candidate:/ {print $2}')
  case "$PHPV" in 8.2) FIXED=8.2.36;; 8.3) FIXED=8.3.37;; 8.4) FIXED=8.4.28;; 8.5) FIXED=8.5.13;; 8.6) FIXED=8.6.0;; *) FIXED='';; esac
  UPSTREAM=${VER#*:}; UPSTREAM=${UPSTREAM%%-*}
  PATCHED=0; PATCH_HOW=''
  if zcat "$CHANGELOG" 2>/dev/null | head -80 | grep -qE "$CVES"; then
    PATCHED=1; PATCH_HOW="changelog names the CVEs"
  elif [[ -n "$FIXED" ]] && dpkg --compare-versions "$UPSTREAM" ge "$FIXED" 2>/dev/null; then
    PATCHED=1; PATCH_HOW="upstream $UPSTREAM >= fixed $FIXED"
  fi
  HANDLER=$(ini session.save_handler)
  UPLOAD=$(ini session.upload_progress.enabled)
  IGNARGS=$(ini zend.exception_ignore_args)
  TZ_VAL=$(ini date.timezone)
  WARNINGS=$("$FPM" -i 2>&1 >/dev/null | head -5)
  TZ_OK=0
  if [[ -n "$TZ_VAL" && "$TZ_VAL" != "no value" ]] &&
     "php${PHPV}" -r 'exit(in_array($argv[1], timezone_identifiers_list(), true) ? 0 : 1);' "$TZ_VAL" 2>/dev/null; then
    TZ_OK=1
  fi
}

report_state() {
  REPORTED=1
  row INFO "Package $PKG" "$VER"
  if [[ -n "$CAND" && "$CAND" != "$VER" ]]; then
    row WARN "apt candidate" "$CAND available, not installed"
  else
    row INFO "apt candidate" "same as installed (run apt update to refresh)"
  fi
  if (( PATCHED )); then row PASS "Patch status" "$PATCH_HOW"
  else row FAIL "Patch status" "unpatched, needs ${FIXED:-a fixed release} or a backport naming the CVEs"; fi

  if [[ "$HANDLER" == files ]]; then row WARN "session.save_handler" "files (CVE-2026-126714 applies)"
  else row PASS "session.save_handler" "${HANDLER:-unknown}"; fi

  if [[ "$UPLOAD" == Off ]]; then row PASS "upload_progress.enabled" "Off"
  else row FAIL "upload_progress.enabled" "${UPLOAD:-unknown}"; fi

  if [[ "$IGNARGS" == On ]]; then row PASS "exception_ignore_args" "On"
  else row FAIL "exception_ignore_args" "${IGNARGS:-unknown}"; fi

  if (( TZ_OK )); then row PASS "date.timezone" "$TZ_VAL"
  else row FAIL "date.timezone" "${TZ_VAL:-empty} (invalid or unset)"; fi

  if [[ -z "$WARNINGS" ]]; then row PASS "FPM startup warnings" "none"
  else row FAIL "FPM startup warnings" "$(head -1 <<<"$WARNINGS")"; fi

  if apache2ctl -M 2>/dev/null | grep -qE 'php[0-9]*_module'; then
    row FAIL "Apache mod_php" "loaded, the FPM stopgap does not cover it"
  else
    row PASS "Apache mod_php" "not loaded, FPM only"
    if dpkg-query -W -f='${Status} ${Package}\n' 'libapache2-mod-php*' 2>/dev/null | grep -q '^install ok installed'; then
      row INFO "mod_php packages" "installed but dormant, remove after this advisory"
    fi
  fi

  if [[ -d /usr/share/phpmyadmin ]]; then
    if grep -RqiE 'phpmyadmin' /etc/apache2/conf-enabled/ /etc/apache2/sites-enabled/ 2>/dev/null; then
      row WARN "phpMyAdmin" "installed and wired into Apache, check exposure"
    else
      row INFO "phpMyAdmin" "installed, not wired into Apache"
    fi
  fi

  [[ -f "$DROPIN" ]] && row INFO "Drop-in" "present: $DROPIN"
}

gate() {   # gate <label> <hits>
  if [[ -n "$2" ]]; then
    row FAIL "Gate: $1" "hit, review before applying"
    GATE_FAIL=1; GATE_HITS+="[$1]"$'\n'"$2"$'\n'
  else
    row PASS "Gate: $1" "clear"
  fi
}

gates() {
  gate "app code" "$(grep -rnoE '.{0,40}(PHP_SESSION_UPLOAD_PROGRESS|session\.upload_progress|\bupload_progress_).{0,40}' "$WEBROOT" \
        --include='*.php' --include='*.js' --include='*.html' \
        --exclude-dir=vendor --exclude-dir=node_modules --exclude-dir=.git 2>/dev/null | head -5)"
  gate "Apache config" "$(grep -RniE 'upload_progress' /etc/apache2/ 2>/dev/null | head -5)"
  gate "FPM pool" "$(grep -RnE '^[^;]*upload_progress' "/etc/php/${PHPV}/fpm/pool.d/" 2>/dev/null | head -5)"
  gate ".user.ini" "$(find "$WEBROOT" -maxdepth 4 -name '.user.ini' -exec grep -HnE 'upload_progress' {} + 2>/dev/null | head -5)"

  local pv
  pv=$(grep -RniE 'PHP_VALUE|PHP_ADMIN_VALUE' /etc/apache2/ 2>/dev/null | head -3)
  if [[ -n "$pv" ]]; then row WARN "Apache PHP_VALUE" "present, review manually"
  else row PASS "Apache PHP_VALUE" "none"; fi
}

discover_vhosts() {   # prints "port name" for every named vhost Apache has loaded
  apache2ctl -S 2>/dev/null | awk '
    / namevhost / { for (i = 1; i <= NF; i++) { if ($i == "port") p = $(i+1); if ($i == "namevhost") n = $(i+1) } print p, n; next }
    /^[^ ]+:[0-9]+ +[^ ]+ +\(/ && $2 != "is" { p = $1; sub(/.*:/, "", p); print p, $2 }
  ' | grep -v '\*' | sort -u
}

smoke_one() {   # smoke_one <port> <name>  ->  "code seconds"
  local port="$1" name="$2" out
  local -a args
  if [[ "$port" == 443 ]]; then
    args=(-k --resolve "${name}:443:127.0.0.1" "https://${name}/")
  else
    args=(-H "Host: ${name}" "http://127.0.0.1:${port}/")
  fi
  out=$(curl -s -o /dev/null -m 10 -w '%{http_code} %{time_total}' "${args[@]}" 2>/dev/null) || true
  printf '%s\n' "${out:-000 0}"
}

smoke() {
  local -a targets=() detail=()
  if [[ -n "$SMOKE_HOST" ]]; then
    targets=("80 $SMOKE_HOST")
  else
    mapfile -t targets < <(discover_vhosts)
  fi
  (( ${#targets[@]} )) || targets=("80 $(hostname)")

  local pass=0 warn=0 fail=0 slow='' slowt=0 entry port name code t
  for entry in "${targets[@]:0:$SMOKE_MAX}"; do
    read -r port name <<<"$entry"
    read -r code t < <(smoke_one "$port" "$name")
    if [[ "$code" =~ ^[23][0-9][0-9]$ ]]; then
      pass=$((pass + 1))
    elif [[ "$code" =~ ^4[0-9][0-9]$ ]]; then
      warn=$((warn + 1)); detail+=("WARN|  $name:$port|HTTP $code in ${t}s")
    else
      fail=$((fail + 1)); detail+=("FAIL|  $name:$port|HTTP $code in ${t}s")
    fi
    if awk -v a="$t" -v b="$slowt" 'BEGIN { exit !(a > b) }'; then slowt="$t"; slow="$name:$port"; fi
  done

  local n=$((pass + warn + fail)) status=PASS
  (( warn )) && status=WARN
  (( fail )) && status=FAIL
  row "$status" "Smoke vhosts" "$n tested: $pass ok, $warn 4xx, $fail failed; slowest $slow ${slowt}s"
  (( ${#targets[@]} > SMOKE_MAX )) && row WARN "Smoke vhosts" "capped at $SMOKE_MAX of ${#targets[@]}, raise SMOKE_MAX"
  local d
  for d in "${detail[@]}"; do ROWS+=("$d"); done
}

apply() {
  if (( GATE_FAIL )); then
    [[ "$FORCE" == 1 ]] || die "a gate failed. Nothing changed. Hits are listed below the summary. Re-run with FORCE=1 once reviewed."
    row WARN "Gates" "overridden with FORCE=1, hits listed below"
  fi
  (( PATCHED )) && row INFO "Stopgap" "package already patched, drop-in is belt and braces"

  local content
  content="; ONCyber php-2026-11 stopgap for CVE-2026-126714."$'\n'"; Remove once ${PKG} is patched (changelog names the CVE) and verified."$'\n'"session.upload_progress.enabled = 0"

  if [[ -f "$DROPIN" && "$(cat "$DROPIN")" == "$content" ]]; then
    row INFO "Drop-in" "already present, unchanged"
  else
    printf '%s\n' "$content" > "$DROPIN" || die "could not write $DROPIN"
    CREATED=1
    row PASS "Drop-in" "written"
  fi

  local up warn
  up=$(ini session.upload_progress.enabled)
  warn=$("$FPM" -i 2>&1 >/dev/null | head -5)
  if [[ "$up" != Off || -n "$warn" ]] || ! "$FPM" -t >/dev/null 2>&1; then
    (( CREATED )) && rm -f "$DROPIN"
    die "pre-reload verify failed (upload_progress=${up:-unknown}${warn:+, warning: $warn}). Drop-in removed, service not reloaded."
  fi
  row PASS "Pre-reload verify" "parses Off, no warnings, config test OK"

  local before
  before=$(wc -l < "$FPMLOG" 2>/dev/null || echo 0)
  systemctl reload "$SVC" || die "reload failed. Roll back with: sudo $0 --rollback"
  sleep 2
  systemctl is-active --quiet "$SVC" || die "$SVC not active after reload. Roll back with: sudo $0 --rollback"
  row PASS "Reload $SVC" "graceful, active"

  local newerr
  newerr=$(tail -n +"$((before + 1))" "$FPMLOG" 2>/dev/null | grep -E 'ERROR|ALERT|WARNING' | grep -v 'finished' | head -1)
  if [[ -n "$newerr" ]]; then row WARN "FPM log" "$newerr"
  else row PASS "FPM log" "clean since reload"; fi

  smoke
}

rollback() {
  if [[ -f "$DROPIN" ]]; then rm -f "$DROPIN" && row PASS "Drop-in" "removed"
  else row INFO "Drop-in" "not present"; fi
  if systemctl reload "$SVC" && sleep 2 && systemctl is-active --quiet "$SVC"; then
    row PASS "Reload $SVC" "graceful, active"
  else
    row FAIL "Reload $SVC" "check: systemctl status $SVC"
  fi
}

verdict() { printf '  %s%-10s%s  %-17s %s\n' "$(colour "$1")" "$2" "$N" "$3" "$4"; }

verdicts() {
  [[ -n "${VER:-}" ]] || return 0
  printf '\n%s\n' "${BOLD}Per CVE${N}"
  if (( PATCHED )); then
    local c
    for c in CVE-2026-126714 CVE-2026-129481 CVE-2026-129496; do verdict PASS PATCHED "$c" "fixed in installed package"; done
    return 0
  fi
  if [[ "$HANDLER" != files ]]; then verdict WARN "LOWER RISK" CVE-2026-126714 "handler ${HANDLER:-unknown}, patch anyway"
  elif [[ "$UPLOAD" == Off ]];  then verdict WARN MITIGATED CVE-2026-126714 "stopgap only, patch when the fixed package lands"
  else verdict FAIL EXPOSED CVE-2026-126714 "files handler, upload progress on, public PoC"; fi

  if [[ "$IGNARGS" == On ]]; then verdict WARN MITIGATED CVE-2026-129481 "zend.exception_ignore_args = On"
  else verdict FAIL EXPOSED CVE-2026-129481 "zend.exception_ignore_args ${IGNARGS:-unknown}"; fi

  if (( TZ_OK )) && [[ -z "$WARNINGS" ]]; then verdict WARN MITIGATED CVE-2026-129496 "valid timezone, no startup warnings"
  else verdict FAIL EXPOSED CVE-2026-129496 "fix timezone or startup warnings"; fi
}

summary() {
  local line r s c d
  line=$(printf '%.0s-' {1..76})
  printf '\n%s\n%s\n' "${BOLD}php-2026-11  $(hostname)  $(date '+%Y-%m-%d %H:%M %Z')  mode=${MODE#--}${N}" "${D}${line}${N}"
  for r in "${ROWS[@]}"; do
    IFS='|' read -r s c d <<<"$r"
    printf '  %s%-4s%s  %-26s %s\n' "$(colour "$s")" "$s" "$N" "$c" "$d"
  done
  verdicts
  if [[ -n "$GATE_HITS" ]]; then printf '\n%s\n%s' "${Y}Gate hits${N}" "$GATE_HITS"; fi
  printf '%s\n' "${D}${line}${N}"
}

[[ $EUID -eq 0 ]] || { echo "Run with sudo."; exit 1; }
[[ -x "$FPM" ]]   || { echo "$FPM not found. Installed: $(find /usr/sbin -maxdepth 1 -name 'php-fpm[0-9]*' -printf '%f ' 2>/dev/null)"; echo "Run: sudo PHPV=<version> $0"; exit 1; }

case "$MODE" in
  --check)    say "Read-only check";            collect; gates; report_state; smoke ;;
  --apply)    say "Gates, then apply stopgap";  collect; gates; apply; collect; report_state ;;
  --rollback) say "Rolling back stopgap";       collect; rollback; collect; report_state ;;
  *)          echo "Usage: sudo $0 [--check|--apply|--rollback]"; exit 2 ;;
esac

summary
PHPFIX_EOF
chmod u+x ~/phpfix.sh

(
set -euo pipefail

V=$(ls /usr/sbin/php-fpm[0-9]* 2>/dev/null | sed 's#.*php-fpm##' || true)
if [[ $(wc -w <<<"$V") -ne 1 ]]; then echo "==> Expected one PHP-FPM, found: ${V:-none}. Stopping."; exit 1; fi
REL=$(. /etc/os-release && echo "$VERSION_CODENAME")
echo "==> PHP-FPM ${V} on ${REL}"

# --- Stop before touching apt sources if packages.sury.org has no build for this release (e.g. Ubuntu 20.04) ---
if ! curl -fsI "https://packages.sury.org/php/dists/${REL}/Release" >/dev/null; then
  echo "==> packages.sury.org has no ${REL} repository. Stopping before any change."; exit 1
fi

# --- Ondrej: Launchpad PPA -> packages.sury.org (safe to re-run) ---
if [[ ! -f /usr/share/keyrings/debsuryorg-archive-keyring.gpg ]]; then
  curl -fsSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
  sudo dpkg -i /tmp/debsuryorg-archive-keyring.deb
fi
echo "deb [signed-by=/usr/share/keyrings/debsuryorg-archive-keyring.gpg] https://packages.sury.org/php/ ${REL} main" \
  | sudo tee /etc/apt/sources.list.d/sury-php.list >/dev/null
for f in /etc/apt/sources.list.d/ondrej-ubuntu-php-*; do
  if [[ -e "$f" && "$f" != *.disabled ]]; then sudo mv "$f" "$f.disabled"; echo "==> Disabled $f"; fi
done
sudo apt-get update

# --- Upgrade only if a newer build exists and the dry run is PHP-only ---
INST=$(dpkg-query -W -f='${Version}' "php${V}-fpm")
CAND=$(apt-cache policy "php${V}-fpm" | awk '/Candidate:/ {print $2}')
echo "==> php${V}-fpm installed ${INST}, candidate ${CAND}"

if dpkg --compare-versions "$CAND" gt "$INST"; then
  PLAN=$(sudo apt-get -s install --only-upgrade "php${V}-*" "libapache2-mod-php${V}" | grep -E '^(Inst|Remv)' || true)
  echo "$PLAN"
  if grep -q '^Remv' <<<"$PLAN" || grep '^Inst' <<<"$PLAN" | grep -vqE "^Inst (php${V}|libapache2-mod-php${V})"; then
    echo "==> Dry run touches more than PHP ${V}. Stopping before any upgrade."; exit 1
  fi
  sudo DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a apt-get install -y --only-upgrade \
    -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold \
    "php${V}-*" "libapache2-mod-php${V}"
  sudo systemctl restart "php${V}-fpm" apache2 || {
    echo "==> Restart failed. If mod_php re-enabled itself: sudo a2dismod php${V} && sudo systemctl restart apache2"; exit 1; }
  systemctl is-active "php${V}-fpm" apache2
else
  echo "==> No newer php${V} build available. Nothing upgraded."
fi

echo "==> Apache modules (want mpm_event + proxy_fcgi, no php_module):"
sudo apache2ctl -M 2>/dev/null | grep -iE 'php|mpm|proxy_fcgi' || true

sudo ~/phpfix.sh
)

Revision history

Sources

This advisory reflects public information as of Nov. 20th.