static-php-cli/README.md

174 lines
8.6 KiB
Markdown
Raw Normal View History

# StaticPHP
2023-03-19 14:05:30 +08:00
2024-02-12 10:39:37 +01:00
[![Chinese readme](https://img.shields.io/badge/README-%E4%B8%AD%E6%96%87%20%F0%9F%87%A8%F0%9F%87%B3-moccasin?style=flat-square)](README-zh.md)
[![English readme](https://img.shields.io/badge/README-English%20%F0%9F%87%AC%F0%9F%87%A7-moccasin?style=flat-square)](README.md)
[![Releases](https://img.shields.io/packagist/v/crazywhalecc/static-php-cli?include_prereleases&label=Release&style=flat-square)](https://github.com/crazywhalecc/static-php-cli/releases)
[![CI](https://img.shields.io/github/actions/workflow/status/crazywhalecc/static-php-cli/tests.yml?branch=main&label=Build%20Test&style=flat-square)](https://github.com/crazywhalecc/static-php-cli/actions/workflows/tests.yml)
[![License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://github.com/crazywhalecc/static-php-cli/blob/main/LICENSE)
2023-04-21 19:10:23 +08:00
**StaticPHP** is a powerful tool designed for building portable executables including PHP, extensions, and more.
2021-05-06 14:44:16 +08:00
2024-01-10 11:11:45 +08:00
## Features
2023-05-02 15:54:52 +08:00
2025-09-21 11:22:11 +08:00
- :elephant: Support multiple PHP versions - PHP 8.1, 8.2, 8.3, 8.4, 8.5
2025-09-20 13:58:09 +08:00
- :handbag: Build single-file PHP executable with zero dependencies
- :hamburger:Build **[phpmicro](https://github.com/dixyes/phpmicro)** self-extracting executables (combines PHP binary and source code into one file)
- :pill: Automatic build environment checker with auto-fix capabilities
2024-01-11 09:51:48 +08:00
- :zap: `Linux`, `macOS`, `FreeBSD`, `Windows` support
2025-09-20 13:58:09 +08:00
- :wrench: Configurable source code patching
- :books: Intelligent dependency management
- 📦 Self-contained `spc` executable (built with [box](https://github.com/box-project/box))
- :fire: Support 100+ popular [extensions](https://static-php.dev/en/guide/extensions.html)
2025-09-20 13:58:09 +08:00
- :floppy_disk: UPX compression support (reduces binary size by 30-50%)
2022-05-16 17:02:57 +08:00
2024-01-10 11:11:45 +08:00
**Single-file standalone php-cli:**
2024-01-10 11:13:47 +08:00
2024-01-10 11:11:45 +08:00
<img width="700" alt="out1" src="https://github.com/crazywhalecc/static-php-cli/assets/20330940/01a2e60f-13b0-4242-a645-f7afa4936396">
2023-10-15 17:01:36 +08:00
2024-01-10 11:11:45 +08:00
**Combine PHP code with PHP interpreter using phpmicro:**
2024-01-10 11:13:47 +08:00
2024-01-10 11:11:45 +08:00
<img width="700" alt="out2" src="https://github.com/crazywhalecc/static-php-cli/assets/20330940/46b7128d-fb72-4169-957e-48564c3ff3e2">
2021-04-28 00:03:14 +08:00
2025-09-20 13:58:09 +08:00
## Quickstart
2023-04-22 22:41:00 +08:00
2025-09-20 13:58:09 +08:00
### 1. Download spc binary
2023-03-19 14:05:30 +08:00
2024-01-10 11:11:45 +08:00
```bash
# For Linux x86_64
2024-08-09 16:56:15 +08:00
curl -fsSL -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-x86_64
2024-01-10 11:11:45 +08:00
# For Linux aarch64
2024-08-09 16:56:15 +08:00
curl -fsSL -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-aarch64
2024-01-10 11:11:45 +08:00
# macOS x86_64 (Intel)
2024-08-09 16:56:15 +08:00
curl -fsSL -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-macos-x86_64
2024-01-10 11:11:45 +08:00
# macOS aarch64 (Apple)
2024-08-09 16:56:15 +08:00
curl -fsSL -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-macos-aarch64
2025-09-20 13:58:09 +08:00
# Windows (x86_64, win10 build 17063 or later, please install VS2022 first)
2024-09-27 12:00:54 +01:00
curl.exe -fsSL -o spc.exe https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-windows-x64.exe
2024-01-10 11:11:45 +08:00
```
2023-10-30 20:32:59 +08:00
2025-09-20 13:58:09 +08:00
For macOS and Linux, add execute permission first:
2024-07-14 01:19:34 +08:00
2022-05-15 00:57:14 +08:00
```bash
2025-09-20 13:58:09 +08:00
chmod +x ./spc
2023-04-22 22:18:44 +08:00
```
2025-09-20 13:58:09 +08:00
### 2. Build Static PHP
First, create a `craft.yml` file and specify which extensions you want to include from [extension list](https://static-php.dev/en/guide/extensions.html) or [command generator](https://static-php.dev/en/guide/cli-generator.html):
```yml
# PHP version support: 8.1, 8.2, 8.3, 8.4, 8.5
php-version: 8.4
# Put your extension list here
extensions: "apcu,bcmath,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,gd,iconv,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,pdo_mysql,pdo_sqlite,phar,posix,readline,redis,session,simplexml,sockets,sodium,sqlite3,tokenizer,xml,xmlreader,xmlwriter,xsl,zip,zlib"
sapi:
- cli
- micro
- fpm
download-options:
prefer-pre-built: true
```
2025-09-20 13:58:09 +08:00
Run command:
```bash
2025-09-20 13:58:09 +08:00
./spc craft
2022-05-15 00:57:14 +08:00
2025-09-20 13:58:09 +08:00
# Output full console log
./spc craft --debug
2023-04-09 13:38:41 +08:00
```
2025-09-20 13:58:09 +08:00
### 3. Static PHP usage
2023-10-26 20:37:35 +08:00
Now you can copy binaries built by StaticPHP to another machine and run with no dependencies:
2023-03-19 14:05:30 +08:00
```
2025-09-20 13:58:09 +08:00
# php-cli
buildroot/bin/php -v
2023-03-19 14:05:30 +08:00
2025-09-20 13:58:09 +08:00
# phpmicro
echo '<?php echo "Hello world!\n";' > a.php
./spc micro:combine a.php -O my-app
./my-app
2023-03-19 14:05:30 +08:00
2025-09-20 13:58:09 +08:00
# php-fpm
buildroot/bin/php-fpm -v
```
2025-09-20 13:58:09 +08:00
## Documentation
2023-03-19 14:05:30 +08:00
The current README contains basic usage. For all the features of StaticPHP,
2025-09-20 13:58:09 +08:00
see <https://static-php.dev> .
2023-03-19 14:05:30 +08:00
2025-09-20 13:58:09 +08:00
## Direct Download
2023-03-19 14:05:30 +08:00
2025-09-20 13:58:09 +08:00
If you don't want to build or want to test first, you can download example pre-compiled artifact from [Actions](https://github.com/static-php/static-php-cli-hosted/actions/workflows/build-php-bulk.yml), or from self-hosted server.
2023-04-23 20:29:50 +08:00
2025-09-20 13:58:09 +08:00
Below are several precompiled static-php binaries with different extension combinations,
which can be downloaded directly according to your needs.
2023-04-23 20:29:50 +08:00
2025-09-20 13:58:09 +08:00
| Combination | Extension Count | OS | Comment |
|----------------------------------------------------------------------|----------------------------------------------------------------------------|--------------|--------------------------------|
| [common](https://dl.static-php.dev/static-php-cli/common/) | [30+](https://dl.static-php.dev/static-php-cli/common/README.txt) | Linux, macOS | The binary size is about 7.5MB |
| [bulk](https://dl.static-php.dev/static-php-cli/bulk/) | [50+](https://dl.static-php.dev/static-php-cli/bulk/README.txt) | Linux, macOS | The binary size is about 25MB |
2025-09-21 11:26:22 +08:00
| [gnu-bulk](https://dl.static-php.dev/static-php-cli/gnu-bulk/) | [50+](https://dl.static-php.dev/static-php-cli/bulk/README.txt) | Linux, macOS | Using shared glibc |
2025-09-20 13:58:09 +08:00
| [minimal](https://dl.static-php.dev/static-php-cli/minimal/) | [5](https://dl.static-php.dev/static-php-cli/minimal/README.txt) | Linux, macOS | The binary size is about 3MB |
| [spc-min](https://dl.static-php.dev/static-php-cli/windows/spc-min/) | [5](https://dl.static-php.dev/static-php-cli/windows/spc-min/README.txt) | Windows | The binary size is about 3MB |
| [spc-max](https://dl.static-php.dev/static-php-cli/windows/spc-max/) | [40+](https://dl.static-php.dev/static-php-cli/windows/spc-max/README.txt) | Windows | The binary size is about 8.5MB |
2023-04-23 20:29:50 +08:00
2025-09-20 13:58:09 +08:00
> Linux and Windows supports UPX compression for binaries, which can reduce the size of the binary by 30% to 50%.
> macOS does not support UPX compression, so the size of the pre-built binaries for mac is larger.
2023-04-23 20:29:50 +08:00
2025-09-20 13:58:09 +08:00
### Build Online (using GitHub Actions)
2023-09-12 22:44:19 +08:00
2025-09-21 11:25:09 +08:00
When the above direct download binaries cannot meet your needs,
you can use GitHub Action to easily build a statically compiled PHP,
2025-09-20 13:58:09 +08:00
and at the same time define the extensions to be compiled by yourself.
2023-09-12 22:44:19 +08:00
2025-09-20 13:58:09 +08:00
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`.
2023-09-13 10:01:08 +08:00
2025-09-20 13:58:09 +08:00
If you enable `debug`, all logs will be output at build time, including compiled logs, for troubleshooting.
2023-09-12 22:44:19 +08:00
## Contribution
2023-03-19 14:05:30 +08:00
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.
2023-03-19 14:05:30 +08:00
If you want to contribute documentation, please just edit in `docs/`.
2023-11-01 20:48:23 +08:00
Now there is a [static-php](https://github.com/static-php) organization, which is used to store the repo related to the project.
2023-03-19 14:05:30 +08:00
## Sponsor this project
2023-07-17 18:39:34 +08:00
2024-12-23 11:04:46 +08:00
You can sponsor me or my project from [GitHub Sponsor](https://github.com/crazywhalecc). A portion of your donation will be used to maintain the **static-php.dev** server.
2023-05-29 23:49:55 +08:00
2025-06-30 21:41:48 +08:00
**Special thanks to sponsors below**:
<a href="https://beyondco.de/"><img src="/docs/public/images/beyondcode-seeklogo.png" width="300" alt="Beyond Code Logo" /></a>
<a href="https://nativephp.com/"><img src="/docs/public/images/nativephp-logo.svg" width="300" alt="NativePHP Logo" /></a>
## Open-Source License
2023-05-29 23:49:55 +08:00
This project itself is based on MIT License,
2023-11-21 23:35:34 +08:00
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.
2023-03-18 14:19:45 +08:00
2023-11-21 23:35:34 +08:00
These are similar projects:
- [dixyes/lwmbs](https://github.com/dixyes/lwmbs)
- [swoole/swoole-cli](https://github.com/swoole/swoole-cli)
2023-03-18 14:19:45 +08:00
2024-01-10 21:08:25 +08:00
The project uses some code from [dixyes/lwmbs](https://github.com/dixyes/lwmbs), such as windows static build target and libiconv support.
lwmbs is licensed under the [Mulan PSL 2](http://license.coscl.org.cn/MulanPSL2).
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.
2023-03-18 14:19:45 +08:00
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.