Skip to content

Commit

Permalink
Nginx unit build
Browse files Browse the repository at this point in the history
feat: use nginx unit as the web server
  • Loading branch information
mhzawadi committed Nov 27, 2024
1 parent e204c6b commit c8bada0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
14 changes: 6 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM alpine:3.20
MAINTAINER Matthew Horwood <[email protected]>
LABEL org.opencontainers.image.authors="[email protected]"

# 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 \
Expand All @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.5'
---

# docker stack deploy --compose-file stack-phpmyadmin.yml phpmyadmin

Expand Down
51 changes: 46 additions & 5 deletions setup/start.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOT
<?php
Expand All @@ -15,6 +11,51 @@ if [ ! -f /etc/phpmyadmin/config.user.inc.php ]; then
touch /etc/phpmyadmin/config.user.inc.php
fi

php-fpm82
ln -s /dev/stdout /var/log/unit.log
ln -s /dev/stdout /var/log/unit/access.log

if [ "$1" = "unitd" ] || [ "$1" = "unitd-debug" ]; then
if /usr/bin/find "/var/lib/unit/" -mindepth 1 -print -quit 2>/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 "$@"
34 changes: 34 additions & 0 deletions setup/unit.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit c8bada0

Please sign in to comment.