← Back to blog

Pack your PHP app into a single binary with rapira pack

Pack your PHP app into a single binary with rapira pack Pack your PHP app into a single binary with rapira pack

Deploying a PHP application usually means shipping thousands of files in vendor/, keeping paths aligned between build and runtime, and guarding the code tree against accidental writes. In Rapira 0.10, rapira pack embeds your application directory, rapira.toml, and optional php.ini inside a copy of the rapira executable. Run the output binary with no arguments, and Rapira serves the application from an in-memory /app virtual filesystem (VFS) without extracting files to disk.

rapira pack requires Rapira 0.10 or later and targets 64-bit ELF (Linux) and 64-bit Mach-O (macOS). In-memory /app VFS requires PHP 8.5 or later; on PHP 8.4, run the same binary with --extract-dir or RAPIRA_EXTRACT_DIR.

How it works #

Pack an application directory with one command, then run the resulting executable directly:

bash
rapira pack . -o dist/my-app --php-ini php.ini
./dist/my-app

rapira pack archives regular files, directories, permissions, modification times, and relative symbolic links inside the application root with Zstd compression and a SHA-256 payload digest. On macOS, it embeds the bundle inside __LINKEDIT and writes an ad-hoc SHA-256 signature so the binary runs without an extra signing step.

At startup, Rapira verifies the payload and mounts the bundle in memory at /app:

  • PHP SAPI VFS: PHP reads /app through hooked file streams, include and require, stat, directory iterators, glob(), SplFileInfo, and open_basedir. Paths outside /app and other stream schemes (php://, phar://, http://) pass through to the host filesystem unchanged.
  • Read-only code tree: Every write, metadata change, or link operation inside /app fails with EROFS, and is_writable() returns false.
  • Direct memory serving: When http.static.root or http.sendfile.root points under /app, Rapira streams immutable bytes directly from VFS memory without disk syscalls.
  • OPcache ready: VFS mode sets opcache.validate_timestamps = 0 globally and mixes the bundle manifest hash into zend_system_id so opcache.file_cache entries never collide across releases.

If an environment needs files on disk, pass --extract-dir <DIR> or set RAPIRA_EXTRACT_DIR. Rapira extracts atomically into a hash-named directory and reuses it on later starts.

Benchmark numbers #

We already used packed /app VFS binaries in our Laravel Rust vs PHP Chirper benchmark (1 vCPU, MySQL 8.4, gzip enabled) and in our VFS microbenchmarks on PHP 8.6.1-dev ZTS.

WorkloadMeasurementContext
rapira-symfony packed binary / image14.94 MiB / 192 MBSymfony 8.2 + Doctrine; 622 MB on Swoole (-69% image size)
rapira-symfony ceiling (1 vCPU)2,912.5 RPS (0.00% fail)0.807 ms p50 / 1.714 ms p99 at 2,000 RPS; 93.85 MiB peak RAM
rapira-laravel packed binary / image18.09 MiB / 196 MBLaravel 13.42 + Octane; 691 MB on FrankenPHP (-72% image size)
rapira-laravel peak RAM (ramp2k)126.93 MiB-49% vs FrankenPHP (249.10 MiB) on the same Laravel code
static-4k (/app VFS vs cached disk)866,097 req/s+12.8% vs optimized disk cache (767,597 req/s), 6.96 us/req IO CPU
PHP VFS reads (read / stat / glob)603k / 464k / 534k req/sZero disk I/O inside the /app VFS
Startup latency (7-run median)39.64 msIn-memory VFS (66.72 ms cold --extract-dir, 17.07 ms warm)

Compression ratios are practical on full framework trees too: a Symfony 8.1 application with 2,763 files (10.4 MB raw) compressed to 2.14 MB, and a 74 MB Laravel 13.44 tree compressed to 14.3 MB.

Use cases #

  • Minimal Distroless containers: Run Composer, warm framework caches, and call rapira pack in a builder stage, then copy one executable into ghcr.io/rapira-rs/rapira:php8.6-distroless. The final image has no /app directory and runs with --read-only --tmpfs /tmp:rw,mode=1777.
  • Single-artifact VM and edge deploys: Distribute one signed binary with a verified SHA-256 bundle instead of syncing thousands of PHP files and symlinks across hosts.
  • Immutable production code: Because /app rejects all writes with EROFS, a compromised request cannot alter PHP source files on disk while logs, sessions, caches, and uploads stay isolated in /tmp or external stores.
  • Self-contained internal services: Bundle a Symfony, Laravel, or plain PHP API together with its rapira.toml and php.ini so any operator starts it with ./my-app.

Why use rapira pack #

  • Smaller artifacts and faster pulls: Zstd compresses PHP and vendor trees by roughly 5:1, and Distroless images drop shell tools and unpacked source layers.
  • Zero disk I/O for application reads: Static assets, Exchange::sendFile(), and PHP script reads come directly from immutable memory.
  • Isolated configuration: Embedded rapira.toml and --php-ini travel with the binary while PHPRC still lets operators override PHP settings at runtime.
  • Clean operational boundaries: Code stays read-only inside /app, while writable paths (http.uploads.dir, server.pidfile, /tmp, external caches) remain explicit.

Note that rapira pack embeds your application into the base rapira binary and does not bundle external shared libraries; when the base binary links dynamically to libphp.so, run it alongside the matching libphp.so or inside the matching ghcr.io/rapira-rs/rapira container image.

See rapira-rs/rapira#351 for the implementation details, or read the Laravel Rust vs PHP benchmark for the full packed-binary test setup.