Run original EzWebServer through nginx and PHP-FPM
This commit is contained in:
1 parent
74e988499e
commit
f3dc89c011
5 files changed
+99
-12
No files matched your search
+51
-9
@@ -5,31 +5,73 @@ 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
|
||||
if ! command -v "$php" >/dev/null; then
|
||||
printf '%s\n' 'PHP CLI is required. Install PHP or set EZSERVER_PHP to its executable.' >&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"] as $extension) {
|
||||
foreach (["intl", "mbstring", "session", "pdo_sqlite"] as $extension) {
|
||||
if (!extension_loaded($extension)) {
|
||||
fwrite(STDERR, "Missing PHP extension: " . $extension . PHP_EOL);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
'
|
||||
export EZSERVER_RUNTIME="$(cd "$runtime" && pwd)"
|
||||
mkdir -p "$EZSERVER_RUNTIME/empty"
|
||||
exec "$php" -d display_errors=0 -d expose_php=0 \
|
||||
-S "127.0.0.1:$port" -t "$EZSERVER_RUNTIME/empty" "$server_dir/router.php"
|
||||
"$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
|
||||
Reference in new issue
Block a user