Files
static-php-cli/assets/en_guide_first-build.md.C3LOd79t.js
2026-05-11 05:18:54 +00:00

30 lines
21 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.
import{_ as s,c as a,o as e,aj as t}from"./chunks/framework.C2AwuPrQ.js";const k=JSON.parse('{"title":"Your First Build","description":"","frontmatter":{},"headers":[],"relativePath":"en/guide/first-build.md","filePath":"en/guide/first-build.md"}'),n={name:"en/guide/first-build.md"};function l(h,i,d,p,o,r){return e(),a("div",null,[...i[0]||(i[0]=[t(`<h1 id="your-first-build" tabindex="-1">Your First Build <a class="header-anchor" href="#your-first-build" aria-label="Permalink to “Your First Build”"></a></h1><p>This page walks you through building a static PHP binary from scratch, end to end.</p><div class="tip custom-block"><p class="custom-block-title custom-block-title-default">TIP</p><p>If you installed spc as a pre-built binary, replace every <code>spc</code> in this page with <code>./spc</code> (or <code>.\\spc.exe</code> on Windows).</p><p>If you installed from source, use <code>bin/spc</code> instead.</p></div><h2 id="two-approaches" tabindex="-1">Two Approaches <a class="header-anchor" href="#two-approaches" aria-label="Permalink to “Two Approaches”"></a></h2><p>StaticPHP supports two build workflows — pick the one that fits your situation:</p><table tabindex="0"><thead><tr><th>Approach</th><th>When to use</th></tr></thead><tbody><tr><td><code>craft</code> (one-shot)</td><td>Everyday use, getting started quickly</td></tr><tr><td>Step-by-step</td><td>Fine-grained control over the build pipeline</td></tr></tbody></table><h2 id="option-1-one-shot-build-with-craft-recommended" tabindex="-1">Option 1: One-Shot Build with <code>craft</code> (Recommended) <a class="header-anchor" href="#option-1-one-shot-build-with-craft-recommended" aria-label="Permalink to “Option 1: One-Shot Build with craft (Recommended)”"></a></h2><p>The <code>craft</code> command reads a <code>craft.yml</code> file and handles everything automatically — downloading dependencies, compiling libraries, and building PHP — in a single run.</p><h3 id="write-craft-yml" tabindex="-1">Write craft.yml <a class="header-anchor" href="#write-craft-yml" aria-label="Permalink to “Write craft.yml”"></a></h3><p>Create a <code>craft.yml</code> in your working directory and declare the PHP version, extensions, and target SAPIs:</p><div class="language-yaml"><button title="Copy Code" class="copy"></button><span class="lang">yaml</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:#22863A;--shiki-dark:#85E89D;">php-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">8.5</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">extensions</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">bcmath,posix,phar,zlib,openssl,curl,fileinfo,tokenizer</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">sapi</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">cli</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">micro</span></span></code></pre></div><p>Not sure which extensions you need? Use the <a href="./cli-generator.html">command generator</a> to produce a <code>craft.yml</code> automatically.</p><h3 id="run-the-build" tabindex="-1">Run the Build <a class="header-anchor" href="#run-the-build" aria-label="Permalink to “Run the Build”"></a></h3><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;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> craft</span></span></code></pre></div><p>The build pipeline runs in order: download dependencies → compile libraries → compile PHP. No interaction required.</p><p>To see more detail, pass <code>-v</code>, <code>-vv</code>, or <code>-vvv</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;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> craft</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span></span></code></pre></div><h3 id="inspect-the-output" tabindex="-1">Inspect the Output <a class="header-anchor" href="#inspect-the-output" aria-label="Permalink to “Inspect the Output”"></a></h3><p>On success, binaries land in <code>buildroot/bin/</code>:</p><table tabindex="0"><thead><tr><th>SAPI</th><th>Output path</th></tr></thead><tbody><tr><td>cli</td><td><code>buildroot/bin/php</code> (Windows: <code>buildroot/bin/php.exe</code>)</td></tr><tr><td>fpm</td><td><code>buildroot/bin/php-fpm</code></td></tr><tr><td>micro</td><td><code>buildroot/bin/micro.sfx</code></td></tr><tr><td>embed</td><td><code>buildroot/lib/libphp.a</code></td></tr><tr><td>frankenphp</td><td><code>buildroot/bin/frankenphp</code></td></tr></tbody></table><p>Give the CLI binary a quick smoke-test:</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;">./buildroot/bin/php</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./buildroot/bin/php</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -m</span></span></code></pre></div><h2 id="option-2-step-by-step-build" tabindex="-1">Option 2: Step-by-Step Build <a class="header-anchor" href="#option-2-step-by-step-build" aria-label="Permalink to “Option 2: Step-by-Step Build”"></a></h2><p>This approach lets you run download and compile as separate steps — useful when you want to cache downloads in CI and reuse them across builds.</p><h3 id="step-1-download-dependencies" tabindex="-1">Step 1: Download Dependencies <a class="header-anchor" href="#step-1-download-dependencies" aria-label="Permalink to “Step 1: Download Dependencies”"></a></h3><p>In v3, you can skip this step entirely — running <code>build:php</code> directly will automatically download any missing artifacts.</p><p>If you want to pre-download ahead of time, or if you&#39;re working in a slow-network environment and want to separate the download phase, use the <code>download</code> command:</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:#6A737D;--shiki-dark:#6A737D;"># Download only what the chosen extensions need (recommended)</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> download</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --for-extensions=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;bcmath,posix,phar,zlib,openssl,curl,fileinfo,tokenizer&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --with-php=8.5</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Download by specific package names</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> download</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;curl,openssl&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --with-php=8.5</span></span></code></pre></div><p>Downloads are cached in <code>downloads/</code> and reused across builds automatically.</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:#6A737D;--shiki-dark:#6A737D;"># Slow connection? Increase parallelism and retries</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> download</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --for-extensions=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;bcmath,openssl,curl&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --parallel</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 10</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --retry=3</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Use pre-built binaries where available — skips compiling those dependencies</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> download</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --for-extensions=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;bcmath,openssl,curl&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --prefer-binary</span></span></code></pre></div><h3 id="step-2-build-php" tabindex="-1">Step 2: Build PHP <a class="header-anchor" href="#step-2-build-php" aria-label="Permalink to “Step 2: Build PHP”"></a></h3><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:#6A737D;--shiki-dark:#6A737D;"># Build the cli SAPI</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build:php</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;bcmath,phar,zlib,openssl,curl,fileinfo,tokenizer&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Build multiple SAPIs in one go</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build:php</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;bcmath,phar,zlib,openssl,curl&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-micro</span></span></code></pre></div><h4 id="common-build-options" tabindex="-1">Common Build Options <a class="header-anchor" href="#common-build-options" aria-label="Permalink to “Common Build Options”"></a></h4><table tabindex="0"><thead><tr><th>Option</th><th>Description</th></tr></thead><tbody><tr><td><code>--build-cli</code></td><td>Build the cli SAPI</td></tr><tr><td><code>--build-fpm</code></td><td>Build php-fpm (not available on Windows)</td></tr><tr><td><code>--build-micro</code></td><td>Build micro.sfx</td></tr><tr><td><code>--build-embed</code></td><td>Build the embed SAPI</td></tr><tr><td><code>--build-frankenphp</code></td><td>Build FrankenPHP</td></tr><tr><td><code>--enable-zts</code></td><td>Enable thread-safe (ZTS) mode</td></tr><tr><td><code>--no-strip</code></td><td>Keep debug symbols; do not strip the binary</td></tr><tr><td><code>-I key=value</code></td><td>Hard-compile an INI option into PHP</td></tr><tr><td><code>--with-upx-pack</code></td><td>Compress output with UPX (run <code>spc install-pkg upx</code> first)</td></tr></tbody></table><p>Example — baking in a larger memory limit and disabling the <code>system</code> function:</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;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build:php</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;bcmath,pcntl,posix&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -I</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;memory_limit=4G&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -I</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;disable_functions=system&quot;</span></span></code></pre></div><h2 id="packaging-a-micro-app" tabindex="-1">Packaging a micro App <a class="header-anchor" href="#packaging-a-micro-app" aria-label="Permalink to “Packaging a micro App”"></a></h2><p>Once you have <code>micro.sfx</code>, use <code>micro:combine</code> to bundle your PHP code into a single self-contained executable:</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:#005CC5;--shiki-dark:#79B8FF;">echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;&lt;?php echo &#39;Hello, World!&#39; . PHP_EOL;&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &gt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> hello.php</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> micro:combine</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> hello.php</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --output=hello</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./hello</span></span></code></pre></div><p>Works with <code>.phar</code> files too, and you can inject INI settings at packaging time:</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:#6A737D;--shiki-dark:#6A737D;"># Bundle a phar</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> micro:combine</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> your-app.phar</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --output=your-app</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Inject INI via command-line options</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> micro:combine</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> your-app.phar</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --output=your-app</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -I</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;memory_limit=512M&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Inject INI from a file</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> micro:combine</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> your-app.phar</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --output=your-app</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -N</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /path/to/custom.ini</span></span></code></pre></div><h2 id="debugging-and-rebuilding" tabindex="-1">Debugging and Rebuilding <a class="header-anchor" href="#debugging-and-rebuilding" aria-label="Permalink to “Debugging and Rebuilding”"></a></h2><p>If a build fails or you want to trace what&#39;s happening, use <code>-v</code> / <code>-vv</code> / <code>-vvv</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;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build:php</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;bcmath,openssl&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -vv</span></span></code></pre></div><ul><li><code>-v</code> shows <code>INFO</code>-level logs: which modules are running and what build commands are being executed.</li><li><code>-vv</code> shows <code>DEBUG</code>-level logs: all internal debug output from StaticPHP.</li><li><code>-vvv</code> shows <code>DEBUG</code>-level logs and also pipes the stdout of every shell command directly to your terminal.</li></ul><p>To wipe compiled artifacts and start fresh without re-downloading, run <code>reset</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;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> reset</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Then rebuild</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build:php</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;bcmath,openssl&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span></span></code></pre></div><div class="tip custom-block"><p class="custom-block-title custom-block-title-default">TIP</p><p><code>reset</code> only removes <code>buildroot/</code> and <code>source/</code>. Your <code>downloads/</code> cache is preserved. Add <code>--with-download</code> if you also want to clear the download cache.</p></div><p>If you&#39;re stuck, open an <a href="https://github.com/static-php/static-php-cli/issues" target="_blank" rel="noreferrer">Issue</a> and include your <code>craft.yml</code> (if any) and a zip of the <code>log/</code> directory.</p><h2 id="what-s-next" tabindex="-1">What&#39;s Next <a class="header-anchor" href="#what-s-next" aria-label="Permalink to “What&#39;s Next”"></a></h2><ul><li><a href="./sapi-reference.html">PHP SAPI Reference</a> — Build options and usage guide for each PHP SAPI</li><li><a href="./cli-reference.html">CLI Reference</a> — Full documentation for every command and option</li><li><a href="./extensions.html">Extensions</a> — Browse supported extensions and their dependencies</li><li><a href="./troubleshooting.html">Troubleshooting</a> — Diagnose common build failures</li></ul>`,51)])])}const u=s(n,[["render",l]]);export{k as __pageData,u as default};