Static compilation of PHP and dependent extensions

Time:2021-9-19

One day, when I was writing a framework using swoole, I realized such a problem. Although like the swoole extension, many PHP extensions can provide some features to facilitate developers’ development and use, they also caused some difficulties in deployment and distribution: not every Linux host and distribution version has a PHP environment, even if there is a PHP environment, You may not have the extensions written in C language you need. So why don’t we compile PHP directly as a static link like a go language program?

Of course, the current research stage is limited to compiling PHP parser ontology into static binary files, and has not studied how to compile PHP code.

So there was a toss for more than a month. Finally, it was successfully compiled on the alpine distribution. In fact, if PHP itself does not bring any extensions, it is--disable-allIt is very simple to compile statically, but the soul of PHP is a pile of easy-to-use extensions. There is no meaning without extensions.

The research ideas of this static compilation are as follows:

  1. Try to use the package management of the system to install dependent packages. If not, compile them manually.
  2. When compiling the extension, download it directly from PECL to keep the version updated.
  3. Abstract and simplify the compilation process as much as possible.
  4. Automatically download and compile all content.

However, finally, in order to facilitate most users to compile Linux static binary under different operating systems, I changed the script to run in docker, so that an old MAC party like me can adjust the script more comfortably.

Finally, attach the script open source address:
https://github.com/crazywhale…