Static PHP Builds Via GitHub Actions


I recently came across the static-php-cli project. In addition to providing static PHP builds for x86_64 and arm64 for both Linux and MacOS that you can download, it also provides a GitHub Actions workflow that can be used to build custom static PHP binaries. I haven’t used GitHub Actions with a workflow before, so I wasn’t sure how much work was involved. Turns out their CI build guide wasn’t kidding when they said it would be easy.

The steps are:

  1. Fork the static-php-cli project
  2. Go to the Actions of the project and select CI on Unix ( for Linux and MacOS builds ).
  3. Select Run workflow, fill in the PHP version you want to compile, the target OS, and the list of extensions.
  4. Wait for the workflow to complete ( varies, mine have all been under 5 minutes ), then click on the workflow run and get the Artifacts.

That really is it. You can start from nothing and have your first custom build in a matter of minutes.

Static PHP CLI Workflow Options

This is a really slick way to do a build. Pick a few options, fill in a couple of blanks, then let GitHub Actions worry about spinning up a new VM, installing dependencies, and then building the PHP binary. Being able to use this for multiple versions of PHP across multiple architectures and operating systems with one interface takes this from good to great.

Workflow Artifacts

As one example, a build of PHP 8.4.5 for MacOS on aarch64 with the only extension added being apcu ( plus the defaults ), finished building in less than 3 minutes. The resulting binary was 4,619,920 bytes.

$ php -m
[PHP Modules]
apcu
Core
date
hash
json
pcre
random
Reflection
SPL
standard

[Zend Modules]

The documentation for static-php-cli is really well done. Check out the list of supported extensions, the library and extension dependency map, and the CLI Build Command Generator for those that want to use this tooling to run the build locally.

On top of all that, they also maintain a fork of phpmicro. phpmicro makes it possible to combine the PHP interpreter with a single PHP file or Phar file into a single executable. The specific tool for that is called micro.sfx.

All of this has me thinking about the viability of including a PHP binary in a Git repo for PHP projects. It would be easy to make sure that it always had all of the right extensions. If you do dev work locally on MacOS you could have multiple PHP binaries, one for dev and one for production. I’m not 100% on board with this idea yet, but wouldn’t be hard to do.