Files
ezServer-MacOS/server/run.sh
T

78 lines
2.4 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);
}
}
'
"$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