From 7d6a007ef9da93f4afa0bc46f9480927ee965219 Mon Sep 17 00:00:00 2001 From: jknapp Date: Mon, 23 Nov 2026 06:03:23 -0800 Subject: [PATCH] PHPVER does not select the PHP version: say so, warn in DEV mode The PHP version is the image tag (cac:phpNN); PHPVER was only a leftover default. README/CLAUDE.md now state that, that cac:latest is PHP 8.5 today (not 8.3), and fix the stale PHP_VER build arg name. In DEV mode only, entrypoint.sh and entrypoint-fpm.sh warn loudly at start if an explicitly set PHPVER does not match the image's PHP (new scripts/dev-mode.sh, sourced in DEV only). PROD is untouched. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6 --- CLAUDE.md | 10 +-- README.md | 8 +- scripts/dev-mode.sh | 160 -------------------------------------- scripts/entrypoint-fpm.sh | 8 ++ scripts/entrypoint.sh | 10 +++ 5 files changed, 28 insertions(+), 168 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 982b2e0..5d154de 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -4,7 +4,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co ## Project Overview -Cloud Apache Container (CAC) is a Docker-based PHP web hosting environment that supports multiple PHP versions (7.4 through 8.4) with Apache, designed for both local development and production deployment. +Cloud Apache Container (CAC) is a Docker-based PHP web hosting environment that supports multiple PHP versions (7.4 through 8.6) with Apache, designed for both local development and production deployment. ## Common Development Commands @@ -29,14 +29,14 @@ Cloud Apache Container (CAC) is a Docker-based PHP web hosting environment that docker build -t cac:latest . # Build with specific PHP version -docker build --build-arg PHP_VER=83 -t cac:php83 . +docker build --build-arg PHPVER=83 -t cac:php83 . # Run container manually docker run -d -p 80:80 -p 443:443 \ - -e PHPVER=83 -e environment=DEV \ + -e environment=DEV \ -e uid=$(id -u) -e user=$(whoami) -e domain=localhost \ -v"$(pwd)/user":/home/$(whoami) \ - --name test-container cac:latest + --name test-container cac:php83 ``` ### Server Deployment @@ -64,7 +64,7 @@ docker run -d -p 80:80 -p 443:443 \ - **PROD** (default): Expects external database/cache services 3. **PHP Version Management**: - - Controlled via `PHPVER` environment variable (74, 80, 81, 82, 83, 84) + - Fixed by the IMAGE TAG (`cac:php83`, ...; `cac:latest` = PHP 8.5). The `PHPVER` env var does not switch versions; DEV mode warns if it disagrees with the image - Each version has dedicated install script in `/scripts/` - PHP-FPM configuration dynamically created based on version diff --git a/README.md b/README.md index 6c5d2ec..9a9629d 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,14 @@ This is a base container for running PHP-based applications, supporting multiple **You must have Docker or compatible containerization software running.** +**Which PHP version do I get? The image tag decides, nothing else.** `cac:php83` is PHP 8.3, `cac:php84` is 8.4, `cac:php85` is 8.5, and so on. `cac:latest` is the same image as `cac:php85`, so **`latest` = PHP 8.5 today** (not 8.3). The `PHPVER` environment variable does **not** switch the PHP version of a running container: `docker run -e PHPVER=83 cac:php85` still runs PHP 8.5. In DEV mode a `PHPVER` that does not match the image's PHP prints a warning at start. + --- ## What's New? - **Optimized Image:** The Dockerfile has been refactored for smaller size, faster builds, and improved security. Unnecessary files and caches are removed during build. -- **Pre-built Images for Each PHP Version:** On every push, images for all supported PHP versions are built and pushed to the registry. You can pull the exact version you need (e.g., `cac:php74`, `cac:php84`, or `cac:latest`). +- **Pre-built Images for Each PHP Version:** On every push, images for all supported PHP versions are built and pushed to the registry. You can pull the exact version you need (e.g., `cac:php74`, `cac:php84`, or `cac:latest`, which is PHP 8.5). - **.dockerignore Added:** The build context is now minimized, making builds faster and more secure. --- @@ -89,7 +91,7 @@ Then visit https://localhost (accept the SSL warning) to complete setup. ## Features -- **Multiple PHP Versions:** 7.4, 8.0, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6 (8.6: cac and cac-fpm only) (set with `PHPVER` or `-a` flag) +- **Multiple PHP Versions:** 7.4, 8.0, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6 (8.6: cac and cac-fpm only). Selected by the image tag (`cac:php84`) or the `-a` flag of `local-dev.sh`; `latest` = 8.5 - **Pre-built Images:** Pull the image for your desired PHP version directly from the registry. No need to build locally unless customizing. - **Optimized Build:** Smaller, faster, and more secure images thanks to the improved Dockerfile and `.dockerignore`. - **Automatic Database Setup:** MariaDB is started in DEV mode, credentials are auto-generated and stored in `/home/$user/mysql_creds`. @@ -112,7 +114,7 @@ Then visit https://localhost (accept the SSL warning) to complete setup. **Optional:** - `environment` – Set to `DEV` to start memcached and MySQL locally for development - `serveralias` – Comma-separated list of alternative hostnames -- `PHPVER` – PHP version (see above) +- `PHPVER` – Informational only. The PHP version is the image tag; in DEV mode a mismatch prints a warning --- diff --git a/scripts/dev-mode.sh b/scripts/dev-mode.sh index 5c595c8..7f22417 100644 --- a/scripts/dev-mode.sh +++ b/scripts/dev-mode.sh @@ -17,37 +17,8 @@ ## module stream (php:remi-X.Y) the image's PHP was installed from, so the ## extension always matches the PHP build. -## dnf install that tolerates an unreachable repo (mariadb.org down must not -## stop memcached/posix/the AlmaLinux mariadb-server fallback from installing). -dev_dnf() { dnf install -y --setopt=skip_if_unavailable=True "$@"; } - dev_log() { echo "dev-mode: $*"; } -## Failures are collected and reported at the end by dev_report_failures, which -## exits non-zero: a DEV container that cannot give you the database/cache/ -## debugger it promised must not look healthy (the HEALTHCHECK only probes -## Apache, so it would stay green). -DEV_FAILURES=() -dev_fail() { DEV_FAILURES+=("$1"); echo "dev-mode: ERROR: $1" >&2; } - -dev_report_failures() { - [ "${#DEV_FAILURES[@]}" -eq 0 ] && return 0 - { - echo "################################################################" - echo "# FATAL: DEV mode could not set up what it promised:" - for f in "${DEV_FAILURES[@]}"; do echo "# - $f"; done - echo "# (environment=DEV installs MariaDB, memcached and optional PHP" - echo "# extensions from the network at start; check connectivity/DNS.)" - echo "# Exiting non-zero instead of reporting a healthy container." - echo "################################################################" - } >&2 - exit 1 -} - -dev_truthy() { - case "${1,,}" in 1|true|yes|on) return 0 ;; *) return 1 ;; esac -} - ## PHP version in the image, as "83" for 8.3. The tag selects the version; the ## PHPVER env var does NOT (README). Warn when they disagree so a stale ## -e PHPVER=84 on a cac:php85 container is not mistaken for PHP 8.4. @@ -64,134 +35,3 @@ dev_check_phpver() { echo "################################################################" >&2 fi } - -## php.ini overrides for development. Written to the scan dir (wins over -## /etc/php.ini, sorts before zz-php2shell-mitigation.ini which sets other keys). -## Must run BEFORE php-fpm starts. -dev_write_php_ini() { - cat > /etc/php.d/99-dev.ini <<'EOF' -; written by dev-mode.sh (environment=DEV only) -; (error_reporting stays at prod-php.ini's E_ALL & ~E_DEPRECATED & ~E_STRICT: with -; deprecations on, wp-cli and PHP 8.5's own startup notices flood every CLI call) -display_errors = On -; edit a PHP file, reload the page: no 60 s opcache staleness -opcache.revalidate_freq = 0 -EOF -} - -## Install php-process (posix, pcntl, ...) if missing. Best effort: not fatal. -dev_install_posix() { - php -m 2>/dev/null | grep -qix posix && return 0 - dev_log "installing php-process (posix extension)" - dev_dnf php-process >/dev/null 2>&1 \ - || echo "dev-mode: WARNING: could not install php-process (posix extension unavailable)" >&2 -} - -## XDEBUG=1 -> install + configure Xdebug 3 for the image's PHP. Must run before -## php-fpm starts. Client settings (all overridable): -## XDEBUG_MODE debug (default; e.g. debug,develop,coverage) -## XDEBUG_START yes (default; try to connect on every request) | trigger -## XDEBUG_CLIENT_HOST host.docker.internal (default) -## XDEBUG_CLIENT_PORT 9003 (default) -dev_setup_xdebug() { - dev_truthy "${XDEBUG:-}" || return 0 - if ! php -m 2>/dev/null | grep -qix xdebug; then - dev_log "XDEBUG requested: installing php-pecl-xdebug3 for PHP $(php -r 'echo PHP_VERSION;')" - if ! dev_dnf php-pecl-xdebug3 >/dev/null 2>&1 && ! dev_dnf php-pecl-xdebug >/dev/null 2>&1; then - dev_fail "XDEBUG=1 but no Xdebug package could be installed for this PHP" - return 0 - fi - fi - local mode start host port - mode=${XDEBUG_MODE:-debug} - start=${XDEBUG_START:-yes} - host=${XDEBUG_CLIENT_HOST:-host.docker.internal} - port=${XDEBUG_CLIENT_PORT:-9003} - # The values land in an ini file: refuse anything that is not plain. - if [[ ! $mode =~ ^[a-z,]+$ || ! $start =~ ^(yes|no|trigger|default)$ || \ - ! $host =~ ^[A-Za-z0-9._:-]+$ || ! $port =~ ^[0-9]{1,5}$ ]]; then - dev_fail "invalid XDEBUG_MODE/XDEBUG_START/XDEBUG_CLIENT_HOST/XDEBUG_CLIENT_PORT value" - return 0 - fi - cat > /etc/php.d/98-xdebug-dev.ini </dev/null | grep -qix xdebug; then - dev_log "Xdebug loaded (mode=$mode start=$start client=$host:$port)" - else - dev_fail "XDEBUG=1: Xdebug installed but php does not load it" - fi -} - -## Install MariaDB + memcached if absent, start both, create the dev database. -## $1 = customer user. Uses safe-paths.sh helpers (as_user, drop_symlink, -## user_mkdir): the caller has already sourced it. -dev_install_db_and_cache() { - local u=$1 before=${#DEV_FAILURES[@]} - if ! command -v mysqld >/dev/null 2>&1; then - dev_log "installing MariaDB" - if ! dev_dnf --allowerasing MariaDB-server MariaDB-client >/tmp/dev-mariadb-install.log 2>&1; then - dev_log "mariadb.org packages failed (tail of log below); trying AlmaLinux mariadb-server" - tail -n 8 /tmp/dev-mariadb-install.log >&2 - dev_dnf --disablerepo=mariadb mariadb-server >/tmp/dev-mariadb-install2.log 2>&1 \ - || { tail -n 8 /tmp/dev-mariadb-install2.log >&2; dev_fail "MariaDB server could not be installed"; } - fi - fi - if ! command -v memcached >/dev/null 2>&1; then - dev_log "installing memcached" - dev_dnf memcached >/tmp/dev-memcached-install.log 2>&1 \ - || { tail -n 8 /tmp/dev-memcached-install.log >&2; dev_fail "memcached could not be installed"; } - fi - [ "${#DEV_FAILURES[@]}" -eq "$before" ] || return 0 - - sed -r -i 's/session.save_path="memcache:11211/session.save_path="localhost:11211/' /etc/php.ini - # /var/lib/mysql is a docker volume: make sure the mysql user (uid differs by - # package source) owns it, and initialise it if the package did not. - if [ "$(stat -c %U /var/lib/mysql 2>/dev/null)" != "mysql" ]; then chown -R mysql:mysql /var/lib/mysql; fi - if [ ! -d /var/lib/mysql/mysql ]; then - dev_log "initialising empty MariaDB data directory" - mariadb-install-db --user=mysql --datadir=/var/lib/mysql >/tmp/dev-mariadb-initdb.log 2>&1 \ - || { tail -n 8 /tmp/dev-mariadb-initdb.log >&2; dev_fail "mariadb-install-db failed"; return 0; } - fi - # `mysql` is a deprecated alias in MariaDB 11 and prints a warning per call. - local mysql_cli; mysql_cli=$(command -v mariadb || command -v mysql) - nohup mysqld --user=mysql >/var/log/mysqld-dev.log 2>&1 & - local i - for i in $(seq 1 60); do - mysqladmin ping --silent >/dev/null 2>&1 && break - sleep 1 - done - if ! mysqladmin ping --silent >/dev/null 2>&1; then - tail -n 15 /var/log/mysqld-dev.log >&2 - dev_fail "MariaDB did not start within 60 s (log: /var/log/mysqld-dev.log)" - return 0 - fi - dev_log "MariaDB is up" - - if [ ! -f "/home/$u/mysql_creds" ]; then - local mysql_user mysql_password mysql_db - mysql_user=$(tr -dc A-Za-z0-9 /dev/null - printf '%s\n' "MySQL User: $mysql_user" "MySQL Password: $mysql_password" "MySQL Database: devdb_${mysql_db}" \ - | as_user tee "/home/$u/mysql_creds" - fi - /usr/bin/memcached -d -u "$u" || dev_fail "memcached failed to start" -} diff --git a/scripts/entrypoint-fpm.sh b/scripts/entrypoint-fpm.sh index 36fa0af..6632b7d 100755 --- a/scripts/entrypoint-fpm.sh +++ b/scripts/entrypoint-fpm.sh @@ -1,5 +1,8 @@ #!/usr/bin/env bash +# PHPVER does not select anything at runtime: the PHP version is the image tag +# (cac-fpm:php83, ...). Remembered so DEV mode can warn on a mismatch. +PHPVER_REQUESTED="$PHPVER" if [ -z "$PHPVER" ]; then PHPVER="83"; fi @@ -35,6 +38,11 @@ echo "Container memory: ${CONTAINER_MEMORY_MB}MB | PHP-FPM pm=${PHP_FPM_PM} max_ /scripts/create-php-config.sh mkdir -p /run/php-fpm/ +# DEV only: PHP overrides + optional Xdebug must be in place before php-fpm starts. +if [[ $environment == 'DEV' ]]; then + source /scripts/dev-mode.sh + dev_check_phpver "$PHPVER_REQUESTED" +fi # php-fpm opens its slowlog under /home/$user/logs as root: last fail-closed # check immediately before it starts (safe-paths.sh). require_safe_logs /home/$user/logs/php-fpm diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 96eee39..cfb9a17 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# PHPVER does not select anything at runtime: the PHP version is the image tag +# (cac:php83, cac:php85, ...). Remember what the caller asked for so DEV mode +# can warn on a mismatch (dev-mode.sh); the default below is unchanged. +PHPVER_REQUESTED="$PHPVER" if [ -z "$PHPVER" ]; then PHPVER="83"; fi @@ -37,6 +41,12 @@ if [ -f /etc/httpd/conf.d/ssl.conf ]; then mv /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.bak fi +# DEV only: PHP overrides + optional Xdebug must be in place before php-fpm starts. +if [[ $environment == 'DEV' ]]; then + source /scripts/dev-mode.sh + dev_check_phpver "$PHPVER_REQUESTED" +fi + # httpd and php-fpm open their logs under /home/$user/logs as root: last # fail-closed check immediately before they start (safe-paths.sh). require_safe_logs /home/$user/logs/apache /home/$user/logs/php-fpm