static-php-cli/README-en.md

242 lines
10 KiB
Markdown
Raw Normal View History

2021-06-06 11:42:21 +08:00
# static-php-cli
2023-03-19 14:05:30 +08:00
2023-03-18 14:19:45 +08:00
Compile A Statically Linked PHP With Swoole and other Extensions.
2022-06-27 11:13:56 +08:00
2023-05-02 16:08:53 +08:00
Compile a purely static php-cli binary file with various extensions to make PHP applications more portable! (cli SAPI)
2023-05-02 15:54:15 +08:00
<img width="600" alt="截屏2023-05-02 15 53 13" src="https://user-images.githubusercontent.com/20330940/235610282-23e58d68-bd35-4092-8465-171cff2d5ba8.png">
2021-05-07 09:33:06 +08:00
2023-05-02 16:08:53 +08:00
You can also use the micro binary file to combine php binary and php source code into one for distribution!
2023-05-02 15:54:15 +08:00
This feature is provided by [dixyes/phpmicro](https://github.com/dixyes/phpmicro). (micro SAPI)
<img width="600" alt="截屏2023-05-02 15 52 33" src="https://user-images.githubusercontent.com/20330940/235610318-2ef4e3f1-278b-4ca4-99f4-b38120efc395.png">
2021-05-07 09:33:06 +08:00
2023-05-17 22:42:37 +08:00
[![Version](https://img.shields.io/badge/Version-2.0--rc1-pink.svg?style=flat-square)]()
2023-04-09 13:58:09 +08:00
[![License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)]()
2023-04-30 14:29:56 +08:00
[![](https://img.shields.io/github/actions/workflow/status/crazywhalecc/static-php-cli/build-linux-x86_64.yml?branch=refactor&label=Linux%20Build&style=flat-square)](https://github.com/crazywhalecc/static-php-cli/actions/workflows/build.yml)
[![](https://img.shields.io/github/actions/workflow/status/crazywhalecc/static-php-cli/build-macos-x86_64.yml?branch=refactor&label=macOS%20Build&style=flat-square)](https://github.com/crazywhalecc/static-php-cli/actions/workflows/build.yml)
2023-05-02 16:05:04 +08:00
2023-05-17 22:41:04 +08:00
[![](https://img.shields.io/badge/Extension%20Counter-50+-yellow.svg?style=flat-square)]()
2023-04-22 22:52:02 +08:00
[![](https://img.shields.io/github/search/crazywhalecc/static-php-cli/TODO?label=TODO%20Counter&style=flat-square)]()
2023-04-09 13:58:09 +08:00
2021-07-28 14:27:18 +08:00
## Compilation Requirements
2022-05-16 17:02:57 +08:00
2023-03-18 14:19:45 +08:00
Yes, this project is written in PHP, pretty funny.
2023-04-22 22:25:01 +08:00
But static-php-cli runtime only requires an environment above PHP 8.0 and `tokenizer`, `iconv` extension.
2022-05-16 17:02:57 +08:00
2023-04-15 21:44:34 +08:00
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.
2023-05-02 16:05:29 +08:00
| | x86_64 | aarch64 |
|---------|-----------|-----------|
| macOS | CI, Local | Local |
| Linux | CI, Local | CI, Local |
| Windows | | |
2023-04-15 21:44:34 +08:00
2023-04-30 12:55:49 +08:00
> 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.
2023-04-15 21:44:34 +08:00
2023-04-22 22:25:01 +08:00
Currently supported PHP versions for compilation are: `7.4`, `8.0`, `8.1`, `8.2`.
2022-05-16 17:02:57 +08:00
2023-03-27 00:13:52 +08:00
## Usage
2022-05-16 17:02:57 +08:00
2023-04-08 22:35:10 +08:00
Please first select the extension you want to compile based on the extension list below.
2022-05-15 01:41:58 +08:00
2023-05-01 12:57:31 +08:00
### Direct Download
If you don't compile yourself, you can download pre-compiled artifact from Actions, or from self-hosted server: [Here](https://dl.zhamao.xin/static-php-cli/)
> 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`
2023-04-14 13:38:58 +08:00
### Supported Extensions
2023-04-03 21:30:32 +08:00
2023-04-22 22:18:44 +08:00
[Supported Extension List](/ext-support.md)
2023-04-03 21:30:32 +08:00
2023-04-22 22:41:00 +08:00
> If there is no extension you need here, you can submit an issue.
2023-04-08 22:35:10 +08:00
### 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`.
2023-04-15 18:58:29 +08:00
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`)
2023-04-08 22:35:10 +08:00
4. After waiting for about a period of time, enter the corresponding task and get `Artifacts`.
2023-04-09 13:38:41 +08:00
If you enable `debug`, all logs will be output at build time, including compiled logs, for troubleshooting.
2023-04-15 21:29:05 +08:00
- When using ubuntu-latest, it will build linux-x86_64 binary.
- When using macos-latest, it will build macOS-x86_64 binary.
2023-04-08 22:35:10 +08:00
### Manual Build
2023-03-19 14:05:30 +08:00
2023-04-22 22:18:44 +08:00
Clone repo first:
2022-05-15 01:41:58 +08:00
```bash
2023-04-09 13:38:41 +08:00
git clone https://github.com/crazywhalecc/static-php-cli.git
2023-04-22 22:18:44 +08:00
```
If you have not installed php on your system, you can download single-file php binary and composer first.
The PHP runtime for static-php-cli itself will be downloaded at `bin/php`, and composer is at `bin/composer`.
```bash
cd static-php-cli
chmod +x bin/setup-runtime
./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:
```bash
2023-04-09 13:38:41 +08:00
cd static-php-cli
composer update
chmod +x bin/spc
2023-04-22 22:18:44 +08:00
# Check system tool dependencies, fix them automatically (only support macOS) (TODO: Linux distro support)
./bin/spc doctor
2023-03-18 17:34:25 +08:00
# fetch all libraries
2023-04-09 13:38:41 +08:00
./bin/spc fetch --all
2023-03-19 14:13:24 +08:00
# with bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl extension, build both CLI and phpmicro SAPI
2023-04-23 20:29:50 +08:00
./bin/spc build bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl --build-cli --build-micro
2023-04-09 13:38:41 +08:00
```
You can also use the parameter `--with-php=x.y` to specify the downloaded PHP version, currently supports 7.4 ~ 8.2:
```bash
2023-04-22 22:18:44 +08:00
# Using PHP >= 8.0 is recommended, because 7.4 cannot use phpmicro
2023-04-09 13:38:41 +08:00
./bin/spc fetch --with-php=8.2 --all
```
2023-04-23 20:29:50 +08:00
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
2023-04-09 13:38:41 +08:00
If anything goes wrong, use `--debug` option to display full terminal output:
```bash
2023-04-23 20:29:50 +08:00
./bin/spc build openssl,pcntl,mbstring --debug --build-all
2023-04-09 13:38:41 +08:00
./bin/spc fetch --all --debug
2022-05-15 01:41:58 +08:00
```
2023-05-17 23:07:39 +08:00
In addition, we build NTS by default. If you are going to build ZTS version, just add `--enable-zts` option.
```bash
./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.
2023-03-19 14:05:30 +08:00
### php-cli Usage
2023-04-22 23:59:36 +08:00
> php-cli is a single static binary, you can use it like normal php installed on your system.
2023-03-31 00:16:40 +08:00
2023-04-23 20:29:50 +08:00
When using the parameter `--build-cli` or `--build-all`,
2023-03-19 14:05:30 +08:00
the final compilation result will output a binary file named `./php`,
which can be distributed and used directly.
2023-04-22 22:25:01 +08:00
This file will be located in the directory `buildroot/bin/`, copy it out for use.
2023-03-19 14:05:30 +08:00
```bash
2023-04-22 22:25:01 +08:00
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)
2023-03-19 14:05:30 +08:00
```
### micro.sfx Usage
2023-06-02 09:59:47 +08:00
> phpmicro is a SelF-extracted eXecutable SAPI module,
2023-04-22 22:18:44 +08:00
> provided by [dixyes/phpmicro](https://github.com/dixyes/phpmicro).
> It can put php runtime and your source code together.
2023-03-31 00:16:40 +08:00
2023-03-19 14:05:30 +08:00
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`.
2023-03-31 00:16:40 +08:00
This file will be located in the path `buildroot/bin/micro.sfx`, simply copy it out for use.
2023-03-19 14:05:30 +08:00
Prepare your project source code, which can be a single PHP file or a Phar file, for use.
```bash
echo "<?php echo 'Hello world' . PHP_EOL;" > code.php
cat micro.sfx code.php > single-app && chmod +x single-app
./single-app
2023-04-21 19:10:23 +08:00
# If packing a PHAR file, replace code.php with the Phar file path.
2023-03-19 14:05:30 +08:00
```
> In some cases, PHAR files may not run in a micro environment.
2023-04-23 20:29:50 +08:00
### 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`.
2023-03-18 14:19:45 +08:00
## Current Status
2023-03-27 00:13:52 +08:00
- [X] Basic CLI framework (by `symfony/console`)
- [X] Linux support
2023-03-18 17:34:25 +08:00
- [X] macOS support
2023-03-18 14:19:45 +08:00
- [X] Exception handler
- [ ] Windows support
2023-03-18 20:08:54 +08:00
- [X] PHP 7.4 support
2023-04-23 20:29:50 +08:00
- [X] fpm support
2023-03-18 14:19:45 +08:00
2023-03-31 00:16:40 +08:00
More functions and features are coming soon, Bugs and TODOs: https://github.com/crazywhalecc/static-php-cli/issues/32
2023-03-19 14:05:30 +08:00
## 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.
2023-05-29 23:53:26 +08:00
## Sponsor this project
You can sponsor my project on [this page](https://github.com/crazywhalecc/crazywhalecc/blob/master/FUNDING.md).
2023-03-19 14:05:30 +08:00
## Open-Source License
2023-03-18 14:19:45 +08:00
This project is based on the tradition of using the MIT License for old versions,
2023-04-09 13:38:41 +08:00
while the new version references source code from some other projects:
2023-03-18 14:19:45 +08:00
2023-03-18 17:44:23 +08:00
- [dixyes/lwmbs](https://github.com/dixyes/lwmbs) (Mulun Permissive License)
- [swoole/swoole-cli](https://github.com/swoole/swoole-cli) (Apache 2.0 LICENSE+SWOOLE-CLI LICENSE)
2023-03-18 14:19:45 +08:00
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-06-02 09:55:33 +08:00
Please use the `bin/spc dump-license` command to export the open source licenses used in the project after compilation,
2023-03-18 14:19:45 +08:00
and comply with the corresponding project's LICENSE.
2023-03-31 00:16:40 +08:00
## Advanced
This project is pure open source project, and some modules are separated for developing.
2023-04-09 13:38:41 +08:00
This section will be improved after refactor version released.