Khởi chạy HTTP/3

Cài đặt backend QUIC, khởi động một listener UDP cục bộ và xác minh HTTP/3 bằng curl.

Cài đặt gói

Sử dụng PHP CLI 8.3 hoặc một phiên bản 8.x mới hơn trên một hệ thống Unix được hỗ trợ. Cài đặt gói trong thư mục ứng dụng của bạn. Composer kiểm tra các tiện ích mở rộng pcntl, posix và sockets cần thiết.

apt · Debian / Ubuntu
Shell
sudo apt update
sudo apt install php-cli php-common composer curl
Shell
sudo apt install build-essential pkg-config libngtcp2-dev \
  libngtcp2-crypto-gnutls-dev libnghttp3-dev libgnutls28-dev
apk · Alpine 3.24 (community)
Shell
apk add php85 php85-pcntl php85-posix php85-sockets composer curl
Shell
apk add php85-ffi build-base pkgconf ngtcp2-dev nghttp3-dev gnutls-dev
pacman · Arch Linux
Shell
sudo pacman -Syu --needed php composer curl
php --ini

Bật sockets trong php.ini của CLI. Với HTTP/2 và HTTP/3, bật thêm ffi.

Shell
sudo pacman -S --needed base-devel libngtcp2 libnghttp3 gnutls
dnf · Fedora
Shell
sudo dnf install php-cli php-process composer curl
Shell
sudo dnf install php-ffi gcc make pkgconf-pkg-config ngtcp2-devel \
  ngtcp2-crypto-gnutls-devel libnghttp3-devel gnutls-devel
brew · macOS
Shell
brew install php composer curl
Shell
brew install cloudflare-quiche

Trong terminal kiểm thử, hãy dùng curl của Homebrew.

Shell
export PATH="$(brew --prefix curl)/bin:$PATH"
Shell
php -v
composer require mini4work/webserver
composer check-platform-reqs
php --ri openssl
Shell
php --ri ffi
curl -V

Trên Linux, biên dịch adapter với ngtcp2 1.22+ và nghttp3 1.14+. Nếu kho gói thiếu phiên bản tương thích, xem hướng dẫn biên dịch cho từng nền tảng. Trên macOS, thư viện quiche từ Homebrew đã sẵn sàng sử dụng.

Shell
if [ "$(uname -s)" = Linux ]; then
  make -C vendor/mini4work/webserver/native/ngtcp2
fi

Khởi động máy chủ

Lưu file này dưới dạng m4w.json trong thư mục gốc của ứng dụng. Nó tạo các chứng chỉ phát triển trong var/tls. Client sẽ tin tưởng CA công khai một cách rõ ràng cho yêu cầu của nó.

JSON
{
  "listen": { "port": 9000 },
  "protocols": {
    "http": "3"
  },
  "tls": {
    "mode": "local",
    "storage": "var/tls",
    "local": {
      "trust_ca": "off"
    }
  }
}
Shell
php -d ffi.enable=true vendor/mini4work/webserver/examples/server.php m4w.json

Yêu cầu thử nghiệm

Chạy curl -V trên máy khách. Nó phải liệt kê HTTP3. Thay thế đường dẫn CA công khai từ tls.trust.root trong nhật ký khởi động.

curl · HTTP/3
Shell
CA=/path/from/tls.trust.root
curl --http3-only --cacert "$CA" -sS -o /dev/null \
  -w 'HTTP/%{http_version} %{http_code}\n' https://127.0.0.1:9000/

HTTP/3 200

HTTP/3Chọn chế độ TLS