diff --git a/CLAUDE.md b/CLAUDE.md index 5d154de..5dbcdb8 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -73,7 +73,9 @@ docker run -d -p 80:80 -p 443:443 \ - `user` (required): Username for container user - `domain` (required): Primary domain for Apache vhost - `serveralias`: Additional domains (comma-separated) -- `PHPVER`: PHP version to use (default: 83) +- `PHPVER`: informational only (the image tag selects PHP) +- `DOCROOT_SUBDIR`: serve `public_html/` (Laravel/CI4 `public`); validated like WHP's docrootsub; default empty = unchanged +- `XDEBUG=1`, `PUBLIC_HTTPS_PORT`: DEV mode only - `environment`: DEV or PROD mode ## Important Technical Details diff --git a/README.md b/README.md index 3e5d215..6a4adcd 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ 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 +- `DOCROOT_SUBDIR` – Serve `public_html/` instead of `public_html` (e.g. `public`, `web/public`). Default empty = `public_html`, unchanged. Rules (same as the WHP panel): up to 4 folders; letters, digits, `.`, `_`, `-`; no folder starting with `.` or `-`; no `.`/`..`; `cgi-bin` is reserved. An invalid value stops the container with a message. The folder is created if missing. Everything above it (`vendor/`, `.env`, `app/`) is not reachable over HTTP, and `public_html/.htaccess` is no longer read - `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): @@ -151,6 +152,10 @@ Then visit https://localhost (accept the SSL warning) to complete setup. 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. +### Laravel / CodeIgniter 4 / Symfony style apps + +These serve from a `public/` folder inside the project. Put the whole project in `user/public_html/` and start with `DOCROOT_SUBDIR=public` (`./local-dev.sh -n app -d public`): the web root becomes `public_html/public`, so `vendor/`, `.env` and `app/` are not served. + --- ## Helpful Notes diff --git a/scripts/create-vhost.sh b/scripts/create-vhost.sh index 2d5d4d0..a8f44b4 100644 --- a/scripts/create-vhost.sh +++ b/scripts/create-vhost.sh @@ -2,6 +2,58 @@ alias_block='' +# ---- DOCROOT_SUBDIR (optional, local development: Laravel / CodeIgniter 4 / +# Symfony style apps whose web root is public_html/public). Unset or empty (the +# default, and what every production site uses) renders EXACTLY the same bytes +# as before this variable existed. Validated with the same rules as WHP's +# panel/internal/vhost/docrootsub validator: letters, digits, '.', '_', '-'; +# at most 4 segments, 64 bytes per segment, 128 bytes total; no segment starts +# with '.' or '-'; no '.', '..' or empty segment; the name cgi-bin (and the +# other reserved names) is refused; no leading/trailing slash. +docroot_valid() { + local LC_ALL=C s=$1 g seg + [ -z "$s" ] && return 0 + # Whole-string charset first: `read` below only sees the first line, so a + # newline (config injection) must be refused before the per-segment checks. + [[ $s =~ ^[A-Za-z0-9._/-]+$ ]] || return 1 + [ ${#s} -le 128 ] || return 1 + [[ $s != /* && $s != */ ]] || return 1 + local -a segs + IFS=/ read -r -a segs <<< "$s" + [ ${#segs[@]} -le 4 ] || return 1 + for seg in "${segs[@]}"; do + [ -n "$seg" ] || return 1 + [ ${#seg} -le 64 ] || return 1 + [[ $seg =~ ^[A-Za-z0-9._-]+$ ]] || return 1 + [[ $seg != .* && $seg != -* ]] || return 1 + g=${seg,,} + case "$g" in .git|.well-known|.whp|.site-builder|.svn|.hg|.bzr|.env|.htaccess|cgi-bin) return 1 ;; esac + done + return 0 +} + +docroot="/home/$user/public_html" +parent_block='' +if [ -n "${DOCROOT_SUBDIR:-}" ]; then + if ! docroot_valid "$DOCROOT_SUBDIR"; then + echo "FATAL: invalid DOCROOT_SUBDIR '$DOCROOT_SUBDIR': use up to 4 folders of ASCII letters, digits, '.', '_' and '-' (e.g. public or web/public); no segment may start with '.' or '-', and '.', '..' and cgi-bin are not allowed." >&2 + exit 1 + fi + docroot="/home/$user/public_html/$DOCROOT_SUBDIR" + # Same shape as WHP's shared-tier render: public_html itself stops being a web + # root (no symlinks followed, public_html/.htaccess never read, nothing served). + parent_block=" + # DOCROOT_SUBDIR is set: the web root is $docroot. + # public_html itself is not served, no symlink is followed into the web + # root, and public_html/.htaccess is never read. + Options None + AllowOverride None + Require all denied + + +" +fi + # ---- 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. @@ -27,7 +79,7 @@ cat < /etc/httpd/conf.d/$domain.conf Require all granted - +${parent_block} Options All MultiViews AllowOverride All Require all granted @@ -36,7 +88,7 @@ cat < /etc/httpd/conf.d/$domain.conf ServerName "$domain" $alias_block - DocumentRoot "/home/$user/public_html" + DocumentRoot "$docroot" RewriteEngine on RewriteCond %{SERVER_NAME} =$domain RewriteRule ^ https://%{SERVER_NAME}${https_port_part}%{REQUEST_URI} [END,NE,R=permanent] @@ -52,7 +104,7 @@ SSLCryptoDevice builtin ServerName "$domain" $alias_block - DocumentRoot "/home/$user/public_html" + DocumentRoot "$docroot" SSLCertificateFile /etc/pki/tls/certs/localhost.crt diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 466bfa5..d84e447 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -33,7 +33,12 @@ echo "RemoteIPInternalProxy $docker_network" >> /etc/httpd/conf.d/remoteip.conf source /scripts/detect-memory.sh echo "Container memory: ${CONTAINER_MEMORY_MB}MB | PHP-FPM pm=${PHP_FPM_PM} max_children=${PHP_FPM_MAX_CHILDREN} | Apache workers=${APACHE_MAX_REQUEST_WORKERS}" -/scripts/create-vhost.sh +/scripts/create-vhost.sh || { echo "FATAL: create-vhost.sh failed (see above)"; exit 1; } +# DOCROOT_SUBDIR (local dev; validated by create-vhost.sh above): make sure the +# web root folder exists, created as the customer. Unset = nothing happens. +if [ -n "${DOCROOT_SUBDIR:-}" ]; then + user_mkdir "/home/$user/public_html/$DOCROOT_SUBDIR" +fi /scripts/create-php-config.sh /scripts/create-apache-mpm-config.sh