Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active July 2, 2025 14:44
Show Gist options
  • Save masakielastic/95681199b2663f0aee9ad4ff7ae46b5e to your computer and use it in GitHub Desktop.
Save masakielastic/95681199b2663f0aee9ad4ff7ae46b5e to your computer and use it in GitHub Desktop.
nghttpx (HTTP/2) + PHP ビルトインサーバーの起動スクリプト

nghttpx (HTTP/2) + PHP ビルトインサーバーの起動スクリプト

動作確認

  • Debian 12
export PATH=$PATH:/usr/sbin

使い方

chmod +x proxy_with_php.sh
./proxy_with_php.sh
Starting PHP built-in server...
Starting nghttpx proxy...
2025-07-02T23:13:22.246+09:00 8942 8942 feb0309c NOTICE (shrpx.cc:3519) Loading configuration from /dev/null
2025-07-02T23:13:22.247+09:00 8942 8942 feb0309c NOTICE (shrpx.cc:1012) Listening on 0.0.0.0:8443, tls
2025-07-02T23:13:22.247+09:00 8942 8942 feb0309c NOTICE (shrpx.cc:1662) Worker process [8943] spawned
2025-07-02T23:13:22.247+09:00 8942 8942 feb0309c NOTICE (shrpx_worker_process.cc:227) Renew new ticket keys
[Wed Jul  2 23:13:22 2025] PHP 8.4.7 Development Server (http://127.0.0.1:8000) started
2025-07-02T23:13:22.324+09:00 8942 8942 feb0309c NOTICE (shrpx_connection_handler.cc:414) [LISTEN:0x7f1b92a8f000] Created worker thread #0
fetch-ocsp-response (using OpenSSL 3.0.16 11 Feb 2025 (Library: OpenSSL 3.0.16 11 Feb 2025))
failed to extract ocsp URI from ./cert.pem
2025-07-02T23:13:22.405+09:00 8942 8942 feb0309c WARN (shrpx_connection_handler.cc:718) ocsp query command for ./cert.pem failed: error=0, rstatus=0xff00, status=255
[Wed Jul  2 23:13:26 2025] 127.0.0.1:34436 Accepted
[Wed Jul  2 23:13:26 2025] 127.0.0.1:34436 [200]: GET /
[Wed Jul  2 23:13:26 2025] 127.0.0.1:34436 Closing
^C
Stopping servers...
nghttpx stopped.
PHP server stopped.
#!/bin/bash
# 設定
BACKEND_PORT=8000
PROXY_PORT=8443
CERT_FILE="./cert.pem"
KEY_FILE="./key.pem"
DOCROOT="public"
generate_cert_if_needed() {
if [ ! -f "$CERT_FILE" ] || [ ! -f "$KEY_FILE" ]; then
echo "Generating self-signed certificate..."
openssl req -x509 -newkey rsa:2048 -nodes \
-keyout "$KEY_FILE" -out "$CERT_FILE" -days 365 \
-subj "/C=JP/ST=Tokyo/L=Tokyo/O=LocalDev/CN=localhost"
echo "Certificate and key generated."
fi
}
cleanup() {
echo
echo "Stopping servers..."
if [ -n "$NGHTTPX_PID" ] && kill -0 "$NGHTTPX_PID" 2>/dev/null; then
kill "$NGHTTPX_PID"
echo "nghttpx stopped."
fi
if [ -n "$PHP_PID" ] && kill -0 "$PHP_PID" 2>/dev/null; then
kill "$PHP_PID"
echo "PHP server stopped."
fi
exit 0
}
trap cleanup SIGINT SIGTERM
generate_cert_if_needed
echo "Starting PHP built-in server..."
php -S 127.0.0.1:$BACKEND_PORT -t "$DOCROOT" &
PHP_PID=$!
echo "Starting nghttpx proxy..."
nghttpx --conf=/dev/null \
-f"0.0.0.0,$PROXY_PORT;tls" \
-b127.0.0.1,$BACKEND_PORT \
"$KEY_FILE" "$CERT_FILE" &
NGHTTPX_PID=$!
# プロセスを待機
wait $PHP_PID
wait $NGHTTPX_PID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment