From c8bada0e15dc0b96964f42c7832ce32366e8ad3e Mon Sep 17 00:00:00 2001 From: Matthew Horwood Date: Wed, 27 Nov 2024 21:23:41 +0000 Subject: [PATCH] Nginx unit build feat: use nginx unit as the web server --- Dockerfile | 14 ++++++------- docker-compose.yml | 2 +- setup/start.sh | 51 +++++++++++++++++++++++++++++++++++++++++----- setup/unit.json | 34 +++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 14 deletions(-) create mode 100644 setup/unit.json diff --git a/Dockerfile b/Dockerfile index c513377..96f5a87 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,9 @@ FROM alpine:3.20 -MAINTAINER Matthew Horwood +LABEL org.opencontainers.image.authors="matt@horwood.biz" # Install required deb packages RUN apk update && \ - apk add gnupg nginx php82-fpm php82-common php82-iconv php82-json php82-gd \ + apk add gnupg unit-php82 php82-common php82-iconv php82-json php82-gd \ php82-curl php82-xml php82-mysqli php82-imap php82-pdo php82-pdo_mysql \ php82-soap php82-posix php82-gettext php82-ldap \ php82-ctype php82-dom php82-session php82-mbstring curl \ @@ -14,8 +14,8 @@ RUN apk update && \ ln -s /usr/bin/php82 /usr/bin/php; # Calculate download URL -ENV VERSION 5.2.1 -ENV URL https://files.phpmyadmin.net/phpMyAdmin/${VERSION}/phpMyAdmin-${VERSION}-all-languages.tar.xz +ENV VERSION=5.2.1 +ENV URL=https://files.phpmyadmin.net/phpMyAdmin/${VERSION}/phpMyAdmin-${VERSION}-all-languages.tar.xz LABEL version=$VERSION # Download tarball, verify it using gpg and extract @@ -47,14 +47,12 @@ RUN set -ex; \ cp -R /usr/src/phpmyadmin/* /var/www/html/; \ cp /config/config.inc.php /etc/phpmyadmin/config.inc.php && \ cp /config/php.ini /etc/php82/php.ini && \ - cp /config/php_fpm_site.conf /etc/php82/php-fpm.d/www.conf; \ - chown -R nobody:nginx /var/www/html /sessions; \ - cp /config/nginx_site.conf /etc/nginx/http.d/default.conf; \ + chown -R unit:unit /var/www/html /sessions; \ cp /config/healthcheck.php /var/www/html/; EXPOSE 80 ENTRYPOINT ["/config/start.sh"] -CMD ["nginx", "-g", "daemon off;"] +CMD ["unitd", "--no-daemon", "--control", "unix:/var/run/control.unit.sock"] ## Health Check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s \ diff --git a/docker-compose.yml b/docker-compose.yml index 03ffeff..18c2856 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '3.5' +--- # docker stack deploy --compose-file stack-phpmyadmin.yml phpmyadmin diff --git a/setup/start.sh b/setup/start.sh index 2a82ebc..afcae6a 100755 --- a/setup/start.sh +++ b/setup/start.sh @@ -1,9 +1,5 @@ #!/bin/sh -ln -s /dev/stdout /var/log/php82/error.log -ln -s /dev/stdout /var/log/nginx/access.log -ln -s /dev/stdout /var/log/nginx/error.log - if [ ! -f /etc/phpmyadmin/config.secret.inc.php ]; then cat > /etc/phpmyadmin/config.secret.inc.php </dev/null | /bin/grep -q .; then + echo "$0: /var/lib/unit/ is not empty, skipping initial configuration..." + else + echo "$0: Launching Unit daemon to perform initial configuration..." + /usr/sbin/$1 --control unix:/var/run/control.unit.sock + + for i in $(/usr/bin/seq $WAITLOOPS); do + if [ ! -S /var/run/control.unit.sock ]; then + echo "$0: Waiting for control socket to be created..." + /bin/sleep $SLEEPSEC + else + break + fi + done + # even when the control socket exists, it does not mean unit has finished initialisation + # this curl call will get a reply once unit is fully launched + /usr/bin/curl -s -X GET --unix-socket /var/run/control.unit.sock http://localhost/ + + curl -X PUT --data-binary @/config/unit.json --unix-socket \ + /var/run/control.unit.sock http://localhost/config/ + + echo "$0: Stopping Unit daemon after initial configuration..." + kill -TERM $(/bin/cat /var/run/unit.pid) + + for i in $(/usr/bin/seq $WAITLOOPS); do + if [ -S /var/run/control.unit.sock ]; then + echo "$0: Waiting for control socket to be removed..." + /bin/sleep $SLEEPSEC + else + break + fi + done + if [ -S /var/run/control.unit.sock ]; then + kill -KILL $(/bin/cat /var/run/unit.pid) + rm -f /var/run/control.unit.sock + fi + + echo + echo "$0: Unit initial configuration complete; ready for start up..." + echo + fi +fi exec "$@" diff --git a/setup/unit.json b/setup/unit.json new file mode 100644 index 0000000..770025d --- /dev/null +++ b/setup/unit.json @@ -0,0 +1,34 @@ +{ + "listeners": { + "*:80": { + "pass": "routes" + } + }, + + "routes": [ + { + "match": { + "uri": "~\\.(css|gif|html?|ico|jpg|js(on)?|png|svg|ttf|woff2?)$" + }, + + "action": { + "share": "/var/www/html$uri" + } + }, + { + "action": { + "pass": "applications/phpmyadmin" + } + } + ], + + "applications": { + "phpmyadmin": { + "type": "php", + "root": "/var/www/html/" + } + }, + "access_log": { + "path": "/var/log/unit/access.log" + } +}