Static CLI Binary
You can easily compile a standalone php binary for general use. Including CLI, FPM sapi.
diff --git a/404.html b/404.html index 8789e0c2..94b7038d 100644 --- a/404.html +++ b/404.html @@ -13,7 +13,7 @@
404
But if you don't change your direction, and if you keep looking, you may end up where you are heading.
The download source module of static-php-cli is a major module. It includes dependent libraries, external extensions, PHP source code download methods and file decompression methods. The download configuration file mainly involves the source.json
file, which records the download method of all downloadable resources.
The main commands involved in the download function are bin/spc download
and bin/spc extract
. The download
command is a downloader that downloads resources according to the configuration file, and the extract
command is an extractor that extract sources from downloaded files.
Generally speaking, downloading resources may be slow because these resources come from various official websites, GitHub, and other different locations. At the same time, they also occupy a large space, so you can download the resources once and reuse them.
The configuration file of the downloader is source.json
, which contains the download methods of all resources. You can add the resource download methods you need, or modify the existing resource download methods.
The download configuration structure of each resource is as follows. The following is the resource download configuration corresponding to the libevent
extension:
{
+import{_ as o,c as e,o as s,U as a}from"./chunks/framework.a73c7ff7.js";const F=JSON.parse('{"title":"Source module","description":"","frontmatter":{},"headers":[],"relativePath":"en/develop/source-module.md","filePath":"en/develop/source-module.md"}'),n={name:"en/develop/source-module.md"},l=a(`Source module
The download source module of static-php-cli is a major module. It includes dependent libraries, external extensions, PHP source code download methods and file decompression methods. The download configuration file mainly involves the source.json
file, which records the download method of all downloadable sources.
The main commands involved in the download function are bin/spc download
and bin/spc extract
. The download
command is a downloader that downloads sources according to the configuration file, and the extract
command is an extractor that extract sources from downloaded files.
Generally speaking, downloading sources may be slow because these sources come from various official websites, GitHub, and other different locations. At the same time, they also occupy a large space, so you can download the sources once and reuse them.
The configuration file of the downloader is source.json
, which contains the download methods of all sources. You can add the source download methods you need, or modify the existing source download methods.
The download configuration structure of each source is as follows. The following is the source download configuration corresponding to the libevent
extension:
json{
"libevent": {
"type": "ghrel",
"repo": "libevent/libevent",
@@ -8,4 +8,4 @@ import{_ as o,c as e,o as s,U as a}from"./chunks/framework.a73c7ff7.js";const F=
"path": "LICENSE"
}
}
-}
The most important field here is type
. Currently, the types it supports are:
url
: Directly use URL to download, for example: https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz
.ghrel
: Use the GitHub Release API to download, download the artifacts uploaded from the latest version released by maintainers.ghtar
: Use the GitHub Release API to download. Different from ghrel
, ghtar
is downloaded from the source code (tar.gz)
in the latest Release of the project.ghtagtar
: Use GitHub Release API to download. Compared with ghtar
, ghtagtar
can find the latest one from the tags
list and download the source code in tar.gz
format (because some projects only use tag
release version).bitbuckettag
: Download using BitBucket API, basically the same as ghtagtar
, except this one applies to BitBucket.git
: Clone the project directly from a Git address to download resources, applicable to any public Git repository.filelist
: Use a crawler to crawl the Web download site that provides file index, and get the latest version of the file name and download it.custom
: If none of the above download methods are satisfactory, you can write custom
, create a new class under src/SPC/store/source/
, inherit CustomSourceBase
, and write the download script yourself.
(TODO)
`,10),t=[l];function c(p,r,d,i,u,D){return s(),e("div",null,t)}const h=o(n,[["render",c]]);export{F as __pageData,h as default};
+}
The most important field here is type
. Currently, the types it supports are:
url
: Directly use URL to download, for example: https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz
.ghrel
: Use the GitHub Release API to download, download the artifacts uploaded from the latest version released by maintainers.ghtar
: Use the GitHub Release API to download. Different from ghrel
, ghtar
is downloaded from the source code (tar.gz)
in the latest Release of the project.ghtagtar
: Use GitHub Release API to download. Compared with ghtar
, ghtagtar
can find the latest one from the tags
list and download the source code in tar.gz
format (because some projects only use tag
release version).bitbuckettag
: Download using BitBucket API, basically the same as ghtagtar
, except this one applies to BitBucket.git
: Clone the project directly from a Git address to download sources, applicable to any public Git repository.filelist
: Use a crawler to crawl the Web download site that provides file index, and get the latest version of the file name and download it.custom
: If none of the above download methods are satisfactory, you can write custom
, create a new class under src/SPC/store/source/
, inherit CustomSourceBase
, and write the download script yourself.(TODO)
`,10),t=[l];function c(p,r,d,i,u,D){return s(),e("div",null,t)}const h=o(n,[["render",c]]);export{F as __pageData,h as default}; diff --git a/assets/en_develop_source-module.md.9402ba24.lean.js b/assets/en_develop_source-module.md.faae4360.lean.js similarity index 100% rename from assets/en_develop_source-module.md.9402ba24.lean.js rename to assets/en_develop_source-module.md.faae4360.lean.js diff --git a/assets/en_develop_structure.md.1a717dd0.js b/assets/en_develop_structure.md.1a717dd0.js new file mode 100644 index 00000000..ebc4004e --- /dev/null +++ b/assets/en_develop_structure.md.1a717dd0.js @@ -0,0 +1 @@ +import{_ as e,c as o,o as t,U as c}from"./chunks/framework.a73c7ff7.js";const b=JSON.parse('{"title":"Introduction to project structure","description":"","frontmatter":{},"headers":[],"relativePath":"en/develop/structure.md","filePath":"en/develop/structure.md"}'),d={name:"en/develop/structure.md"},n=c('static-php-cli mainly contains three logical components: sources, dependent libraries, and extensions. These three components also correspond to three configuration files: source.json
, lib.json
, and ext.json
.
A complete process for building standalone static PHP is:
Downloader
to download specified or all source codes. These sources include PHP source code, dependent library source code, and extension source code.SourceExtractor
to decompress the downloaded sources to the compilation directory.Builder
under the corresponding operating system, install it to the buildroot
directory.source/php-src/ext/
directory.Builder
to build the PHP source code and build target to the buildroot
directory.The project is mainly divided into several folders:
bin/
: used to store program entry files, including bin/spc
, bin/spc-alpine-docker
, bin/setup-runtime
.config/
: Contains all the extensions and dependent libraries supported by the project, as well as the download link and download methods of these sources. It is divided into three files: lib.json
, ext.json
, source.json
.src/
: The core code of the project, including the entire framework and commands for compiling various extensions and libraries.vendor/
: The directory that Composer depends on, you do not need to make any modifications to it.The operating principle is to start a ConsoleApplication
of symfony/console
, and then parse the commands entered by the user in the terminal.
bin/spc
is an entry file, including the Unix common #!/usr/bin/env php
, which is used to allow the system to automatically execute with the PHP interpreter installed on the system. After the project executes new ConsoleApplication()
, the framework will automatically register them as commands.
The project does not directly use the Command registration method and command execution method recommended by Symfony. Here are small changes:
#[AsCommand()]
Attribute to register the name and description.execute()
so that all commands are based on BaseCommand
(which is based on Symfony\\Component\\Console\\Command\\Command
), and the execution code of each command itself is written in the handle()
method .$no_motd
to BaseCommand
, which is used to display the Figlet greeting when the command is executed.BaseCommand
saves InputInterface
and OutputInterface
as member variables. You can use $this->input
and $this->output
within the command class.The source code of the project is located in the src/SPC
directory, supports automatic loading of the PSR-4 standard, and contains the following subdirectories and classes:
src/SPC/builder/
: The core compilation command code used to build libraries, PHP and related extensions under different operating systems, and also includes some compilation system tool methods.src/SPC/command/
: All commands of the project are here.src/SPC/doctor/
: Doctor module, which is a relatively independent module used to check the system environment. It can be entered using the command bin/spc doctor
.src/SPC/exception/
: exception class.src/SPC/store/
: Classes related to storage, files and sources are all here.src/SPC/util/
: Some reusable tool methods are here.src/SPC/ConsoleApplication.php
: command line program entry file.If you have read the source code, you may find that there is also a src/globals/
directory, which is used to store some global variables, global methods, and non-PSR-4 standard PHP source code that is relied upon during the build process, such as extension sanity check code etc.
static-php-cli mainly contains three logical components: sources, dependent libraries, and extensions. These three components also correspond to three configuration files: source.json
, lib.json
, and ext.json
.
A complete process for building standalone static PHP is:
Downloader
to download specified or all source codes. These resources include PHP source code, dependent library source code, and extension source code.SourceExtractor
to decompress the downloaded resources to the compilation directory.Builder
under the corresponding operating system, install it to the buildroot
directory.source/php-src/ext/
directory.Builder
to build the PHP source code and build target to the buildroot
directory.The project is mainly divided into several folders:
bin/
: used to store program entry files, including bin/spc
, bin/spc-alpine-docker
, bin/setup-runtime
.config/
: Contains all the extensions and dependent libraries supported by the project, as well as the download link and download methods of these sources. It is divided into three files: lib.json
, ext.json
, source.json
.src/
: The core code of the project, including the entire framework and commands for compiling various extensions and libraries.vendor/
: The directory that Composer depends on, you do not need to make any modifications to it.The operating principle is to start a ConsoleApplication
of symfony/console
, and then parse the commands entered by the user in the terminal.
bin/spc
is an entry file, including the Unix common #!/usr/bin/env php
, which is used to allow the system to automatically execute with the PHP interpreter installed on the system. After the project executes new ConsoleApplication()
, the framework will automatically register them as commands.
The project does not directly use the Command registration method and command execution method recommended by Symfony. Here are small changes:
#[AsCommand()]
Attribute to register the name and description.execute()
so that all commands are based on BaseCommand
(which is based on Symfony\\Component\\Console\\Command\\Command
), and the execution code of each command itself is written in the handle()
method .$no_motd
to BaseCommand
, which is used to display the Figlet greeting when the command is executed.BaseCommand
saves InputInterface
and OutputInterface
as member variables. You can use $this->input
and $this->output
within the command class.The source code of the project is located in the src/SPC
directory, supports automatic loading of the PSR-4 standard, and contains the following subdirectories and classes:
src/SPC/builder/
: The core compilation command code used to build libraries, PHP and related extensions under different operating systems, and also includes some compilation system tool methods.src/SPC/command/
: All commands of the project are here.src/SPC/doctor/
: Doctor module, which is a relatively independent module used to check the system environment. It can be entered using the command bin/spc doctor
.src/SPC/exception/
: exception class.src/SPC/store/
: Classes related to storage, files and resources are all here.src/SPC/util/
: Some reusable tool methods are here.src/SPC/ConsoleApplication.php
: command line program entry file.If you have read the source code, you may find that there is also a src/globals/
directory, which is used to store some global variables, global methods, and non-PSR-4 standard PHP source code that is relied upon during the build process, such as extension sanity check code etc.
Use the build command to start building the static php binary. Before executing the bin/spc build
command, be sure to use the download
command to download resources. It is recommended to use doctor
to check the environment.
You need to go to Extension List or Command Generator to select the extension you want to add, and then use the command bin/spc build
to compile. You need to specify a compilation target, choose from the following parameters:
--build-cli
: Build a cli sapi (command line interface, which can execute PHP code on the command line)--build-fpm
: Build a fpm sapi (php-fpm, used in conjunction with other traditional fpm architecture software such as nginx)--build-micro
: Build a micro sapi (used to build a standalone executable binary containing PHP code)--build-embed
: Build an embed sapi (used to embed into other C language programs)--build-all
: build all above sapi# Compile PHP with bcmath,curl,openssl,ftp,posix,pcntl extensions, the compilation target is cli
+bin/spc doctor --auto-fix
Use the build command to start building the static php binary. Before executing the bin/spc build
command, be sure to use the download
command to download sources. It is recommended to use doctor
to check the environment.
You need to go to Extension List or Command Generator to select the extension you want to add, and then use the command bin/spc build
to compile. You need to specify a compilation target, choose from the following parameters:
--build-cli
: Build a cli sapi (command line interface, which can execute PHP code on the command line)--build-fpm
: Build a fpm sapi (php-fpm, used in conjunction with other traditional fpm architecture software such as nginx)--build-micro
: Build a micro sapi (used to build a standalone executable binary containing PHP code)--build-embed
: Build an embed sapi (used to embed into other C language programs)--build-all
: build all above sapi# Compile PHP with bcmath,curl,openssl,ftp,posix,pcntl extensions, the compilation target is cli
bin/spc build bcmath,curl,openssl,ftp,posix,pcntl --build-cli
# Compile PHP with phar,curl,posix,pcntl,tokenizer extensions, compile target is micro
@@ -54,7 +54,7 @@ import{_ as s,c as a,o as e,U as o}from"./chunks/framework.a73c7ff7.js";const m=
# Use ini file specification (-N is shorthand for --with-ini-file)
bin/spc micro:combine a.php -N /path/to/your/custom.ini
WARNING
Note, please do not directly use the PHP source code or the php.ini
file in the system-installed PHP, it is best to manually write an ini configuration file that you need, for example:
; custom.ini
curl.cainfo=/path/to/your/cafile.pem
-memory_limit=1G
The ini injection of this command is achieved by appending a special structure after micro.sfx, which is different from the function of inserting hard-coded INI during compilation.
Use the command bin/spc extract
to unpack and copy the source code required for compilation, including php-src and the source code of various dependent libraries (you need to specify the name of the library to be unpacked).
For example, after we have downloaded resources, we want to distribute and execute the build process, manually unpack and copy the package to a specified location, and we can use commands.
# Unzip the downloaded compressed package of php-src and libxml2, and store the decompressed source code in the source directory
+memory_limit=1G
The ini injection of this command is achieved by appending a special structure after micro.sfx, which is different from the function of inserting hard-coded INI during compilation.
Use the command bin/spc extract
to unpack and copy the source code required for compilation, including php-src and the source code of various dependent libraries (you need to specify the name of the library to be unpacked).
For example, after we have downloaded sources, we want to distribute and execute the build process, manually unpack and copy the package to a specified location, and we can use commands.
# Unzip the downloaded compressed package of php-src and libxml2, and store the decompressed source code in the source directory
bin/spc extract php-src,libxml2
Debug commands refer to a collection of commands that can assist in outputting some information when you use static-php-cli to build PHP or modify and enhance the static-php-cli project itself.
dev:extensions
: output all currently supported extension names, or output the specified extension informationdev:php-version
: output the currently compiled PHP version (by reading php_version.h
)dev:sort-config
: Sort the list of configuration files in the config/
directory in alphabetical order# output all extensions information
bin/spc dev:extensions
diff --git a/assets/en_guide_manual-build.md.99d1a681.lean.js b/assets/en_guide_manual-build.md.fc78940a.lean.js
similarity index 100%
rename from assets/en_guide_manual-build.md.99d1a681.lean.js
rename to assets/en_guide_manual-build.md.fc78940a.lean.js
diff --git a/en/contributing/index.html b/en/contributing/index.html
index e355f3ba..0c6c845d 100644
--- a/en/contributing/index.html
+++ b/en/contributing/index.html
@@ -16,7 +16,7 @@
-
diff --git a/en/develop/doctor-module.html b/en/develop/doctor-module.html
index ca126548..e2ec5772 100644
--- a/en/develop/doctor-module.html
+++ b/en/develop/doctor-module.html
@@ -44,7 +44,7 @@
}
return true;
}
#[AsFixItem()]
first parameter is the name of the fix item, and this method must return True or False. When False is returned, the automatic repair failed and manual handling is required.
In the code here, shell()->exec()
is the method of executing commands of the project, which is used to replace exec()
and system()
, and also provides debugging, obtaining execution status, entering directories, etc. characteristic.
Here will be some questions that you may encounter easily. There are currently many, but I need to take time to organize them.
Because the principle of installing extensions in PHP under the traditional architecture is to install new extensions using .so
type dynamic link libraries, and statically linked PHP compiled using this project cannot directly install new extensions using dynamic link libraries.
For the macOS platform, almost all binary files under macOS cannot be linked purely statically, and almost all binary files will link macOS system libraries: /usr/lib/libresolv.9.dylib
and /usr/lib/libSystem.B.dylib
. So under macOS system, statically compiled php binary files can be used under certain compilation conditions, and dynamic link extensions can be used at the same time:
--no-strip
parameter will not strip information such as debugging symbols from the binary file for use with external Zend extensions such as Xdebug
.phpize && ./configure && make
command to compile the extensions you want to use.xxxx.so
to the outside, use the statically compiled PHP binary, for example to use the Xdebug extension: cd buildroot/bin/ && ./php -d "zend_extension=/path/to/xdebug.so"
.# build statically linked php-cli but not stripped
bin/spc build ffi --build-cli --no-strip
For the Linux platform, the current compilation result is a purely statically linked binary file, and new extensions cannot be installed using a dynamic link library.
Some extensions that rely on closed source libraries, such as oci8
, sqlsrv
, sourceguardian
, etc., they do not provide purely statically compiled dependent library files (.a
), only dynamic dependent library files (.so
). These extensions cannot be compiled into static-php-cli from source, so this project may never support them. However, in theory, you can access and use such extensions under macOS according to the above questions.
If you have a need for such extensions, or most people have needs for these closed-source extensions, see the discussion on standalone-php-cli. Welcome to leave a message.
The project does not currently support Windows, because Windows is a new underlying architecture, not Unix. This project started on Linux, and the refactored version supports macOS.
Windows support mainly has the following issues:
You can't. micro.sfx is essentially combining php and php code into one file, there is no process of compiling or encrypting the PHP code.
First of all, php-src is the official interpreter of PHP code, and there is no PHP compiler compatible with mainstream branches on the market. I saw on the Internet that there is a project called BPC (Binary PHP Compiler?) that can compile PHP into binary, but there are many restrictions.
The direction of encrypting and protecting the code is not the same as compiling. After compiling, the code can also be obtained through reverse engineering and other methods. The real protection is still carried out by means of packing and encrypting the code.
Therefore, this project (static-php-cli) and related projects (lwmbs, swoole-cli) all provide a convenient compilation tool for php-src source code. The phpmicro referenced by this project and related projects is only a package of PHP's sapi interface, not a compilation tool for PHP code. The compiler for PHP code is a completely different project, so the extra cases are not taken into account. If you are interested in encryption, you can consider using existing encryption technologies, such as Swoole Compiler, Source Guardian, etc.
Build single static PHP binary, with PHP project together, with popular extensions included.
You can easily compile a standalone php binary for general use. Including CLI, FPM sapi.
You can compile a self-extracted executable and build with your php source code.
static-php-cli comes with dependency management and supports installation of different types of PHP extensions.
这里将会编写一些你容易遇到的问题。目前有很多,但是我需要花时间来整理一下。
因为传统架构下的 PHP 安装扩展的原理是使用 .so
类型的动态链接的库方式安装新扩展,而使用本项目编译的静态链接的 PHP 无法直接使用动态链接库安装新扩展。
对于 macOS 平台来说,macOS 下的几乎所有二进制文件都无法真正纯静态链接,几乎所有二进制文件都会链接 macOS 的系统库:/usr/lib/libresolv.9.dylib
和 /usr/lib/libSystem.B.dylib
。 所以在 macOS 系统下,在特定的编译条件下可以使用静态编译的 php 二进制文件,同时使用动态链接的扩展:
--no-strip
参数,将不会对二进制文件去除调试符号等信息,以供使用 Xdebug
等外部 Zend 扩展。phpize && ./configure && make
命令编译想要使用的扩展。xxxx.so
拷贝到外部,使用静态编译的 PHP 二进制,例如使用 Xdebug 扩展:cd buildroot/bin/ && ./php -d "zend_extension=/path/to/xdebug.so"
。# 构建静态 php-cli
bin/spc build ffi --build-cli --no-strip
对于 Linux 平台来说,目前的编译结果为纯静态链接的二进制文件,无法使用动态链接库安装新扩展。
部分依赖库闭源的扩展,如 oci8
、sqlsrv
,sourceguardian
等,它们没有提供纯静态编译的依赖库文件(.a
),仅提供了动态依赖库文件(.so
), 这些扩展无法使用源码的形式编译到 static-php-cli 中,所以本项目可能永远也不会支持这些扩展。不过,理论上你可以根据上面的问题在 macOS 下接入和使用这类扩展。
如果你对此类扩展有需求,或者大部分人都对这些闭源扩展使用有需求, 可以看看有关 standalone-php-cli 的讨论。欢迎留言。
该项目目前不支持 Windows,因为 Windows 是一套全新的底层架构,不是 Unix 架构。本项目起始于 Linux,重构的版本支持了 macOS。
Windows 的支持主要有以下几个问题:
不可以。micro.sfx 本质上是将 php 和 php 代码结合为一个文件,没有 PHP 代码编译或加密的过程。 首先 php-src 是 PHP 代码的官方解释器,而且现在市面上还没有一个能兼容主流分支的 PHP 编译器。 之前我在网上看到有一个项目是 BPC(Binary PHP Compiler?)可以把 PHP 编译为二进制,但是限制也是很多很多。
加密保护代码的方向和编译也不是一回事,编译过后也可以通过逆向工程等方式拿到代码,真正保护还是通过加壳、加密代码等手段进行。
所以本项目(static-php-cli)、相关项目(lwmbs、swoole-cli)都是提供一个对 php-src 源码的便捷编译工具, 本项目和相关项目引用的 phpmicro 也仅仅是 PHP 的 sapi 接口封装,而不是 PHP 代码的编译工具。 PHP 代码的编译器是完全不同的项目,因此不会考虑额外的情况。如果你对加密感兴趣,可以考虑使用现有的加密技术,如 Swoole Compiler、Source Guardian 等。
您可以轻松地编译一个独立的 PHP 二进制文件以供嵌入程序使用。包括 cli、fpm、micro。
您可以使用 micro SAPI 编译一个自解压的可执行文件,并将 PHP 代码与二进制文件打包为一个文件。
static-php-cli 附带依赖项管理,支持安装不同类型的 PHP 扩展和不同的依赖库。