Skip to content

Commit

Permalink
Add initial "NGINX Unit" based image
Browse files Browse the repository at this point in the history
The Unit configuration I've included here is based on the example provided by the Unit documentation (https://unit.nginx.org/howto/wordpress/), although I did spend time understanding it before blindly trusting it. 😄

The biggest 😬 here (IMO) is that WordPress itself does not know about Unit yet, so the permalink handling is a bit off -- I've added a hack in `wp-config-docker.php` (specifically in the Unit images only) which works around it by teaching WordPress that Unit is effectively NGINX (which does work and is accurate enough for what WordPress needs), but that should probably be an issue or PR to the WordPress community instead.
  • Loading branch information
tianon committed Jan 12, 2024
1 parent ac65dab commit 3610d02
Show file tree
Hide file tree
Showing 32 changed files with 1,205 additions and 25 deletions.
32 changes: 31 additions & 1 deletion Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
def is_alpine:
env.variant | index("alpine")
-}}
{{ if env.variant == "unit" then ( -}}
FROM unit:{{ .unit.version }}-php{{ env.phpVersion }}
{{ ) else ( -}}
FROM php:{{ env.phpVersion }}-{{ env.variant }}
{{ ) end -}}

{{ if env.version != "cli" then ( -}}
# persistent dependencies
Expand Down Expand Up @@ -252,12 +256,38 @@ VOLUME /var/www/html

{{ if env.version != "cli" then ( -}}
COPY --chown=www-data:www-data wp-config-docker.php /usr/src/wordpress/
{{ if env.variant == "unit" then ( -}}

# NGINX Unit specific bits
WORKDIR /var/www/html
RUN set -eux; \
# save Unit's entrypoint so we can successfully/correctly configure Unit at runtime
sed -e 's/docker-entrypoint/unit-entrypoint/g' /usr/local/bin/docker-entrypoint.sh > /usr/local/bin/unit-entrypoint.sh; \
grep -F '/unit-entrypoint.d/' /usr/local/bin/unit-entrypoint.sh; \
chmod +x /usr/local/bin/unit-entrypoint.sh; \
\
{ \
echo; \
echo '// WordPress does not support NGINX Unit (yet), so we have to do a little hackery to let it know that Unit supports nice permalinks (and prevent it from insisting on injecting "/index.php" in all the permalink options) -- the simplest way is teaching it to recognize Unit as if it were NGINX (which is only semi-true, but true enough for these purposes)'; \
echo '$is_nginx = $is_nginx || str_starts_with($_SERVER["SERVER_SOFTWARE"], "Unit/");'; \
echo '// see also https://github.com/WordPress/WordPress/blob/39f7f558d91afdd2f3afc7f3b049a6a800cd3f80/wp-includes/vars.php#L127'; \
} >> /usr/src/wordpress/wp-config-docker.php
COPY unit.json /unit-entrypoint.d/wordpress.json

{{ ) else "" end -}}
{{ ) else "" end -}}
COPY docker-entrypoint.sh /usr/local/bin/

ENTRYPOINT ["docker-entrypoint.sh"]
{{ if env.version != "cli" then ( -}}
CMD {{ [ if env.variant == "apache" then "apache2-foreground" else "php-fpm" end ] | @json }}
CMD {{
{
apache: [ "apache2-foreground" ],
# https://github.com/nginx/unit/blob/fb33ec86a3b6ca6a844dfa6980bb9e083094abec/pkg/docker/Dockerfile.php8.2#L89
unit: [ "unitd", "--no-daemon", "--control", "unix:/var/run/control.unit.sock" ],
}[env.variant] // [ "php-fpm" ]
| @json
}}
{{ ) else ( -}}
USER www-data
CMD ["wp", "shell"]
Expand Down
8 changes: 8 additions & 0 deletions apply-templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ for version; do
for variant in "${variants[@]}"; do
export variant

if [ "$variant" = 'unit' ] && jq -e '.[env.version][env.variant].phpVersions | index(env.phpVersion) | not' versions.json > /dev/null; then
continue
fi

dir="$version/php$phpVersion/$variant"
mkdir -p "$dir"

Expand All @@ -58,6 +62,10 @@ for version; do
else
cp -a docker-entrypoint.sh wp-config-docker.php "$dir/"
fi

if [ "$variant" = 'unit' ]; then
cp -a unit.json "$dir/"
fi
done
done
done
10 changes: 9 additions & 1 deletion beta/php8.1/apache/docker-entrypoint.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion beta/php8.1/fpm-alpine/docker-entrypoint.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion beta/php8.1/fpm/docker-entrypoint.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion beta/php8.2/apache/docker-entrypoint.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion beta/php8.2/fpm-alpine/docker-entrypoint.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion beta/php8.2/fpm/docker-entrypoint.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3610d02

Please sign in to comment.