Files
ezServer-MacOS/server/run.sh
T

104 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
umask 077
server_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
project_dir="$(dirname "$server_dir")"
runtime="${EZSERVER_RUNTIME:-$project_dir/.build/ezserver-macos/runtime}"
nginx="${EZSERVER_NGINX:-nginx}"
php="${EZSERVER_PHP:-php}"
fpm="${EZSERVER_PHP_FPM:-php-fpm}"
port="${EZSERVER_PORT:-55005}"
if [[ ! -f "$runtime/www/ezwebserver/src/public/api/index.php" ]]; then
printf '%s\n' 'Prepare the EzServer runtime with server/prepare.sh first.' >&2
exit 1
fi
for executable in "$nginx" "$php" "$fpm"; do
if ! command -v "$executable" >/dev/null; then
printf 'Missing executable: %s. Configure EZSERVER_NGINX, EZSERVER_PHP or EZSERVER_PHP_FPM.\n' "$executable" >&2
exit 1
fi
done
if ! [[ "$port" =~ ^[1-9][0-9]{0,4}$ ]] || (( port > 65535 )); then
printf '%s\n' 'EZSERVER_PORT must be between 1 and 65535.' >&2
exit 1
fi
export EZSERVER_RUNTIME="$(cd "$runtime" && pwd)"
stage="$(mktemp -d /tmp/ezserver.XXXXXX)"
nginx_pid=''
fpm_pid=''
cleanup() {
if [[ -n "$nginx_pid" ]]; then
kill -QUIT "$nginx_pid" 2>/dev/null || true
wait "$nginx_pid" 2>/dev/null || true
fi
if [[ -n "$fpm_pid" ]]; then
kill -TERM "$fpm_pid" 2>/dev/null || true
wait "$fpm_pid" 2>/dev/null || true
fi
rm -rf "$stage"
}
trap cleanup EXIT
trap 'exit 0' INT TERM
cp "$server_dir/router.php" "$stage/router.php"
sed -e "s|@PORT@|$port|g" -e "s|@STAGE@|$stage|g" "$server_dir/nginx.conf.in" > "$stage/nginx.conf"
sed "s|@STAGE@|$stage|g" "$server_dir/php-fpm.conf.in" > "$stage/php-fpm.conf"
"$php" -r '
foreach (["intl", "mbstring", "session", "pdo_sqlite"] as $extension) {
if (!extension_loaded($extension)) {
fwrite(STDERR, "Missing PHP extension: " . $extension . PHP_EOL);
exit(1);
}
}
'
if [[ -n "${EZSERVER_SQLITE:-}" ]]; then
if [[ "$EZSERVER_SQLITE" != /* || "${EZSERVER_JWT_KEY_FILE:-}" != /* ||
! -f "$EZSERVER_JWT_KEY_FILE" || ! -r "$EZSERVER_JWT_KEY_FILE" ]]; then
printf '%s\n' 'Set EZSERVER_SQLITE and EZSERVER_JWT_KEY_FILE to absolute paths.' >&2
exit 1
fi
cp "$EZSERVER_JWT_KEY_FILE" "$stage/jwt-key"
chmod 600 "$stage/jwt-key"
export EZSERVER_JWT_KEY_FILE="$stage/jwt-key"
"$php" -r '
if (strlen(trim(file_get_contents($argv[1]))) < 32) {
fwrite(STDERR, "The JWT signing key must contain at least 32 bytes." . PHP_EOL);
exit(1);
}
' "$EZSERVER_JWT_KEY_FILE"
"$php" "$server_dir/init-sqlite.php" "$EZSERVER_SQLITE"
cp -R "$EZSERVER_RUNTIME" "$stage/runtime"
export EZSERVER_RUNTIME="$stage/runtime"
config="$EZSERVER_RUNTIME/www/ezwebserver/src/application/config/production"
mkdir -p "$config"
cp "$server_dir/sqlite-database.php" "$config/database.php"
cp "$server_dir/sqlite-rest.php" "$config/rest.php"
fi
if [[ -f "$EZSERVER_RUNTIME/webconsole/index.html" ]]; then
ln -s "$EZSERVER_RUNTIME/webconsole" "$stage/webconsole"
fi
"$nginx" -e stderr -p "$stage/" -c nginx.conf -t
"$fpm" -F -y "$stage/php-fpm.conf" &
fpm_pid=$!
for (( attempt = 0; attempt < 100; attempt++ )); do
[[ -S "$stage/php.sock" ]] && break
kill -0 "$fpm_pid" 2>/dev/null || break
sleep 0.1
done
if [[ ! -S "$stage/php.sock" ]]; then
printf '%s\n' 'PHP-FPM did not open its FastCGI socket.' >&2
exit 1
fi
"$nginx" -e stderr -p "$stage/" -c nginx.conf &
nginx_pid=$!
while kill -0 "$nginx_pid" 2>/dev/null && kill -0 "$fpm_pid" 2>/dev/null; do
sleep 0.2
done
printf '%s\n' 'An EzServer HTTP process exited.' >&2
exit 1