static-php-cli/assets/zh_faq_index.md.DyNJg3PZ.js
2025-10-27 01:57:36 +00:00

5 lines
11 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import{_ as i,c as a,o,ah as s}from"./chunks/framework.Bhsyh9kO.js";const P=JSON.parse('{"title":"常见问题","description":"","frontmatter":{},"headers":[],"relativePath":"zh/faq/index.md","filePath":"zh/faq/index.md"}'),c={name:"zh/faq/index.md"};function p(d,e,l,n,r,h){return o(),a("div",null,[...e[0]||(e[0]=[s(`<h1 id="常见问题" tabindex="-1">常见问题 <a class="header-anchor" href="#常见问题" aria-label="Permalink to “常见问题”"></a></h1><p>这里将会编写一些你容易遇到的问题。目前有很多,但是我需要花时间来整理一下。</p><h2 id="php-ini-的路径是什么" tabindex="-1">php.ini 的路径是什么? <a class="header-anchor" href="#php-ini-的路径是什么" aria-label="Permalink to “php.ini 的路径是什么?”"></a></h2><p>在 Linux、macOS 和 FreeBSD 上,<code>php.ini</code> 的路径是 <code>/usr/local/etc/php/php.ini</code>。 在 Windows 中,路径是 <code>C:\\windows\\php.ini</code> 或 <code>php.exe</code> 所在的当前目录。 可以在 *nix 系统中使用手动构建选项 <code>--with-config-file-path</code> 来更改查找 <code>php.ini</code> 的目录。</p><p>此外,在 Linux、macOS 和 FreeBSD 上,<code>/usr/local/etc/php/conf.d</code> 目录中的 <code>.ini</code> 文件也会被加载。 在 Windows 中,该路径默认为空。 可以使用手动构建选项 <code>--with-config-file-scan-dir</code> 更改该目录。</p><p>PHP 默认也会从 <a href="https://www.php.net/manual/zh/configuration.file.php" target="_blank" rel="noreferrer">其他标准位置</a> 中搜索 <code>php.ini</code>。</p><h2 id="静态编译的-php-可以安装扩展吗" tabindex="-1">静态编译的 PHP 可以安装扩展吗? <a class="header-anchor" href="#静态编译的-php-可以安装扩展吗" aria-label="Permalink to “静态编译的 PHP 可以安装扩展吗?”"></a></h2><p>因为传统架构下的 PHP 安装扩展的原理是使用 <code>.so</code> 类型的动态链接的库方式安装新扩展,而使用本项目编译的静态链接的 PHP。但是静态链接在不同操作系统有不同的定义。</p><p>首先,对于 Linux 系统,静态链接的二进制文件不会链接系统的动态链接库。纯静态链接的二进制文件(<code>-all-static</code>)无法加载动态库,因此无法添加新扩展。 同时,在纯静态模式下,你也不能使用 <code>ffi</code> 等扩展来加载外部 <code>.so</code> 模块。</p><p>你可以使用命令 <code>ldd buildroot/bin/php</code> 来检查你在 Linux 下构建的二进制文件是否为纯静态链接。</p><p>如果你 <a href="./../guide/build-with-glibc.html">构建基于 GNU libc 的 PHP</a>,你可以使用 <code>ffi</code> 扩展来加载外部 <code>.so</code> 模块,并加载具有相同 ABI 的 <code>.so</code> 扩展。</p><p>例如,你可以使用以下命令构建一个与 glibc 动态链接的静态 PHP 二进制文件,支持 FFI 扩展并加载相同 PHP 版本和相同 TS 类型的 <code>xdebug.so</code> 扩展:</p><div class="language-bash"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/spc-gnu-docker</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> download</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --for-extensions=ffi,xml</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --with-php=8.4</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/spc-gnu-docker</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ffi,xml</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --debug</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">buildroot/bin/php</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;zend_extension=/path/to/php{PHP_VER}-{ts/nts}/xdebug.so&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --ri</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> xdebug</span></span></code></pre></div><p>对于 macOS 平台macOS 下的几乎所有二进制文件都无法真正纯静态链接,几乎所有二进制文件都会链接 macOS 系统库:<code>/usr/lib/libresolv.9.dylib</code> 和 <code>/usr/lib/libSystem.B.dylib</code>。 因此,在 macOS 上,你可以<strong>直接</strong>使用 SPC 构建具有动态链接扩展的静态编译 PHP 二进制文件:</p><ol><li>使用 <code>--build-shared=XXX</code> 选项构建共享扩展 <code>xxx.so</code>。例如:<code>bin/spc build bcmath,zlib --build-shared=xdebug --build-cli</code></li><li>你将获得 <code>buildroot/modules/xdebug.so</code> 和 <code>buildroot/bin/php</code>。</li><li><code>xdebug.so</code> 文件可用于版本和线程安全相同的 php。</li></ol><p>对于 Windows 平台,由于官方构建的扩展(如 <code>php_yaml.dll</code>)强制使用了 <code>php8.dll</code> 动态库作为链接,静态构建的 PHP 不包含任何系统库以外的动态库, 所以 Windows 下无法加载官方构建的动态扩展。 由于 static-php-cli 还暂未支持构建动态扩展,所以目前还没有让 static-php 加载动态扩展的方法。</p><p>不过Windows 可以正常使用 <code>FFI</code> 扩展加载其他的 dll 文件并调用。</p><h2 id="可以支持-oracle-数据库扩展吗" tabindex="-1">可以支持 Oracle 数据库扩展吗? <a class="header-anchor" href="#可以支持-oracle-数据库扩展吗" aria-label="Permalink to “可以支持 Oracle 数据库扩展吗?”"></a></h2><p>部分依赖库闭源的扩展,如 <code>oci8</code>、<code>sourceguardian</code> 等,它们没有提供纯静态编译的依赖库文件(<code>.a</code>),仅提供了动态依赖库文件(<code>.so</code> 这些扩展无法使用源码的形式编译到 static-php-cli 中,所以本项目可能永远也不会支持这些扩展。不过,理论上你可以根据上面的问题在 macOS 和 Linux 下接入和使用这类扩展。</p><p>如果你对此类扩展有需求,或者大部分人都对这些闭源扩展使用有需求, 可以看看有关 <a href="https://github.com/crazywhalecc/static-php-cli/discussions/58" target="_blank" rel="noreferrer">standalone-php-cli</a> 的讨论。欢迎留言。</p><h2 id="支持-windows-吗" tabindex="-1">支持 Windows 吗? <a class="header-anchor" href="#支持-windows-吗" aria-label="Permalink to “支持 Windows 吗?”"></a></h2><p>该项目目前支持 Windows但支持的扩展数量较少。Windows 支持并不完美。主要有以下问题:</p><ol><li>Windows 的编译过程与 *nix 不同,使用的工具链也不同。用于编译每个扩展依赖库的编译工具也几乎完全不同。</li><li>Windows 版本的需求也会根据所有使用本项目的人的需求推进。如果很多人需要,我会尽快支持相关扩展。</li></ol><h2 id="我可以使用-micro-保护我的源代码吗" tabindex="-1">我可以使用 micro 保护我的源代码吗? <a class="header-anchor" href="#我可以使用-micro-保护我的源代码吗" aria-label="Permalink to “我可以使用 micro 保护我的源代码吗?”"></a></h2><p>不可以。micro.sfx 本质上是将 php 和 php 代码合并为一个文件,没有编译或加密 PHP 代码的过程。</p><p>首先php-src 是 PHP 代码的官方解释器,市场上没有与主流分支兼容的 PHP 编译器。 我在网上看到一个名为 BPCBinary PHP Compiler的项目可以将 PHP 编译为二进制,但有很多限制。</p><p>加密和保护代码的方向与编译不同。编译后,也可以通过逆向工程等方法获得代码。真正的保护仍然通过打包和加密代码等手段进行。</p><p>因此本项目static-php-cli和相关项目lwmbs、swoole-cli都提供了 php-src 源代码的便捷编译工具。 本项目和相关项目引用的 phpmicro 只是 PHP 的 sapi 接口封装,而不是 PHP 代码的编译工具。 PHP 代码的编译器是一个完全不同的项目,因此不考虑额外的情况。 如果你对加密感兴趣,可以考虑使用现有的加密技术,如 Swoole Compiler、Source Guardian 等。</p><h2 id="无法使用-ssl" tabindex="-1">无法使用 ssl <a class="header-anchor" href="#无法使用-ssl" aria-label="Permalink to “无法使用 ssl”"></a></h2><p><strong>更新:该问题已在最新版本的 static-php-cli 中修复,现在默认读取系统的证书文件。如果你仍然遇到问题,请尝试下面的解决方案。</strong></p><p>使用 curl、pgsql 等请求 HTTPS 网站或建立 SSL 连接时,可能会出现 <code>error:80000002:system library::No such file or directory</code> 错误。 此错误是由于静态编译的 PHP 未通过 <code>php.ini</code> 指定 <code>openssl.cafile</code> 导致的。</p><p>你可以通过在使用 PHP 前指定 <code>php.ini</code> 并在 INI 中添加 <code>openssl.cafile=/path/to/your-cert.pem</code> 来解决此问题。</p><p>对于 Linux 系统,你可以从 curl 官方网站下载 <a href="https://curl.se/docs/caextract.html" target="_blank" rel="noreferrer">cacert.pem</a> 文件,也可以使用系统自带的证书文件。 有关不同发行版的证书位置,请参考 <a href="https://go.dev/src/crypto/x509/root_linux.go" target="_blank" rel="noreferrer">Golang 文档</a>。</p><blockquote><p>INI 配置 <code>openssl.cafile</code> 不能使用 <code>ini_set()</code> 函数动态设置,因为 <code>openssl.cafile</code> 是 <code>PHP_INI_SYSTEM</code> 类型的配置,只能在 <code>php.ini</code> 文件中设置。</p></blockquote><h2 id="为什么不支持旧版本的-php" tabindex="-1">为什么不支持旧版本的 PHP <a class="header-anchor" href="#为什么不支持旧版本的-php" aria-label="Permalink to “为什么不支持旧版本的 PHP”"></a></h2><p>因为旧版本的 PHP 有很多问题,如安全问题、性能问题和功能问题。此外,许多旧版本的 PHP 与最新的依赖库不兼容,这也是不支持旧版本 PHP 的原因之一。</p><p>你可以使用 static-php-cli 早期编译的旧版本,如 PHP 8.0,但不会明确支持早期版本。</p>`,37)])])}const b=i(c,[["render",p]]);export{P as __pageData,b as default};