micro self-executable SAPI makes PHP self-executable.
Just concatenate micro.sfx and random php source file or phar into a single file to use it.
Yet only support PHP8+; Windows, Linux, macOS.
There's a micro micro.sfx binary contains minimal extensions set builds automatically in Github actions. If you need extensions, build your own micro or use crazywhalecc/static-php-cli
Just concatenate micro.sfx and php source to use it.
For example: if the content of myawesomeapp.php is
<?php
echo "hello, this is my awesome app." . PHP_EOL;
at Linux/macOS:
Note: If you downloaded micro.sfx and macOS does not let you execute it, try:
sudo xattr -d com.apple.quarantine /path/to/micro.sfx
then
cat /path/to/micro.sfx myawesomeapp.php > myawesomeapp
chmod 0755 ./myawesomeapp
./myawesomeapp
# show "hello, this is my awesome app."
or Windows:
COPY /b \path\to\micro.sfx + myawesomeapp.php myawesomeapp.exe
myawesomeapp.exe
REM show "hello, this is my awesome app."
1.Clone this repo into sapi/micro under PHP source
# at PHP source dir
git clone <url for this repo> sapi/micro
2.Apply patches
Patches are located at patches directory, choose patch(es) as you like, see Readme.md in patches dir for detail
Apply patch:
# at PHP source dir
patch -p1 < sapi/micro/patches/<name of patch>
0.Prepare build environment according to official PHP documents.
1.buildconf
# at PHP source dir
./buildconf --force
2.configure
# at PHP source dir
./configure <options>
Options for reference:
--disable-phpdbg --disable-cgi --disable-cli --disable-all --enable-micro --enable-phar --with-ffi --enable-zlib
At Linux libc compatibility can be a problem, micro provides two kinds of configure
argument:
--enable-micro=yes
or--enable-micro
: this will make PIE shared ELF micro sfx, this kind of binary cannot be invoked cross libc (i.e. you cannot run such a binary which built on alpine with musl on any glibc-based CentOS), but the binary can do ffi and PHPdl()
function.--enable-micro=all-static
: this will make static ELF micro sfx, this kind of binary can even run barely on the top of the kernel, but ffi/dl()
is not supported.
3.make
# at PHP source dir
make micro
(make all
(aka. make
) may work also, but only build micro SAPI -s recommended.
That built file is located at sapi/micro/micro.sfx.
0.Prepare build environment according to official PHP documents.
1.buildconf
# at PHP source dir
buildconf
2.configure
# at PHP source dir
configure <options>
Options for reference:
--disable-all --disable-zts --enable-micro --enable-phar --with-ffi --enable-zlib
3.make
Due to PHP build system on Windows lack of ability to statically build PHP binary, you cannot build micro with nmake
# at PHP source dir
nmake micro
That built file is located at <arch name like x64>\\<configuration like Release>\\micro.sfx
.
Hugepages optimization for Linux in PHP build system insults huge size of sfx, if you do not take advantage of hugepages, use disable_huge_page.patch to shrink sfx size.
Statically build under Linux needs libc, most common glibc may be large, musl is recommended hence. manually installed musl or some distros provided musl will provide musl-gcc
or musl-clang
wrapper, use one of them before configure by specify CC/CXX environ, for example
# ./buildconf things...
export CC=musl-gcc
export CXX=musl-gcc
# ./configure balabala
# make balabala
We hope all dependencies are statically linked into sfx, however, some distro do not provide static version of them, we may manually build them, the case of libffi (ffi extension is not supported in all-static
builds):
# fetch sources througe git
git clone https://github.com/libffi/libffi
cd libffi
git checkout <version you like, v3.3 for example>
autoreconf -i
# or download tarball
wget <url>
tar xf <tar name>
cd <extracted name>
# if we use musl
export CC=musl-gcc
export CXX=musl-gcc
# build, install it
./configure --prefix=/my/prefered/path &&
make -j`nproc` &&
make install
then build micro as
# ./buildconf things...
# export CC=musl-xxx things...
export PKG_CONFIG_PATH=/my/prefered/path/lib/pkgconfig
# ./configure balabala
# make balabala
See wiki:INI-settings(TODO: en version)
In micro, PHP_BINARY
is an empty string, you can use an ini setting to modify it: micro.php_binary=somestring
Copyright 2020 Longyan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.