Compile From Sources

This document explains how to create a FrankenPHP binary that will load PHP as a dynamic library. This is the recommended method.

Alternatively, static builds can also be created.

Install PHP

FrankenPHP is compatible with PHP 8.2 and superior.

First, get the PHP sources and extract them:

  1. tar xf php-*
  2. cd php-*/

Then, run the configure script with the options needed for your platform. The following ./configure flags are mandatory, but you can add others, for example, to compile extensions or additional features.

Linux

  1. ./configure \
  2. --enable-embed \
  3. --enable-zts \
  4. --disable-zend-signals \
  5. --enable-zend-max-execution-timers

Mac

Use the Homebrew package manager to install libiconv, bison, re2c and pkg-config:

  1. brew install libiconv bison brotli re2c pkg-config
  2. echo 'export PATH="/opt/homebrew/opt/bison/bin:$PATH"' >> ~/.zshrc

Then run the configure script:

  1. ./configure \
  2. --enable-embed=static \
  3. --enable-zts \
  4. --disable-zend-signals \
  5. --disable-opcache-jit \
  6. --enable-static \
  7. --enable-shared=no \
  8. --with-iconv=/opt/homebrew/opt/libiconv/

Compile PHP

Finally, compile and install PHP:

  1. make -j"$(getconf _NPROCESSORS_ONLN)"
  2. sudo make install

Install Optional Dependencies

Some FrankenPHP features depend on optional system dependencies that must be installed. Alternatively, these features can be disabled by passing build tags to the Go compiler.

FeatureDependencyBuild tag to disable it
Brotli compressionBrotlinobrotli
Restart workers on file changeWatcher Cnowatcher

Compile the Go App

You can now build the final binary:

  1. curl -L https://github.com/dunglas/frankenphp/archive/refs/heads/main.tar.gz | tar xz
  2. cd frankenphp-main/caddy/frankenphp
  3. CGO_CFLAGS=$(php-config --includes) CGO_LDFLAGS="$(php-config --ldflags) $(php-config --libs)" go build -tags=nobadger,nomysql,nopgx

Using xcaddy

Alternatively, use xcaddy to compile FrankenPHP with custom Caddy modules:

  1. CGO_ENABLED=1 \
  2. XCADDY_GO_BUILD_FLAGS="-ldflags='-w -s' -tags=nobadger,nomysql,nopgx" \
  3. CGO_CFLAGS=$(php-config --includes) \
  4. CGO_LDFLAGS="$(php-config --ldflags) $(php-config --libs)" \
  5. xcaddy build \
  6. --output frankenphp \
  7. --with github.com/dunglas/frankenphp/caddy \
  8. --with github.com/dunglas/mercure/caddy \
  9. --with github.com/dunglas/vulcain/caddy
  10. # Add extra Caddy modules here

Compile from sources - 图1Tip

If you’re using musl libc (the default on Alpine Linux) and Symfony, you may need to increase the default stack size. Otherwise, you may get errors like PHP Fatal error: Maximum call stack size of 83360 bytes reached during compilation. Try splitting expression

To do so, change the XCADDY_GO_BUILD_FLAGS environment variable to something like XCADDY_GO_BUILD_FLAGS=$'-ldflags "-w -s -extldflags \'-Wl,-z,stack-size=0x80000\'"' (change the stack size value according to your app needs).