static-php-cli is a powerful tool designed for building static, standalone PHP runtime with popular extensions.
Static PHP built by static-php-cli supports cli
, fpm
, embed
and micro
SAPI.
static-php-cli also has the ability to package PHP projects along with the PHP interpreter into one single executable file.
static-php-cli (you can call it spc
) has a lot of features:
Linux
, macOS
, FreeBSD
, Windows
supportspc
own standalone executable (built by spc and box)Single-file standalone php-cli:
Combine PHP code with PHP interpreter using phpmicro:
The current README contains basic usage. For all the features of static-php-cli, see https://static-php.dev .
If you don't want to build or want to test first, you can download example pre-compiled artifact from Actions, or from self-hosted server.
Below are several precompiled static-php binaries with different extension combinations, which can be downloaded directly according to your needs.
common
contains about 30+ commonly used extensions, and the size is about 22MB.bulk
contains 50+ extensions and is about 70MB in size.minimal
contains 5 extensions and is about 6MB in size.mbstring,pcntl,posix,tokenizer,phar
curl
and git
installedYou can say I made a PHP builder written in PHP, pretty funny.
But static-php-cli runtime only requires an environment above PHP 8.1 and mbstring
, pcntl
extension.
Here is the supported OS and arch, where :octocat: represents support for GitHub Action builds, :computer: represents support for local manual builds, and blank represents not currently supported.
x86_64 | aarch64 | |
---|---|---|
macOS | :octocat: :computer: | :computer: |
Linux | :octocat: :computer: | :octocat: :computer: |
Windows | :computer: | |
FreeBSD | :computer: | :computer: |
Currently supported PHP versions for compilation are: 7.3
, 7.4
, 8.0
, 8.1
, 8.2
, 8.3
.
Please first select the extension you want to compile based on the extension list below.
If an extension you need is missing, you can submit an issue.
Here is the current planned roadmap for extension support: #152 .
Use GitHub Action to easily build a statically compiled PHP, and at the same time define the extensions to be compiled by yourself.
CI
.Run workflow
, fill in the PHP version you want to compile, the target type, and the list of extensions. (extensions comma separated, e.g. bcmath,curl,mbstring
)Artifacts
.If you enable debug
, all logs will be output at build time, including compiled logs, for troubleshooting.
This project provides a binary file of static-php-cli: spc
.
You can use spc
binary instead of installing any runtime like golang app.
Currently, the platforms supported by spc
binary are Linux and macOS.
Download from self-hosted nightly builds using commands below:
# Download from self-hosted nightly builds (sync with main branch)
# For Linux x86_64
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-x86_64
# For Linux aarch64
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-aarch64
# macOS x86_64 (Intel)
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-macos-x86_64
# macOS aarch64 (Apple)
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-macos-aarch64
# add x perm
chmod +x ./spc
./spc --version
Self-hosted spc
is built by GitHub Actions, you can also download from Actions artifacts here.
# just clone me!
git clone https://github.com/crazywhalecc/static-php-cli.git
If you have not installed php on your system, we recommend that you use the built-in setup-runtime to install PHP and Composer automatically.
cd static-php-cli
chmod +x bin/setup-runtime
# it will download static php (from self-hosted server) and composer (from getcomposer)
bin/setup-runtime
# initialize composer deps
bin/composer install
# chmod
chmod +x bin/spc
bin/spc --version
Basic usage for building php with some extensions:
If you are using the packaged
spc
binary, you need to replacebin/spc
with./spc
in the following commands.
# Check system tool dependencies, auto-fix them if possible
./bin/spc doctor --auto-fix
# fetch all libraries
./bin/spc download --all
# only fetch necessary sources by needed extensions (recommended)
./bin/spc download --for-extensions=openssl,pcntl,mbstring,pdo_sqlite
# download different PHP version (--with-php=x.y, recommend 7.3 ~ 8.3)
./bin/spc download --for-extensions=openssl,curl,mbstring --with-php=8.1
# with bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl extension, build both CLI and phpmicro SAPI
./bin/spc build bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl --build-cli --build-micro
# build thread-safe (ZTS) version (--enable-zts)
./bin/spc build curl,phar --enable-zts --build-cli
Now we support cli
, micro
, fpm
and embed
SAPI. You can use one or more of the following parameters to specify the compiled SAPI:
--build-cli
: build static cli executable--build-micro
: build static phpmicro self-extracted executable--build-fpm
: build static fpm binary--build-embed
: build embed (libphp)--build-all
: build allIf anything goes wrong, use --debug
option to display full terminal output:
./bin/spc build openssl,pcntl,mbstring --debug --build-all
./bin/spc download --all --debug
php-cli is a single static binary, you can use it like normal php installed on your system.
When using the parameter --build-cli
or --build-all
,
the final compilation result will output a binary file named ./php
,
which can be distributed and used directly.
This file will be located in the directory buildroot/bin/
, copy it out for use.
cd buildroot/bin/
./php -v # check version
./php -m # check extensions
./php your_code.php # run your php code
./php your_project.phar # run your phar (project archive)
phpmicro is a SelF-extracted eXecutable SAPI module, provided by phpmicro project. But this project is using a fork of phpmicro, because we need to add some features to it. It can put php runtime and your source code together.
When using the parameter --build-all
or --build-micro
,
the final compilation result will output a file named ./micro.sfx
,
which needs to be used with your PHP source code like code.php
.
This file will be located in the path buildroot/bin/micro.sfx
, simply copy it out for use.
Prepare your project source code, which can be a single PHP file or a Phar file, for use.
echo "<?php echo 'Hello world' . PHP_EOL;" > code.php
cat micro.sfx code.php > single-app && chmod +x single-app
./single-app
If you package a PHAR file, just replace code.php
with the phar file path.
You can use box-project/box to package your CLI project as Phar,
It is then combined with phpmicro to produce a standalone executable binary.
# Use the micro.sfx generated by static-php-cli to combine,
bin/spc micro:combine my-app.phar
# or you can directly use the cat command to combine them.
cat buildroot/bin/micro.sfx my-app.phar > my-app && chmod +x my-app
# Use micro:combine combination to inject INI options into the binary.
bin/spc micro:combine my-app.phar -I "memory_limit=4G" -I "disable_functions=system" --output my-app-2
In some cases, PHAR files may not run in a micro environment. Overall, micro is not production ready.
When using the parameter --build-all
or --build-fpm
,
the final compilation result will output a file named ./php-fpm
,
This file will be located in the path buildroot/bin/
, simply copy it out for use.
In common Linux distributions and macOS systems, the package manager will automatically generate a default fpm configuration file after installing php-fpm.
Because php-fpm must specify a configuration file before running, the php-fpm compiled by this project will not have any configuration files, so you need to write php-fpm.conf
and pool.conf
configuration files yourself.
Specifying php-fpm.conf
can use the command parameter -y
, for example: ./php-fpm -y php-fpm.conf
.
When using the project parameters --build-embed
or --build-all
,
the final compilation result will output a libphp.a
, php-config
and a series of header files,
stored in buildroot/
. You can introduce them in your other projects.
If you know embed SAPI, you should know how to use it.
You may require the introduction of other libraries during compilation,
you can use buildroot/bin/php-config
to obtain the compile-time configuration.
For an advanced example of how to use this feature, take a look at how to use it to build a static version of FrankenPHP.
If the extension you need is missing, you can create an issue. If you are familiar with this project, you are also welcome to initiate a pull request.
If you want to contribute documentation, please go to static-php/static-php-cli-docs.
Now there is a static-php organization, which is used to store the repo related to the project.
You can sponsor my project on this page.
This project itself is based on MIT License, some newly added extensions and dependencies may originate from the the other projects, and the headers of these code files will also be given additional instructions LICENSE and AUTHOR.
These are similar projects:
The project uses some code from dixyes/lwmbs, such as windows static build target and libiconv support. lwmbs is licensed under the Mulan PSL 2.
Due to the special nature of this project, many other open source projects such as curl and protobuf will be used during the project compilation process, and they all have their own open source licenses.
Please use the bin/spc dump-license
command to export the open source licenses used in the project after compilation,
and comply with the corresponding project's LICENSE.