On this page

Packing an application

rapira pack embeds a PHP application directory and its rapira.toml into a copy of the rapira executable. Run the packed executable with no arguments to serve the application from an in-memory /app virtual filesystem (VFS) without extracting files to disk.

Pass --extract-dir or set RAPIRA_EXTRACT_DIR when an environment runs PHP 8.4 or needs files on disk. Rapira then extracts the bundle once and runs from disk.

Create a packed executable #

Pack an application directory containing rapira.toml and your PHP files:

bash
rapira pack . -o dist/my-app

Run the packed executable with no arguments:

bash
./dist/my-app
OptionDefaultMeaning
APP_DIRrequiredDirectory containing the PHP application and rapira.toml.
-o, --output <PATH>requiredOutput executable path. Rapira writes the binary atomically with mode 0755.
--base <BINARY>running rapiraClean rapira executable to embed the bundle into.
--config <REL>rapira.tomlPath to rapira.toml relative to APP_DIR.
--php-ini <REL>unsetOptional php.ini path relative to APP_DIR.
--level <1..=22>3Zstd compression level for the embedded payload.

Rapira validates rapira.toml before writing and requires [http] or [grpc]. If you pass --php-ini, the file must exist as a regular file inside APP_DIR.

If --output lies inside APP_DIR, Rapira skips it automatically so it does not pack itself. Running ./my-app serve /path/to/rapira.toml ignores the embedded bundle and serves that external file. Rapira refuses to repack an already-packed executable. Pass --base with a clean rapira binary when running pack from a packed binary.

rapira pack embeds your application into the base binary and does not bundle shared libraries. Pass a static Linux release binary to --base for a single self-contained file with PHP embedded. If the base binary links dynamically to libphp.so, keep the matching libphp.so on the host or run inside the matching ghcr.io/rapira-rs/rapira container image.

Supported targets and bundle limits #

On Linux and macOS, rapira pack supports 64-bit ELF and 64-bit little-endian Mach-O executables.

On macOS, Rapira places the bundle inside __LINKEDIT before LC_CODE_SIGNATURE and writes an ad-hoc SHA-256 signature so the binary runs without an extra signing step. It keeps LC_SYMTAB aligned with the signature boundary so codesign --force preserves the bundle.

Bundle limits and checks:

  • Maximum manifest size: 64 MiB.
  • Maximum uncompressed payload: 1 GiB.
  • Integrity: Rapira verifies uncompressed length and SHA-256 digest when decoding.
  • Paths and metadata: All file paths and symlink targets must be valid UTF-8. Rapira preserves modification times and permission bits (0777).
  • Symbolic links: Rapira supports relative symbolic links that stay inside APP_DIR (maximum depth 40). Absolute links and links escaping APP_DIR fail during pack and decode.
  • Special files: Rapira rejects FIFOs, sockets, and device nodes.

In-memory /app VFS #

Running a packed executable without --extract-dir or RAPIRA_EXTRACT_DIR mounts the bundle in memory at /app and loads /app/<config>. Both /app/... and file:///app/... resolve inside the VFS.

In-memory VFS requires PHP 8.5 or newer. On PHP 8.4, running a packed executable without --extract-dir or RAPIRA_EXTRACT_DIR exits with code 1.

