import{_ as e,c as t,o as s,ah as a}from"./chunks/framework.Bhsyh9kO.js";const h=JSON.parse('{"title":"Build glibc Compatible Linux Binary","description":"","frontmatter":{},"headers":[],"relativePath":"en/guide/build-with-glibc.md","filePath":"en/guide/build-with-glibc.md"}'),n={name:"en/guide/build-with-glibc.md"};function l(o,i,r,c,u,b){return s(),t("div",null,[...i[0]||(i[0]=[a('
Currently, the binaries built by static-php-cli on Linux by default are based on musl-libc (statically linked). musl-libc is a lightweight libc implementation that aims to be compatible with glibc and provides good support for pure static linking. This means that the compiled static PHP executable can be used on almost any Linux distribution without worrying about the versions of libc, libstdc++, etc.
However, there are some issues with pure static linking of musl-libc binaries on Linux:
dl() function in PHP cannot be used to load dynamic libraries and external PHP extensions.Different Linux distributions use different default libc. For example, Alpine Linux uses musl libc, while most Linux distributions use glibc. However, even so, we cannot directly use any distribution using glibc to build portable static binaries because glibc has some issues:
However, we can use Docker to solve this problem. The final output is a binary dynamically linked with glibc and some necessary libraries, but statically linked with all other dependencies.
Using glibc static binaries can run on most modern Linux distributions but cannot run on musl libc distributions, such as CentOS 6, Alpine Linux, etc.
The latest version of static-php-cli includes the bin/spc-gnu-docker script, which can create a CentOS 7.x (glibc-2.17) Docker container with one click and build a glibc compatible PHP static binary in the container.
Then, run the following command once. The first run will take a long time because it needs to download the CentOS 7.x image and some build tools.
bin/spc-gnu-dockerAfter the image is built, you will see the same command help menu as bin/spc, which means the container is ready.
After the container is ready, you can refer to the local build section to build your PHP static binary. Just replace bin/spc or ./spc with bin/spc-gnu-docker.
bin/spc-gnu-docker build bcmath,ctype,openssl,pdo,phar,posix,session,tokenizer,xml,zip --build-cli --debugIn rare cases, glibc-based static PHP may encounter segment faults and other errors, but there are currently few examples. If you encounter any issues, please submit an issue.
glibc build is an extended feature and is not part of the default static-php support. If you have related issues or requirements, please indicate that you are building based on glibc when submitting an issue.
If you need to build glibc-based binaries without using Docker, please refer to the bin/spc-gnu-docker script to manually create a similar environment.
Please keep in mind that we only support glibc build with bin/spc-gnu-docker. Compilation on RHEL 9 & 10 has been tested and is stable, but if you run into issues, we may choose not to fix them.