static-php-cli/README-en.md

181 lines
8.4 KiB
Markdown
Raw Normal View History

2021-06-06 11:42:21 +08:00
# static-php-cli
2022-06-27 11:13:56 +08:00
Compile A Statically Linked PHP With Swoole and other Popular Extensions.
2021-05-07 09:33:06 +08:00
2022-06-27 11:09:32 +08:00
Compile A Single Binary With PHP Code.
2021-05-07 09:33:06 +08:00
2023-03-04 21:54:07 +08:00
**If you are intrested in this project, don't miss this discussion: <https://github.com/crazywhalecc/static-php-cli/discussions/29>**
2022-06-21 13:49:38 +08:00
[![version](https://img.shields.io/badge/version-1.5.2-green.svg)]()
2021-09-01 16:28:14 +08:00
![Build Actions](https://github.com/crazywhalecc/static-php-cli/actions/workflows/build-php.yml/badge.svg)
2021-05-07 09:33:06 +08:00
2021-07-28 14:27:18 +08:00
## Compilation Requirements
2022-05-16 17:02:57 +08:00
2022-04-02 16:50:43 +08:00
- Supporting architecture: `x86_64`, `arm64(aarch64)`, `armv7(armv7l)`
- Docker required (or alpine linux 3.13+)
2022-06-27 11:13:56 +08:00
- PHP version from 7.2 to 8.1
- Micro Package requires PHP >= 8.0
2021-05-07 09:33:06 +08:00
2022-06-27 11:14:43 +08:00
## Runtime Requirements
2022-05-16 17:02:57 +08:00
2021-07-28 14:27:18 +08:00
Linux
2022-05-15 01:41:58 +08:00
## Usage
2022-07-15 14:37:36 +08:00
1. Directly download static binary from this link or [Actions uploaded artifacts](https://github.com/crazywhalecc/static-php-cli/actions).
2021-09-01 16:28:14 +08:00
2022-04-02 16:50:43 +08:00
<https://dl.zhamao.xin/php-bin/file/>
2021-05-07 09:33:06 +08:00
2022-04-17 22:19:22 +08:00
2. Use fast install script `install-runtime.sh` to download static php and composer distribution into `runtime/` directory
But this script has some Chinese comments and prompts, if you cannot understand or have to use it in English, I will make an pure international version! :)
```bash
bash -c "`curl -fsSL https://raw.githubusercontent.com/crazywhalecc/static-php-cli/master/install-runtime.sh`"
```
2022-05-16 17:02:57 +08:00
## Packing PHP Code into a Static Binary
From v1.5.0, we support packing PHP code into a static binary. You can pack your PHP code into a static binary by micro.
You can directly download `micro-` prefix file, untar it and you will get file `micro.sfx`.
Here's a simple example to use it:
```bash
echo "<?php echo 'Hello world' . PHP_EOL;" > code.php
cat micro.sfx code.php > single-app && chmod +x single-app
./single-app
# If packing phar into a static binary, just change code.php to your phar path.
```
2022-06-27 11:11:40 +08:00
> Note: It means that your PHP code won't be compiled and you can't protect your source code by using micro!
>
> If you are looking for compiling PHP code or encrypting code, here's not your solution.
2022-06-27 11:09:32 +08:00
>
2022-06-21 13:49:38 +08:00
> Special thanks: <https://github.com/dixyes/phpmicro>
2022-05-16 17:02:57 +08:00
2022-05-15 01:41:58 +08:00
## Compiling
Here's help command to compile it yourself:
2022-04-17 22:19:22 +08:00
2021-05-07 09:33:06 +08:00
```bash
2021-06-06 11:41:55 +08:00
git clone https://github.com/crazywhalecc/static-php-cli.git
cd static-php-cli/docker
2022-06-11 02:09:18 +08:00
docker build -t static-php . --build-arg USE_BACKUP_ADDRESS=yes
# Making a directory to put binary files
mkdir dist
# It will ask you for PHP version, extensions, and compile static binaries
docker run --rm -v $(pwd)/dist:/dist/ -it static-php build-php
2021-05-07 09:33:06 +08:00
```
2022-06-11 02:09:18 +08:00
After compilation you can use command to get static php binary file.
2021-06-06 11:41:55 +08:00
```bash
cd dist
file ./php
```
2022-06-11 02:09:18 +08:00
If you don't want to use docker, a single script for compiling in **Alpine Linux**:
2022-05-15 01:41:58 +08:00
```bash
cd docker
# Change PHP Version
2022-06-10 18:23:07 +08:00
export VER_PHP="8.1.7"
2022-05-15 01:41:58 +08:00
# Use Original download link (Default is China mainland mirror link, for others please use 'yes' for original link)
export USE_BACKUP="yes"
./fast-compiler.sh
```
2021-06-06 11:41:55 +08:00
To customize PHP extensions, edit `docker/extensions.txt` file, and rules below:
2022-06-21 13:49:38 +08:00
- Use `^` as deselect, to mark not install. Use `#` as comments.
2021-06-06 11:41:55 +08:00
- extensions name uses lower case, and default file contains all supported extensions, if u need other extensions, consider write an Issue
2021-05-07 09:33:06 +08:00
2021-06-06 11:41:55 +08:00
## Supported PHP extensions
| Support | PHP Ext Name | Version | Comments |
| ------- | ------------ | ------- | ---------------------------------------- |
| yes | bcmath | * | |
| yes | calendar | * | |
| yes | ctype | * | |
| yes | curl | * | |
| yes | dom | * | |
2022-06-10 18:23:07 +08:00
| yes | event | >=3.0.8 | author's bitbucket version, not pecl |
| yes | exif | * | |
| yes | filter | * | |
2022-04-11 23:46:00 +08:00
| yes | fileinfo | * | |
| yes | gd | * | |
2021-07-28 14:28:43 +08:00
| yes | hash | * | |
| yes | iconv | * | |
| yes | inotify | 3.0.0 | |
| yes | json | * | |
| yes | libxml | * | |
| yes | mbstring | * | |
| yes | mongodb | >=1.9.1 | not tested |
| | mysqli | | |
| yes | mysqlnd | * | |
| yes | openssl | * | |
| yes | pcntl | * | |
| yes | pdo | * | |
| yes | pdo_mysql | * | |
| yes | pdo_sqlite | * | |
| | pdo_pgsql | * | |
| yes | phar | * | |
| yes | posix | * | |
2022-06-21 13:49:38 +08:00
| yes, not compiled | protobuf | * | Not compiled and enabled as default |
| yes | readline | * | Not support `./php -a` |
| yes | redis | * | |
| yes | shmop | * | |
| yes | simplexml | * | |
| yes | soap | * | |
| yes | sockets | * | |
| yes | sqlite3 | * | |
| yes | swoole | >=4.6.6 | support mysqlnd, sockets, openssl, redis |
| yes | tokenizer | * | |
| yes | xml | * | |
| yes | xmlreader | * | |
| yes | xmlwriter | * | |
2022-05-09 19:30:40 +08:00
| yes | zip | * | not support `bzip2`, `lzma` compression |
| yes | zlib | * | |
2021-06-06 11:41:55 +08:00
2021-06-06 11:53:16 +08:00
## Customization
2022-06-11 02:09:18 +08:00
- If you are going to run without prompt, Just add it to the end of `docker run xxx` cmd according to the parameters given below.
> 1st parameter `original` represents that you are using global original download address to fetch dependencies, if you are in mainland China, use `mirror`.
>
> 2nd parameter `8.1.7` is your PHP version you are compiling.
>
> 3rd parameter `all` represents that you will compile all supported extensions.
>
> 4th parameter `/dist/` is your binary output directory.
>
> For example, `docker run --rm -v $(pwd)/dist:/dist/ -it static-php build-php original 8.1.7 all /dist/`
2021-06-06 11:53:16 +08:00
- `docker/extensions.txt` edit extensions.
- `docker/compile-php.sh` file `php_compile_args` function to adjust PHP configure arguments.
- `docker/check-extensions.sh` file `check_in_configure` function to adjust extensions' configure arguments.
- `docker/config.json` edit extensions and dependencies version and download links.
2021-06-06 11:41:55 +08:00
## Current Issue
2022-06-10 18:23:07 +08:00
- [X] Not support event(libevent), because of its `config.m4` and code.
2021-06-06 11:41:55 +08:00
- [ ] Swoole not support `--enable-swoole-curl`.
- [X] Not support readline, maybe caused by ncurses library.
2021-06-06 11:41:55 +08:00
- [X] Not support curl (solved)
- [X] Customize extensions to compile
2022-06-11 02:09:18 +08:00
- [X] php.ini integration
2021-06-06 11:41:55 +08:00
- [X] i18n (including README and scripts)
2021-05-07 09:33:06 +08:00
## Running preview
2022-05-16 17:02:57 +08:00
### Using static binary
2022-05-15 01:10:07 +08:00
<img width="881" alt="未命名" src="https://user-images.githubusercontent.com/20330940/168441751-e62cb8d4-a3c8-42d9-b34e-d804b39756a1.png">
2021-05-07 09:33:06 +08:00
2022-05-16 17:02:57 +08:00
### Using swoole application packed with micro
2022-05-16 17:05:04 +08:00
<img width="937" alt="all" src="https://user-images.githubusercontent.com/20330940/168557743-b8f92263-712f-490e-9fe0-831597741595.png">
2022-05-16 17:02:57 +08:00
2021-05-07 09:33:06 +08:00
## References
- <https://blog.terrywh.net/post/2019/php-static-openssl/>
- <https://stackoverflow.com/a/37245653>
- <http://blog.gaoyuan.xyz/2014/04/09/statically-compile-php/>