static-php-cli/README.md

100 lines
6.2 KiB
Markdown
Raw Normal View History

2021-05-07 10:06:00 +08:00
# static-php-cli
2021-05-06 14:44:16 +08:00
Compile A Statically Linked PHP With Swoole and other Extensions. [English README](/README-en.md)
2021-06-05 23:48:37 +08:00
编译纯静态的 PHP Binary 二进制文件,带有各种扩展,让 PHP-cli 应用变得更便携!
注:只能编译 CLI 模式,暂不支持 CGI 和 FPM 模式
2021-04-26 16:32:26 +08:00
2021-06-06 01:03:10 +08:00
[![版本](https://img.shields.io/badge/script--version-1.3.0-green.svg)]()
2021-04-28 00:03:14 +08:00
2021-04-26 16:32:26 +08:00
## 环境需求
2021-04-28 15:34:23 +08:00
- 目前在 x86_64 和 aarch64(arm64) 架构上编译成功,其他架构需自行测试
2021-06-05 23:48:37 +08:00
- 需要 Docker或等我将脚本提出来也可以直接在 Alpine Linux 上使用)
2021-06-03 22:06:48 +08:00
- 脚本支持编译的 PHP 版本7.2 ~ 8.0
2021-04-26 16:32:26 +08:00
## 开始
2021-06-05 23:48:37 +08:00
可以直接在旁边的 Release 中下载编译好的二进制。
也可以自己使用 Dockerfile 进行编译构建:
2021-04-26 16:32:26 +08:00
```bash
2021-06-05 23:48:37 +08:00
git clone https://github.com/crazywhalecc/static-php-cli.git
cd static-php-cli/docker
docker build -t static-php .
```
2021-05-07 10:01:18 +08:00
2021-06-05 23:48:37 +08:00
编译之后可以使用下方命令将二进制 PHP 提取出来,用以下方式:
```bash
mkdir dist
2021-06-06 01:03:10 +08:00
docker run --rm -v $(pwd)/dist:/dist/ -it static-php cp php-dist/bin/php /dist/
cd dist
file ./php
2021-04-28 01:27:52 +08:00
```
2021-04-26 16:32:26 +08:00
2021-06-05 23:48:37 +08:00
如果要选择安装的扩展,可以修改 `docker/extensions.txt` 文件,具体规则如下:
- 文件内使用 `#` 可以注释,表示不安装
- 扩展名一律使用小写,目前默认状态下文件内所列的扩展为支持的扩展,其他扩展暂不支持,如有需求请提 Issue 添加
2021-06-06 00:49:00 +08:00
## 支持的扩展表
| 是否支持 | PHP 扩展名称 | 支持版本 | 备注 |
| -------- | ------------ | -------- | ------------------------------------------------------- |
| yes | bcmath | * | |
| yes | calendar | * | |
| yes | ctype | * | |
| yes | curl | * | |
| yes | dom | * | |
| | event | | |
| yes | filter | * | |
| yes | gd | * | |
| yes | hash | * | PHP7.3及以下版本需指定 `--enable-hash`7.4以上默认开启 |
| yes | iconv | * | |
| yes | inotify | 3.0.0 | |
| yes | json | * | |
| yes | libxml | * | |
| yes | mbstring | * | |
| | mongodb | | |
| | mysqli | | |
| yes | mysqlnd | * | |
| yes | openssl | * | |
| yes | pcntl | * | |
| yes | pdo | * | |
| yes | pdo_mysql | * | |
| | pdo_pgsql | * | |
| yes | phar | * | |
| yes | posix | * | |
| yes | redis | * | |
| yes | simplexml | * | |
| yes | sockets | * | |
| yes | swoole | >=4.6.6 | 默认支持mysqlnd, sockets, openssl, redis |
| yes | tokenizer | * | |
| yes | xml | * | |
| yes | xmlreader | * | |
| yes | xmlwriter | * | |
| | zip | | |
| yes | zlib | * | |
2021-04-26 16:32:26 +08:00
2021-06-06 00:59:20 +08:00
## 目前的问题(对勾为已解决)
2021-06-06 00:49:00 +08:00
- [ ] 不支持 event(libevent) 扩展event 扩展的 sockets 支持不能在静态编译中使用,因为静态内嵌编译暂时没办法调整扩展编译顺序,同时其本身也不支持静态编译。
2021-06-05 23:34:41 +08:00
- [ ] Swoole 扩展不支持 `--enable-swoole-curl`,也是因为编译顺序和加载顺序的问题。
2021-06-06 00:49:00 +08:00
- [ ] 不支持 readline 扩展readline 扩展安装后无法正常使用 `php -a`,原因还没有弄清楚,可能是静态编译造成的 ncurses 库出现了问题。
2021-06-05 23:34:41 +08:00
- [X] curl/libcurl 扩展静态编译
- [X] 可自行选择不需要编译进入的扩展
- [ ] php.ini 内嵌或分发
- [ ] i18n国际化脚本本身和 README
2021-06-06 00:59:20 +08:00
如果你对以上问题有解决方案,请提出 Issue 或 PR
2021-04-26 16:32:26 +08:00
## 运行示例
2021-04-26 16:35:36 +08:00
编译后的状态
2021-04-28 02:14:22 +08:00
![image](https://user-images.githubusercontent.com/20330940/116291663-6df47580-a7c7-11eb-8df3-6340c6f87055.png)
2021-04-26 16:35:36 +08:00
2021-04-26 16:32:26 +08:00
在不同系统直接运行 Swoft
![image](https://user-images.githubusercontent.com/20330940/116053161-f16d7400-a6ac-11eb-87b8-e510c6454861.png)
2021-04-26 20:12:30 +08:00
2021-06-06 11:16:59 +08:00
直接运行 Workerman
![image](https://user-images.githubusercontent.com/20330940/120911280-b2254080-c6b8-11eb-8c09-5c749b685b5d.png)
2021-04-27 16:14:18 +08:00
## 参考资料
- <https://blog.terrywh.net/post/2019/php-static-openssl/>
- <https://stackoverflow.com/a/37245653>
- <http://blog.gaoyuan.xyz/2014/04/09/statically-compile-php/>