diff --git a/.github/actions/action.yml b/.github/actions/action.yml index 0e67983a..33650e57 100644 --- a/.github/actions/action.yml +++ b/.github/actions/action.yml @@ -18,6 +18,9 @@ inputs: user_id: description: user id required: true + web: + description: web server variant, such as apache + required: false runs: using: "composite" steps: @@ -25,6 +28,7 @@ runs: env: PHP_VER: ${{ inputs.version }} PHP_DEV: ${{ inputs.dev }} + PHP_WEB: ${{ inputs.web }} WODBY_USER_ID: ${{ inputs.user_id }} LATEST: ${{ inputs.latest }} LATEST_MAJOR: ${{ inputs.latest_major }} diff --git a/.github/actions/release.sh b/.github/actions/release.sh index f932174c..9e2ec66b 100755 --- a/.github/actions/release.sh +++ b/.github/actions/release.sh @@ -26,6 +26,12 @@ if [[ "${GITHUB_REF}" == refs/heads/master || "${GITHUB_REF}" == refs/tags/* ]]; fi fi + # Web server variants, such as apache, carry their name in every tag. + if [[ -n "${PHP_WEB}" ]]; then + minor_tag="${minor_tag}-${PHP_WEB}" + major_tag="${major_tag}-${PHP_WEB}" + fi + tags=("${minor_tag}") if [[ -n "${LATEST_MAJOR}" ]]; then tags+=("${major_tag}") @@ -38,7 +44,7 @@ if [[ "${GITHUB_REF}" == refs/heads/master || "${GITHUB_REF}" == refs/tags/* ]]; tags+=("${major_tag}-${image_revision}") fi else - if [[ -n "${LATEST}" ]]; then + if [[ -n "${LATEST}" && -z "${PHP_WEB}" ]]; then if [[ -z "${PHP_DEV}" ]]; then tags+=("latest") else diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 409804a1..0cd5db8e 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -63,6 +63,9 @@ jobs: user_id: - 1000 - 501 + web: + - '' + - 'apache' include: - arch: amd64 runner: ubuntu-24.04 @@ -74,6 +77,9 @@ jobs: exclude: - dev: '' user_id: 501 + # The Apache variant has no development images. + - dev: 'dev' + web: 'apache' runs-on: ${{ matrix.runner }} steps: - uses: actions/checkout@v7 @@ -84,6 +90,7 @@ jobs: PHP_VER: ${{ matrix.php.version }} ARCH: ${{ matrix.arch }} PHP_DEV: ${{ matrix.dev }} + PHP_WEB: ${{ matrix.web }} WODBY_USER_ID: ${{ matrix.user_id }} WODBY_GROUP_ID: ${{ matrix.group_id }} working-directory: ${{ matrix.php.workdir }} @@ -106,6 +113,7 @@ jobs: PHP_VER: ${{ matrix.php.version }} ARCH: ${{ matrix.arch }} PHP_DEV: ${{ matrix.dev }} + PHP_WEB: ${{ matrix.web }} WODBY_USER_ID: ${{ matrix.user_id }} WODBY_GROUP_ID: ${{ matrix.group_id }} working-directory: ${{ matrix.php.workdir }} @@ -126,9 +134,14 @@ jobs: user_id: - 1000 - 501 + web: + - '' + - 'apache' exclude: - dev: '' user_id: 501 + - dev: 'dev' + web: 'apache' steps: - uses: actions/checkout@v7 - uses: docker/login-action@v4 @@ -141,6 +154,7 @@ jobs: workdir: ${{ matrix.php.workdir }} dev: ${{ matrix.dev }} user_id: ${{ matrix.user_id }} + web: ${{ matrix.web }} latest: ${{ matrix.php.latest }} latest_major: ${{ matrix.php.latest_major }} diff --git a/.image-revision-aliases.json b/.image-revision-aliases.json index 0d63ad7c..71ad23fd 100644 --- a/.image-revision-aliases.json +++ b/.image-revision-aliases.json @@ -27,6 +27,13 @@ "{major}-dev-macos" ], "full": "{version}-dev-macos" + }, + { + "short": [ + "{minor}-apache", + "{major}-apache" + ], + "full": "{version}-apache" } ] }, @@ -52,6 +59,12 @@ "{minor}-dev-macos" ], "full": "{version}-dev-macos" + }, + { + "short": [ + "{minor}-apache" + ], + "full": "{version}-apache" } ] }, @@ -77,6 +90,12 @@ "{minor}-dev-macos" ], "full": "{version}-dev-macos" + }, + { + "short": [ + "{minor}-apache" + ], + "full": "{version}-apache" } ] }, @@ -102,6 +121,12 @@ "{minor}-dev-macos" ], "full": "{version}-dev-macos" + }, + { + "short": [ + "{minor}-apache" + ], + "full": "{version}-apache" } ] } diff --git a/8/Dockerfile b/8/Dockerfile index 0234f157..d7753758 100644 --- a/8/Dockerfile +++ b/8/Dockerfile @@ -1,9 +1,10 @@ # check=skip=InvalidDefaultArgInFrom -# The Makefile supplies the required digest-pinned BASE_IMAGE argument. +# The Makefile supplies the required digest-pinned BASE_IMAGE argument and +# selects the stage: php (the default) or apache. ARG PHP_VER ARG BASE_IMAGE -FROM ${BASE_IMAGE} +FROM ${BASE_IMAGE} AS php LABEL com.wodby.ci.cache="composer" @@ -445,3 +446,31 @@ COPY init /docker-entrypoint-init.d/ ENTRYPOINT ["/docker-entrypoint.sh"] CMD ["sudo", "-E", "php-fpm"] + +# PHP-FPM with Apache in front of it in the same container. Apache serves the +# application's static files and passes PHP scripts to the local PHP-FPM, so +# one image runs an application without a separate web server service. +FROM php AS apache + +USER root + +RUN set -xe; \ + apk add --update --no-cache apache2 apache2-proxy; \ + # The image's own configuration replaces Alpine's defaults. \ + rm -f /etc/apache2/conf.d/*.conf; \ + touch /etc/apache2/vhost.conf; \ + chown wodby:wodby /etc/apache2/httpd.conf /etc/apache2/vhost.conf; \ + install -d -o root -g root /run/apache2; \ + echo 'wodby ALL=(root) NOPASSWD:SETENV: /usr/sbin/httpd' > /etc/sudoers.d/wodby-apache + +USER wodby + +EXPOSE 80 + +COPY apache/templates /etc/gotpl/apache/ +COPY apache/bin /usr/local/bin/ + +CMD ["php-fpm-apache"] + +# Builds without a target get the PHP-FPM image. +FROM php diff --git a/8/Makefile b/8/Makefile index f6c1926c..9adefecb 100644 --- a/8/Makefile +++ b/8/Makefile @@ -25,8 +25,24 @@ ifeq ($(WODBY_GROUP_ID),) WODBY_GROUP_ID := 1000 endif +# PHP_WEB=apache builds the variant with Apache in front of PHP-FPM. +ifneq ($(PHP_WEB),) + ifneq ($(PHP_WEB),apache) + $(error Unsupported PHP_WEB: $(PHP_WEB)) + endif + ifneq ($(PHP_DEV),) + $(error PHP_WEB has no development variant) + endif + TARGET := $(PHP_WEB) +else + TARGET := php +endif + ifeq ($(TAG),) - ifneq ($(PHP_DEV),) + ifneq ($(PHP_WEB),) + TAG := $(PHP_VER_MINOR)-$(PHP_WEB) + NAME := $(NAME)-$(PHP_WEB) + else ifneq ($(PHP_DEV),) ifeq ($(WODBY_USER_ID),501) TAG := $(PHP_VER_MINOR)-dev-macos NAME := $(NAME)-dev-macos @@ -59,6 +75,7 @@ build: --build-arg PHP_DEV=$(PHP_DEV) \ --build-arg WODBY_USER_ID=$(WODBY_USER_ID) \ --build-arg WODBY_GROUP_ID=$(WODBY_GROUP_ID) \ + --target $(TARGET) \ $(PROXY_BUILD_ARGS) $(PIE_BUILD_SECRET) \ ./ @@ -68,6 +85,7 @@ build-debug: --build-arg PHP_DEV=$(PHP_DEV) \ --build-arg WODBY_USER_ID=$(WODBY_USER_ID) \ --build-arg WODBY_GROUP_ID=$(WODBY_GROUP_ID) \ + --target $(TARGET) \ $(PROXY_BUILD_ARGS) $(PIE_BUILD_SECRET) \ --no-cache --progress=plain ./ 2>&1 | tee build.log @@ -77,6 +95,7 @@ buildx-build: --build-arg PHP_DEV=$(PHP_DEV) \ --build-arg WODBY_USER_ID=$(WODBY_USER_ID) \ --build-arg WODBY_GROUP_ID=$(WODBY_GROUP_ID) \ + --target $(TARGET) \ $(PROXY_BUILD_ARGS) $(PIE_BUILD_SECRET) \ --load \ ./ @@ -87,6 +106,7 @@ buildx-push: --build-arg PHP_DEV=$(PHP_DEV) \ --build-arg WODBY_USER_ID=$(WODBY_USER_ID) \ --build-arg WODBY_GROUP_ID=$(WODBY_GROUP_ID) \ + --target $(TARGET) \ $(PROXY_BUILD_ARGS) $(PIE_BUILD_SECRET) \ ./ @@ -97,7 +117,11 @@ buildx-imagetools-create: .PHONY: buildx-imagetools-create test: +ifneq ($(PHP_WEB),) + cd ./tests && IMAGE=$(REPO):$(TAG) ./$(PHP_WEB).sh +else cd ./tests && IMAGE=$(REPO):$(TAG) ./run.sh +endif push: docker push $(REPO):$(TAG) diff --git a/8/apache/bin/php-fpm-apache b/8/apache/bin/php-fpm-apache new file mode 100755 index 00000000..726d9f4c --- /dev/null +++ b/8/apache/bin/php-fpm-apache @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +# Runs PHP-FPM and Apache side by side in the foreground. The container stops +# when either of them exits, so the orchestrator restarts it. A stop signal +# shuts both down gracefully: Apache finishes the requests it is serving and +# PHP-FPM the scripts it is running. + +set -o pipefail + +if [[ -n "${DEBUG}" ]]; then + set -x +fi + +sudo -E php-fpm & +fpm=$! + +sudo -E httpd -DFOREGROUND & +httpd=$! + +stop() { + trap - TERM INT QUIT + sudo -E httpd -k graceful-stop + kill -QUIT "${fpm}" 2>/dev/null + wait + exit 0 +} + +trap stop TERM INT QUIT + +wait -n +status=$? + +trap - TERM INT QUIT +echo "PHP-FPM or Apache exited with status ${status}; stopping the container." >&2 +kill -TERM "${fpm}" "${httpd}" 2>/dev/null +wait +exit "${status}" diff --git a/8/apache/templates/httpd.conf.tmpl b/8/apache/templates/httpd.conf.tmpl new file mode 100644 index 00000000..7b1ea712 --- /dev/null +++ b/8/apache/templates/httpd.conf.tmpl @@ -0,0 +1,88 @@ +ServerRoot "/etc/apache2" +PidFile /run/apache2/httpd.pid + +Listen {{ getenv "APACHE_PORT" "80" }} + +# PHP runs in PHP-FPM, so Apache uses the threaded event MPM. +LoadModule mpm_event_module /usr/lib/apache2/mod_mpm_event.so + +LoadModule access_compat_module /usr/lib/apache2/mod_access_compat.so +LoadModule alias_module /usr/lib/apache2/mod_alias.so +LoadModule auth_basic_module /usr/lib/apache2/mod_auth_basic.so +LoadModule authn_core_module /usr/lib/apache2/mod_authn_core.so +LoadModule authn_file_module /usr/lib/apache2/mod_authn_file.so +LoadModule authz_core_module /usr/lib/apache2/mod_authz_core.so +LoadModule authz_groupfile_module /usr/lib/apache2/mod_authz_groupfile.so +LoadModule authz_host_module /usr/lib/apache2/mod_authz_host.so +LoadModule authz_user_module /usr/lib/apache2/mod_authz_user.so +LoadModule deflate_module /usr/lib/apache2/mod_deflate.so +LoadModule dir_module /usr/lib/apache2/mod_dir.so +LoadModule env_module /usr/lib/apache2/mod_env.so +LoadModule expires_module /usr/lib/apache2/mod_expires.so +LoadModule filter_module /usr/lib/apache2/mod_filter.so +LoadModule headers_module /usr/lib/apache2/mod_headers.so +LoadModule log_config_module /usr/lib/apache2/mod_log_config.so +LoadModule mime_module /usr/lib/apache2/mod_mime.so +LoadModule negotiation_module /usr/lib/apache2/mod_negotiation.so +LoadModule proxy_module /usr/lib/apache2/mod_proxy.so +LoadModule proxy_fcgi_module /usr/lib/apache2/mod_proxy_fcgi.so +LoadModule remoteip_module /usr/lib/apache2/mod_remoteip.so +LoadModule reqtimeout_module /usr/lib/apache2/mod_reqtimeout.so +LoadModule rewrite_module /usr/lib/apache2/mod_rewrite.so +LoadModule setenvif_module /usr/lib/apache2/mod_setenvif.so +LoadModule unixd_module /usr/lib/apache2/mod_unixd.so +LoadModule version_module /usr/lib/apache2/mod_version.so + +# Apache only reads files; PHP-FPM writes them as its own user. +User {{ getenv "APACHE_USER" "www-data" }} +Group {{ getenv "APACHE_GROUP" "www-data" }} + +ServerName localhost + + + AllowOverride None + Require all denied + + + + Require all denied + + +ErrorLog /proc/self/fd/2 +LogLevel {{ getenv "APACHE_LOG_LEVEL" "warn" }} + +LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined +LogFormat "%h %l %u %t \"%r\" %>s %b" common +CustomLog /proc/self/fd/1 {{ getenv "APACHE_LOG_FORMAT" "common" }} env=!dontlog + +RequestHeader unset Proxy early + +TypesConfig /etc/apache2/mime.types +AddType application/x-compress .Z +AddType application/x-gzip .gz .tgz + +Timeout {{ getenv "APACHE_TIMEOUT" "60" }} +KeepAlive {{ getenv "APACHE_KEEP_ALIVE" "On" }} +MaxKeepAliveRequests {{ getenv "APACHE_MAX_KEEP_ALIVE_REQUESTS" "100" }} +KeepAliveTimeout {{ getenv "APACHE_KEEP_ALIVE_TIMEOUT" "5" }} +UseCanonicalName {{ getenv "APACHE_USE_CANONICAL_NAME" "Off" }} +AccessFileName .htaccess +ServerTokens {{ getenv "APACHE_SERVER_TOKENS" "Prod" }} +ServerSignature {{ getenv "APACHE_SERVER_SIGNATURE" "Off" }} +HostnameLookups Off +# A stop lets running requests finish for at most this many seconds. +GracefulShutdownTimeout {{ getenv "APACHE_GRACEFUL_SHUTDOWN_TIMEOUT" "20" }} + +RequestReadTimeout {{ getenv "APACHE_REQUEST_READ_TIMEOUT" "header=20-40,MinRate=500 body=20,MinRate=500" }} + + + ServerLimit {{ getenv "APACHE_MPM_EVENT_SERVER_LIMIT" "16" }} + MaxRequestWorkers {{ getenv "APACHE_MPM_EVENT_MAX_REQUEST_WORKERS" "400" }} + StartServers {{ getenv "APACHE_MPM_EVENT_START_SERVERS" "3" }} + ThreadsPerChild {{ getenv "APACHE_MPM_EVENT_THREADS_PER_CHILD" "25" }} + ThreadLimit {{ getenv "APACHE_MPM_EVENT_THREAD_LIMIT" "64" }} + + +Include /etc/apache2/vhost.conf +# Extra configuration files, for example mounted by a deployment. +IncludeOptional /etc/apache2/conf.d/*.conf diff --git a/8/apache/templates/vhost.conf.tmpl b/8/apache/templates/vhost.conf.tmpl new file mode 100644 index 00000000..dcd60631 --- /dev/null +++ b/8/apache/templates/vhost.conf.tmpl @@ -0,0 +1,48 @@ +{{ $docroot := getenv "APACHE_DOCUMENT_ROOT" "/var/www/html" }} +{{ $fpm := printf "%s:%s" (getenv "APACHE_FPM_HOST" "127.0.0.1") (getenv "APACHE_FPM_PORT" "9000") }} + + DocumentRoot "{{ $docroot }}" + ServerName default + + DirectoryIndex {{ getenv "APACHE_DIRECTORY_INDEX" "index.php index.html" }} + + + Options FollowSymLinks{{ if getenv "APACHE_INDEXES_ENABLED" }} Indexes{{ end }} + AllowOverride {{ getenv "APACHE_ALLOW_OVERRIDE" "All" }} + Require all granted + # Applications read the Authorization header themselves. + CGIPassAuth On + + + # PHP scripts go to the PHP-FPM of this container. + + ProxySet connectiontimeout={{ getenv "APACHE_FCGI_PROXY_CONN_TIMEOUT" "5" }} timeout={{ getenv "APACHE_FCGI_PROXY_TIMEOUT" "900" }} + + + + SetHandler "proxy:fcgi://{{ $fpm }}" + + + + # TLS ends at the proxy in front of the container, which says so in + # X-Forwarded-Proto. + SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on + + {{ if getenv "APACHE_REMOTEIP_TRUSTED_PROXIES" }} + # The client address comes from X-Forwarded-For when the request passed + # these proxies. + RemoteIPHeader {{ getenv "APACHE_REMOTEIP_HEADER" "X-Forwarded-For" }} + RemoteIPInternalProxy {{ getenv "APACHE_REMOTEIP_TRUSTED_PROXIES" }} + {{ end }} + + + Require all denied + + + + Require all denied + + + SetEnvIf Request_URI "^/\.healthz$" dontlog + RedirectMatch 204 "^/\.healthz$" + diff --git a/8/bin/actions.mk b/8/bin/actions.mk index a0ababda..6d8db5df 100644 --- a/8/bin/actions.mk +++ b/8/bin/actions.mk @@ -14,8 +14,15 @@ delay_seconds ?= 0 is_hash ?= 0 branch = "" # Some symbols in env vars break cgi-fcgi -command ?= env -i SCRIPT_NAME="/ping" SCRIPT_FILENAME="/ping" REQUEST_METHOD=GET cgi-fcgi -bind -connect "${host}":9001 | grep -q "pong" +fpm_command = env -i SCRIPT_NAME="/ping" SCRIPT_FILENAME="/ping" REQUEST_METHOD=GET cgi-fcgi -bind -connect "${host}":9001 | grep -q "pong" +# The Apache variant is ready when Apache answers as well. +ifneq ($(wildcard /usr/sbin/httpd),) +command ?= $(fpm_command) && curl -fsS -o /dev/null "http://${host}:$${APACHE_PORT:-80}/.healthz" +service = PHP-FPM/Apache +else +command ?= $(fpm_command) service = PHP-FPM +endif default: check-ready diff --git a/8/docker-entrypoint.sh b/8/docker-entrypoint.sh index 7a55770b..d7032669 100755 --- a/8/docker-entrypoint.sh +++ b/8/docker-entrypoint.sh @@ -92,7 +92,11 @@ process_templates() { fi _gotpl "msmtprc.tmpl" "/etc/msmtprc" - _gotpl "mariadb-client.cnf.tmpl" "/etc/my.cnf.d/mariadb-client.cnf" + _gotpl "mariadb-client.cnf.tmpl" "/etc/my.cnf.d/mariadb-client.cnf" + + # Only the Apache variant has these templates. + _gotpl "apache/httpd.conf.tmpl" "/etc/apache2/httpd.conf" + _gotpl "apache/vhost.conf.tmpl" "/etc/apache2/vhost.conf" } disable_modules() { diff --git a/8/templates/zz-www.conf.tmpl b/8/templates/zz-www.conf.tmpl index 55959c73..f8220403 100644 --- a/8/templates/zz-www.conf.tmpl +++ b/8/templates/zz-www.conf.tmpl @@ -1,5 +1,7 @@ [global] log_level = {{ getenv "PHP_FPM_LOG_LEVEL" "notice" }} +; On a graceful stop (SIGQUIT), running scripts get this many seconds to finish. +process_control_timeout = {{ getenv "PHP_FPM_PROCESS_CONTROL_TIMEOUT" "20" }} [www] clear_env = {{ getenv "PHP_FPM_CLEAR_ENV" "yes" }} diff --git a/8/tests/README.md b/8/tests/README.md index 53089e57..966c5254 100644 --- a/8/tests/README.md +++ b/8/tests/README.md @@ -44,3 +44,12 @@ anonymous volumes on both success and failure, and prints service logs on failur Service readiness retries are bounded. Coverage engines run separately, and the upload fixture talks FastCGI directly to avoid nginx buffering the request before PHP can observe progress. + +## Apache variant + +`PHP_WEB=apache make test` runs `apache.sh` against the `-apache` image instead +of this suite: its PHP layer is the image tested above. It checks that Apache +serves static files and `.htaccess` rewrites, passes PHP scripts, the +`Authorization` header and `HTTPS=on` behind a TLS-terminating proxy to PHP-FPM, +denies hidden files, answers `/.healthz`, stops gracefully, and stops the +container when PHP-FPM exits. diff --git a/8/tests/apache.sh b/8/tests/apache.sh new file mode 100755 index 00000000..334b1955 --- /dev/null +++ b/8/tests/apache.sh @@ -0,0 +1,107 @@ +#!/usr/bin/env bash + +# Tests of the Apache variant: Apache serves static files and .htaccess rules +# and passes PHP scripts to the PHP-FPM of the same container. The PHP layer is +# the same as in the PHP-FPM image, which run.sh tests. + +set -eo pipefail + +if [[ -n "${DEBUG}" ]]; then + set -x +fi + +name="php-apache-test-$$" +docroot=/var/www/html/docroot + +cleanup() { + local status=$? + if [[ "${status}" -ne 0 ]]; then + docker logs "${name}" 2>&1 | tail -50 || true + fi + docker rm -f "${name}" "${name}-crash" >/dev/null 2>&1 || true + exit "${status}" +} +trap cleanup EXIT + +start() { + docker run -d --name "$1" \ + -e APACHE_DOCUMENT_ROOT="${docroot}" \ + -v "${PWD}/apache/docroot:${docroot}:ro" \ + "${IMAGE}" >/dev/null + docker exec "$1" make check-ready max_try=30 -f /usr/local/bin/actions.mk +} + +# Requests are made from inside the container, to its own Apache. +request() { + docker exec "${name}" curl -sS "$@" +} + +status() { + request -o /dev/null -w '%{http_code}' "http://localhost$1" +} + +expect() { + local description=$1 actual=$2 expected=$3 + echo -n "${description}... " + if [[ "${actual}" != *"${expected}"* ]]; then + echo "Error: expected ${expected}, got: ${actual}" + exit 1 + fi + echo "OK" +} + +echo -n "Running a command instead of the servers... " +docker run --rm --network none "${IMAGE}" php -r 'echo "cli\n";' | grep -qx cli +echo "OK" + +start "${name}" + +expect "Checking the Apache configuration" "$(docker exec "${name}" sudo httpd -t 2>&1)" "Syntax OK" +expect "Checking Apache's user" "$(docker exec "${name}" ps -o user,args | grep 'httpd -DFOREGROUND' | grep -v '^root')" "www-data" +expect "Running PHP through PHP-FPM" "$(request http://localhost/)" "\"script\":\"${docroot}/index.php\"" +expect "Serving a static file" "$(request -D - http://localhost/static.txt)" "Static file served by Apache." +expect "Serving a static file without PHP" "$(request -D - http://localhost/static.txt | grep -ci 'X-Powered-By' || true)" "0" +expect "Applying .htaccess rewrites" "$(request http://localhost/pretty/abc)" '"route":"abc"' +expect "Detecting HTTPS behind a proxy" "$(request -H 'X-Forwarded-Proto: https' http://localhost/)" '"https":"on"' +expect "Keeping plain HTTP" "$(request http://localhost/)" '"https":""' +expect "Passing the Authorization header" "$(request -H 'Authorization: Bearer token' http://localhost/)" '"authorization":"Bearer token"' +expect "Denying hidden files" "$(status /.env)" "403" +expect "Answering for a missing script" "$(status /missing.php)" "404" +expect "Answering the health check" "$(status /.healthz)" "204" + +echo -n "Stopping gracefully... " +# A request that is still running when the stop arrives must finish. +slow_response=$(mktemp) +request -w ' %{http_code}' http://localhost/slow.php > "${slow_response}" 2>&1 & +slow=$! +sleep 1 +started=$(date +%s) +docker stop -t 30 "${name}" >/dev/null +elapsed=$(($(date +%s) - started)) +wait "${slow}" || true +exit_code=$(docker inspect -f '{{.State.ExitCode}}' "${name}") +if [[ "${exit_code}" -ne 0 || "${elapsed}" -gt 20 ]]; then + echo "Error: exit code ${exit_code} after ${elapsed}s" + exit 1 +fi +if [[ "$(cat "${slow_response}")" != "finished"*" 200" ]]; then + echo "Error: the running request did not finish: $(cat "${slow_response}")" + exit 1 +fi +rm -f "${slow_response}" +echo "OK" + +echo -n "Stopping the container when PHP-FPM exits... " +start "${name}-crash" >/dev/null +docker exec -u 0 "${name}-crash" pkill -KILL -f 'php-fpm: master' +for _ in $(seq 1 20); do + if [[ "$(docker inspect -f '{{.State.Running}}' "${name}-crash")" == "false" ]]; then + break + fi + sleep 1 +done +if [[ "$(docker inspect -f '{{.State.Running}}' "${name}-crash")" != "false" ]]; then + echo "Error: the container kept running" + exit 1 +fi +echo "OK" diff --git a/8/tests/apache/docroot/.env b/8/tests/apache/docroot/.env new file mode 100644 index 00000000..11114c21 --- /dev/null +++ b/8/tests/apache/docroot/.env @@ -0,0 +1 @@ +SECRET=never-served diff --git a/8/tests/apache/docroot/.htaccess b/8/tests/apache/docroot/.htaccess new file mode 100644 index 00000000..bd3d4849 --- /dev/null +++ b/8/tests/apache/docroot/.htaccess @@ -0,0 +1,2 @@ +RewriteEngine On +RewriteRule ^pretty/(.*)$ index.php?route=$1 [L,QSA] diff --git a/8/tests/apache/docroot/index.php b/8/tests/apache/docroot/index.php new file mode 100644 index 00000000..7e14647d --- /dev/null +++ b/8/tests/apache/docroot/index.php @@ -0,0 +1,9 @@ + $_SERVER['SCRIPT_FILENAME'] ?? '', + 'route' => $_GET['route'] ?? '', + 'https' => $_SERVER['HTTPS'] ?? '', + 'authorization' => $_SERVER['HTTP_AUTHORIZATION'] ?? '', +], JSON_UNESCAPED_SLASHES), "\n"; diff --git a/8/tests/apache/docroot/slow.php b/8/tests/apache/docroot/slow.php new file mode 100644 index 00000000..61115e9d --- /dev/null +++ b/8/tests/apache/docroot/slow.php @@ -0,0 +1,4 @@ +