diff --git a/README.md b/README.md index 1018b22..3e5d215 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,29 @@ Then visit https://localhost (accept the SSL warning) to complete setup. - `environment` – Set to `DEV` to start memcached and MariaDB locally for development (see "DEV mode" below) - `serveralias` – Comma-separated list of alternative hostnames - `PHPVER` – Informational only. The PHP version is the image tag; in DEV mode a mismatch prints a warning +- `PUBLIC_HTTPS_PORT` – DEV only: the host port mapped to 443, so the HTTP to HTTPS redirect keeps it (e.g. `8443`) + +**DEV mode only** (`environment=DEV`; production containers never read these): +- `XDEBUG` – `1` installs and enables Xdebug 3 for the image's PHP at start (see below) +- `XDEBUG_MODE` (default `debug`), `XDEBUG_START` (`yes` default, or `trigger`), `XDEBUG_CLIENT_HOST` (default `host.docker.internal`), `XDEBUG_CLIENT_PORT` (default `9003`) + +--- + +## DEV mode + +`environment=DEV` is for your laptop, not production. At start it: +- installs **MariaDB** (11.4 from mariadb.org, falling back to AlmaLinux's 10.11) and **memcached**, starts them, and writes a random database login to `/home/$user/mysql_creds` (backups every 15 minutes to `_db_backups`); +- turns on `display_errors` and sets `opcache.revalidate_freq=0` (PHP file edits show immediately); `error_reporting` stays at the production value (no deprecation notices), set it in your app or `.user.ini` if you want them; +- installs the `posix` extension (`php-process`); +- **exits with an error (non-zero) and a loud message in `docker logs` if MariaDB, memcached or a requested Xdebug cannot be installed or started**, instead of looking healthy. The first start needs internet access. + +### Debugging with Xdebug (DEV mode only) + +```bash +./local-dev.sh -n myproject -a 84 -x # or: docker run ... -e XDEBUG=1 --add-host host.docker.internal:host-gateway ... +``` + +Xdebug 3 is installed from the same Remi module stream as the image's PHP. Configure your IDE as a **debug client listening on port 9003**, with a path mapping from your project folder (`./user/public_html`) to `/home//public_html` in the container. The container connects out to `host.docker.internal:9003` (`--add-host host.docker.internal:host-gateway` is needed on Linux; `local-dev.sh` adds it). Use `XDEBUG_START=trigger` to debug only requests that carry the `XDEBUG_TRIGGER` cookie/parameter, and `XDEBUG_MODE=debug,develop` etc. to change the mode. --- diff --git a/scripts/create-vhost.sh b/scripts/create-vhost.sh index 9e734a5..2d5d4d0 100644 --- a/scripts/create-vhost.sh +++ b/scripts/create-vhost.sh @@ -2,6 +2,23 @@ alias_block='' +# ---- HTTP -> HTTPS redirect keeps a non-standard HTTPS port (DEV only, set by +# local-dev.sh from -s). Without it a `-s 8443` mapping is redirected to +# https://host/ (port 443) and breaks. Unset, non-DEV or 443 = unchanged bytes. +https_port_part='' +if [ "${environment:-}" = "DEV" ] && [[ "${PUBLIC_HTTPS_PORT:-}" =~ ^[0-9]{1,5}$ ]] && [ "$PUBLIC_HTTPS_PORT" != "443" ]; then + https_port_part=":$PUBLIC_HTTPS_PORT" +fi + +#Create Server Alias Block +if [ ! -z $serveralias ]; then + for alias in $(echo $serveralias | tr ',' ' ') + do + alias_block=$alias_block"ServerAlias $alias + " + done +fi + cat < /etc/httpd/conf.d/$domain.conf @@ -22,7 +39,7 @@ cat < /etc/httpd/conf.d/$domain.conf DocumentRoot "/home/$user/public_html" RewriteEngine on RewriteCond %{SERVER_NAME} =$domain - RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] + RewriteRule ^ https://%{SERVER_NAME}${https_port_part}%{REQUEST_URI} [END,NE,R=permanent] Listen 443 https diff --git a/scripts/dev-mode.sh b/scripts/dev-mode.sh index 9a85105..5c595c8 100644 --- a/scripts/dev-mode.sh +++ b/scripts/dev-mode.sh @@ -44,6 +44,10 @@ dev_report_failures() { 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. @@ -61,6 +65,69 @@ dev_check_phpver() { 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. diff --git a/scripts/entrypoint-fpm.sh b/scripts/entrypoint-fpm.sh index 967c285..b0b9b6d 100755 --- a/scripts/entrypoint-fpm.sh +++ b/scripts/entrypoint-fpm.sh @@ -42,6 +42,9 @@ mkdir -p /run/php-fpm/ if [[ $environment == 'DEV' ]]; then source /scripts/dev-mode.sh dev_check_phpver "$PHPVER_REQUESTED" + dev_write_php_ini + dev_install_posix + dev_setup_xdebug fi # php-fpm opens its slowlog under /home/$user/logs as root: last fail-closed # check immediately before it starts (safe-paths.sh). diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index c560143..466bfa5 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -45,6 +45,9 @@ fi if [[ $environment == 'DEV' ]]; then source /scripts/dev-mode.sh dev_check_phpver "$PHPVER_REQUESTED" + dev_write_php_ini + dev_install_posix + dev_setup_xdebug fi # httpd and php-fpm open their logs under /home/$user/logs as root: last