Skip to content

Commit

Permalink
Merge pull request #219 from Automattic/refactor-nginx
Browse files Browse the repository at this point in the history
refactor(nginx): support Debian and Ubuntu
  • Loading branch information
sjinks authored Jun 1, 2024
2 parents 016369b + f19ab2c commit 1b2f52e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
1 change: 0 additions & 1 deletion features/src/nginx/conf-nginx.tpl
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
NGINX_USER="${NGINX_USER}"
MEDIA_REDIRECT_URL="${MEDIA_REDIRECT_URL}"
7 changes: 2 additions & 5 deletions features/src/nginx/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{
"id": "nginx",
"name": "nginx",
"version": "1.1.1",
"version": "1.2.0",
"description": "Installs nginx into the Dev Environment",
"options": {
"mediaRedirectURL": {
"type": "string",
"description": "The URL to redirect for missing media files",
"default": ""
}
},
"installsAfter": [
"ghcr.io/automattic/vip-codespaces/base"
]
}
}
77 changes: 62 additions & 15 deletions features/src/nginx/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,81 @@ fi

echo '(*) Installing nginx...'

if [ -z "${_REMOTE_USER}" ] || [ "${_REMOTE_USER}" = "root" ]; then
NGINX_USER=nginx
else
NGINX_USER="${_REMOTE_USER}"
fi
: "${MEDIAREDIRECTURL:=}"

MEDIA_REDIRECT_URL="${MEDIAREDIRECTURL:-}"
# shellcheck source=/dev/null
. /etc/os-release
: "${ID:=}"
: "${ID_LIKE:=${ID}}"
NGINX_USER=

apk add --no-cache nginx
case "${ID_LIKE}" in
"debian")
export DEBIAN_FRONTEND=noninteractive
PACKAGES="nginx"
if ! hash envsubst >/dev/null 2>&1; then
PACKAGES="${PACKAGES} gettext"
fi

install -D -m 0755 -o root -g root service-run /etc/sv/nginx/run
install -d -m 0755 -o root -g root /etc/service
ln -sf /etc/sv/nginx /etc/service/nginx
# /etc/nginx, /etc/nginx/conf.d, /etc/nginx/modules-available, /etc/nginx/modules-enabled, /etc/nginx/sites-available, /etc/nginx/sites-enabled, /etc/nginx/snippets
# nginx.conf:
# top-level: include /etc/nginx/modules-enabled/*.conf;
# http: include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*;
apt-get update
# shellcheck disable=SC2086
apt-get install -y --no-install-recommends ${PACKAGES}
apt-get clean
rm -rf /var/lib/apt/lists/*
update-rc.d -f nginx remove
rm -f /etc/nginx/sites-enabled/default /etc/nginx/sites-available/default
sed -i '/pid \/run\/nginx.pid;/d' /etc/nginx/nginx.conf
NGINX_USER=www-data
;;

"alpine")
PACKAGES="nginx"
if ! hash envsubst >/dev/null 2>&1; then
PACKAGES="${PACKAGES} gettext"
fi

# /etc/nginx, /etc/nginx/http.d, /etc/nginx/modules
# nginx.conf:
# top-level: include /etc/nginx/modules/*.conf; include /etc/nginx/conf.d/*.conf;
# http: include /etc/nginx/http.d/*.conf;
# shellcheck disable=SC2086
apk add --no-cache ${PACKAGES}
rm -f /etc/nginx/http.d/default.conf
install -d -D -m 0755 -o root -g root /etc/nginx/sites-enabled /etc/nginx/sites-available
sed -i '/include \/etc\/nginx\/http.d\/\*.conf;/a \\tinclude \/etc\/nginx\/sites-enabled\/*;' /etc/nginx/nginx.conf
NGINX_USER=nginx
;;

*)
echo "Unsupported distribution: ${ID_LIKE}"
exit 1
;;
esac

install -d -D -m 0755 -o root -g root /etc/nginx/http.d /etc/nginx/conf.extra
install -d -D -m 0755 -o root -g root /etc/nginx/conf.extra /etc/conf.d

export NGINX_USER MEDIA_REDIRECT_URL

install -D -d -m 0755 -o root -g root /etc/service /etc/sv/nginx
# shellcheck disable=SC2016
envsubst '$NGINX_USER' < service-run.tpl > /etc/sv/nginx/run && chmod 0755 /etc/sv/nginx/run
ln -sf /etc/sv/nginx /etc/service/nginx

# shellcheck disable=SC2016
envsubst '$NGINX_USER $MEDIA_REDIRECT_URL' < conf-nginx.tpl > /etc/conf.d/nginx
envsubst '$MEDIA_REDIRECT_URL' < conf-nginx.tpl > /etc/conf.d/nginx

if [ -n "${MEDIA_REDIRECT_URL}" ]; then
# shellcheck disable=SC2016
envsubst '$MEDIA_REDIRECT_URL' < media-redirect.tpl > /etc/nginx/conf.extra/media-redirect.conf
fi

install -m 0644 -o root -g root default.conf /etc/nginx/http.d/default.conf
install -m 0644 -o root -g root default.conf /etc/nginx/sites-available/default.conf
ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf

sed -i "s/user nginx;/user ${NGINX_USER};/" /etc/nginx/nginx.conf
chown -R "${NGINX_USER}:${NGINX_USER}" /run/nginx /var/log/nginx /var/lib/nginx
nginx -t

echo 'Done!'
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ set -eu

exec 2>&1

if [ -f /etc/conf.d/nginx ]; then
# shellcheck source=/dev/null
. /etc/conf.d/nginx
fi

: "${NGINX_USER:=nginx}"

PID_FILE=/run/nginx/nginx.pid

# shellcheck disable=SC2154
/usr/bin/install -d -o "${NGINX_USER}" -g "${NGINX_USER}" "${PID_FILE%/*}" /var/log/nginx
/usr/bin/install -d -o "${NGINX_USER}" -g "${NGINX_USER}" -m 0750 /var/lib/nginx
exec /usr/sbin/nginx -c /etc/nginx/nginx.conf -g "pid ${PID_FILE}; daemon off;"

0 comments on commit 1b2f52e

Please sign in to comment.