Build single static PHP binary, with PHP project together, with popular extensions included

crazywhalecc 6131e1881b enable compile optimization for other libraries 1 год назад
.github 0e024a8c43 change default to docker (some c++ packages needed) 1 год назад
bin 09ba11affb update README and version 1 год назад
config 980da4ea0f fix mbregex without mbstring failed check (fix #96) 1 год назад
src 6131e1881b enable compile optimization for other libraries 1 год назад
.gitignore 117cd93e8f update gitignore 1 год назад
.php-cs-fixer.php 5d347adbcf initial framework commit 1 год назад
LICENSE df609e28ca update LICENSE 1 год назад
README-en.md f1eacac4fd change extension count 1 год назад
README.md f1eacac4fd change extension count 1 год назад
captainhook.json 880242ed93 update composer and readme 1 год назад
composer.json 0bed76da11 refactor download 1 год назад
ext-support.md 57e3193a2a add document 1 год назад
phpstan.neon 5d347adbcf initial framework commit 1 год назад

README-en.md

static-php-cli

Compile A Statically Linked PHP With Swoole and other Extensions.

Compile a purely static php-cli binary file with various extensions to make PHP applications more portable! (cli SAPI)

截屏2023-05-02 15 53 13

You can also use the micro binary file to combine php binary and php source code into one for distribution! This feature is provided by dixyes/phpmicro. (micro SAPI)

截屏2023-05-02 15 52 33

[Version]() [License]()

[]() []()

After the project releases the last RC version (maybe no more than 5 candidate releases), the project will rename the refactor branch to the main branch, please pay attention to changing the branch name for dependent projects.

Compilation Requirements

Yes, this project is written in PHP, pretty funny. But static-php-cli runtime only requires an environment above PHP 8.0 and tokenizer, iconv extension.

Here is the architecture support status, where CI represents support for GitHub Action builds, Local represents support for local builds, and blank represents not currently supported.

x86_64 aarch64
macOS CI, Local Local
Linux CI, Local CI, Local
Windows

macOS-arm64 is not supported for GitHub Actions, if you are going to build on arm, you can build it manually on your own machine.

Currently supported PHP versions for compilation are: 7.4, 8.0, 8.1, 8.2.

Docs

docs here: https://static-php-cli.zhamao.me.

Simple Usage

Please first select the extension you want to compile based on the extension list below.

Direct Download

If you don't compile yourself, you can download pre-compiled artifact from Actions, or from self-hosted server: Here

self-hosted server contains extensions: bcmath,bz2,calendar,ctype,curl,dom,exif,fileinfo,filter,ftp,gd,gmp,iconv,xml,mbstring,mbregex,mysqlnd,openssl,pcntl,pdo,pdo_mysql,pdo_sqlite,phar,posix,redis,session,simplexml,soap,sockets,sqlite3,tokenizer,xmlwriter,xmlreader,zlib,zip

Supported Extensions

Supported Extension List

If there is no extension you need here, you can submit an issue.

GitHub Actions Build

Use GitHub Action to easily build a statically compiled PHP and phpmicro, and at the same time define the extensions to be compiled by yourself.

  1. Fork me.
  2. Go to the Actions of the project and select CI.
  3. Select 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)
  4. After waiting for about a period of time, enter the corresponding task and get Artifacts.

If you enable debug, all logs will be output at build time, including compiled logs, for troubleshooting.

  • When using ubuntu-latest, it will build linux-x86_64 binary.
  • When using macos-latest, it will build macOS-x86_64 binary.

Manual Build

Clone repo first:

git clone https://github.com/crazywhalecc/static-php-cli.git

If you have not installed php on your system, you can use package management to install PHP (such as brew, apt, yum, apk etc.).

And you can also download single-file php binary and composer using command bin/setup-runtime. The PHP runtime for static-php-cli itself will be downloaded at bin/php, and composer is at bin/composer.

cd static-php-cli
chmod +x bin/setup-runtime
# It will download php-cli from self-hosted server and composer from getcomposer.org
./bin/setup-runtime

# Use this php runtime to run static-php-cli compiler
./bin/php bin/spc

# Use composer
./bin/php bin/composer

Basic usage for building php and micro with some extensions:

cd static-php-cli
composer update
chmod +x bin/spc
# Check system tool dependencies, fix them automatically
./bin/spc doctor
# fetch all libraries
./bin/spc fetch --all
# 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

You can also use the parameter --with-php=x.y to specify the downloaded PHP version, currently supports 7.4 ~ 8.2:

# Using PHP >= 8.0 is recommended, because 7.4 cannot use phpmicro
./bin/spc fetch --with-php=8.2 --all

Now we support cli, micro, fpm, 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-all: build all

If anything goes wrong, use --debug option to display full terminal output:

./bin/spc build openssl,pcntl,mbstring --debug --build-all
./bin/spc fetch --all --debug

In addition, we build NTS by default. If you are going to build ZTS version, just add --enable-zts option.

./bin/spc build openssl,pcntl --build-all --enable-zts

Adding option --no-strip can produce binaries with debug symbols, in order to debug (using gdb). Disabling strip will increase the size of static binary.

php-cli Usage

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)

micro.sfx Usage

phpmicro is a SelF-extracted eXecutable SAPI module, provided by dixyes/phpmicro. 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 packing a PHAR file, replace code.php with the Phar file path.

In some cases, PHAR files may not run in a micro environment.

php-fpm Usage

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 normal 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.

Current Status

  • [X] Basic CLI framework (by symfony/console)
  • [X] Linux support
  • [X] macOS support
  • [X] Exception handler
  • Windows support
  • [X] PHP 7.4 support
  • [X] fpm support

More functions and features are coming soon, Bugs and TODOs: https://github.com/crazywhalecc/static-php-cli/issues/32

Contribution

Currently, there are only a few supported extensions. 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.

The basic principles for contributing are as follows:

  • This project uses php-cs-fixer and phpstan as code formatting tools. Before contributing, please run composer analyze and composer cs-fix on the updated code.
  • If other open source libraries are involved, the corresponding licenses should be provided. Also, configuration files should be sorted using the command sort-config after modification. For more information about sorting commands, see the documentation.
  • Naming conventions should be followed, such as using the extension name registered in PHP for the extension name itself, and external library names should follow the project's own naming conventions. For internal logic functions, class names, variables, etc., camelCase and underscore formats should be followed, and mixing within the same module is prohibited.
  • When compiling external libraries and creating patches, compatibility with different operating systems should be considered.

If you want to contribute document content, please go to crazywhalecc/static-php-cli-docs.

Part of the English document is written by me, and part is translated by Google, and there may be inaccurate descriptions, strange or offensive expressions. If you are a native English speaker, some corrections to the documentation are welcome.

Sponsor this project

You can sponsor my project on this page.

Open-Source License

This project is based on the tradition of using the MIT License for old versions, while the new version references source code from some other projects:

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.

Advanced

The refactoring branch of this project is written modularly. If you are interested in this project and want to join the development, you can refer to the Contribution Guide of the documentation to contribute code or documentation. (TODO)