Paths outside /app and non-file schemes (php://, phar://, http://) pass through to PHP and the host filesystem unchanged.

Read-only filesystem #

The /app VFS is strictly read-only. stat strips write bits (mode & 0555), and is_writable(), is_writeable(), and SplFileInfo::isWritable() return false inside /app.

These operations fail with EROFS (Read-only file system) when targeting /app:

  • Opening files with write modes (w, a, x, c, +) or calling file_put_contents().
  • unlink(), mkdir(), rmdir(), and rename() with a source or destination inside /app.
  • touch(), chmod(), chown(), lchown(), chgrp(), and lchgrp().
  • symlink() with a link path inside /app (host symlinks pointing to /app/... work).
  • link() with a target or link inside /app.

Write runtime files such as logs, sessions, caches, and uploads to a writable host directory (/tmp) or external storage.

Working directory, include_path, and open_basedir #

Each worker starts in the VFS directory of its /app entry script. getcwd() returns the virtual /app path, and chdir() switches freely between /app and host directories.

Relative paths in include, require, include_once, require_once, and fopen() with use_include_path resolve against the VFS working directory without leaking . to the host working directory. Explicit host paths in include_path still resolve on the host.

PHP open_basedir applies to VFS file opens, stat, copy(), linkinfo(), directory reads, glob(), glob://, GlobIterator, realpath(), readlink(), chdir(), SplFileInfo, and SplFileObject.

Supported PHP APIs and subprocesses #

VFS hooks cover plain-file streams (fopen, file_get_contents, parse_ini_file, fseek, feof, shared flock with LOCK_SH/LOCK_UN), stat functions, copy, linkinfo, realpath, readlink, SplFileInfo, and SplFileObject. Directory iteration covers opendir, scandir, DirectoryIterator, FilesystemIterator, and RecursiveDirectoryIterator. Pattern matching supports glob(), glob://, and GlobIterator (GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ONLYDIR). Exclusive flock(..., LOCK_EX) fails with EROFS.

The /app VFS exists only inside Rapira and PHP. Subprocesses started with proc_open(), exec(), or native extensions see only the host filesystem and inherit the host process working directory.

Configuration, static files, and sendFile() #

Relative paths in an embedded rapira.toml resolve against /app/<config>'s directory. These keys read directly from /app:

  • http.pool.entrypoint and grpc.pool.entrypoint
  • http.static.root
  • http.sendfile.root
  • grpc.descriptor_set (file paths)
  • grpc.auth.tokens_file

These keys always use writable host paths:

  • http.uploads.dir (defaults to the host temp directory)
  • server.pidfile
  • unix: listener sockets in [http], [grpc], and [observability]

When http.static.root points under /app, the static file middleware serves immutable VFS bytes directly from memory. It avoids disk syscalls and bypasses the disk file cache limits (1-second TTL, 256 KiB per file, 16 MiB total).

In Dispatcher mode, Exchange::sendFile() resolves /app roots and relative paths against the VFS working directory and streams bytes from memory. VFS and host roots stay isolated: /app roots reject host paths and host roots reject /app paths.

php.ini and OPcache #

When PHPRC is unset or empty, a packed executable ignores host php.ini files and PHP_INI_SCAN_DIR:

  • With --php-ini <REL>, Rapira loads that embedded file (inline in VFS mode, or from disk in --extract-dir mode).
  • Without --php-ini, PHP uses built-in defaults plus Rapira's display_errors=0 and log_errors=1.

When you set a non-empty PHPRC, host PHPRC and PHP_INI_SCAN_DIR override the embedded php.ini.

In VFS mode, Rapira configures OPcache automatically before initialization:

  • Sets opcache.validate_timestamps = 0, opcache.validate_permission = 0, opcache.file_update_protection = 0, and opcache.revalidate_path = 1.
  • Installs all VFS hooks before opcache.preload runs, so preload scripts inside /app work.
  • Mixes the manifest SHA-256 into zend_system_id so opcache.file_cache entries never collide across bundle versions.

Multi-worker OPcache requires PHP 8.4.29, 8.5.14, 8.6.1, or newer (PHP 8.5 and 8.6 enable OPcache by default). Older patch releases (such as PHP 8.6.0) fail at startup unless total workers equal 1 or OPcache is disabled. Set opcache.enable = 0 via --php-ini or PHPRC.

Because the /app VFS is immutable, a reload (SIGUSR2 or SIGHUP) resets OPcache and recycles apps using the same bundle. Replace and restart the executable to deploy new code.

Extract to disk with --extract-dir #

Pass --extract-dir <DIR> or set RAPIRA_EXTRACT_DIR to run from disk instead of VFS:

bash
./my-app --extract-dir /var/cache/my-app
bash
RAPIRA_EXTRACT_DIR=/var/cache/my-app ./my-app

--extract-dir overrides RAPIRA_EXTRACT_DIR, and Rapira ignores an empty RAPIRA_EXTRACT_DIR.

Rapira extracts atomically into <DIR>/<first-16-hex-of-manifest-sha256>/. It reuses that directory if the bundle's config file (rapira.toml, or the --config path) is already present. New bundle versions extract to a new hash directory.

--extract-dir works on PHP 8.4, PHP 8.5, and PHP 8.6, and applies only when running a packed executable without a subcommand. Passing --extract-dir with serve, pack, or an unpacked binary exits with code 1.

Running a packed binary in a container #

Pack the application in a builder stage. Then copy only the packed binary into a Distroless image with no /app directory:

dockerfile
FROM ghcr.io/rapira-rs/rapira:php8.6 AS builder
WORKDIR /src
COPY . /src
# Install Composer dependencies and warm framework caches here.
RUN rapira pack /src -o /usr/local/bin/my-app

FROM ghcr.io/rapira-rs/rapira:php8.6-distroless
COPY --from=builder /usr/local/bin/my-app /usr/local/bin/my-app
ENTRYPOINT ["/usr/local/bin/my-app"]
CMD []

Run the container with --init, a read-only root filesystem, and a writable /tmp mount:

bash
docker run --init --read-only --tmpfs /tmp:rw,mode=1777 -p 8000:8000 my-app

For Laravel and Symfony, pre-warm configuration, route, and template caches before rapira pack. Send logs to stderr, and place runtime caches, sessions, and queues in /tmp or external stores.