<spanclass="line"><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> static-php-cli</span></span></code></pre></div><p>It is a bit strange that static-php-cli itself requires a PHP environment, but now you can quickly install the PHP environment through a script. Generally, your computer will not have the Windows version of PHP installed, so we recommend that you use <code>bin/setup-runtime</code> directly after downloading static-php-cli to install PHP and Composer in the current directory.</p><divclass="language-shell"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">shell</span><preclass="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><spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Install PHP and Composer to the ./runtime/ directory</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># After installation, if you need to use PHP and Composer in global commands, </span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># use the following command to add the runtime/ directory to PATH</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/setup-runtime</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -action</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> remove-path</span></span></code></pre></div><p>Finally, now that you have PHP and Composer installed, you need to install static-php-cli's Composer dependencies:</p><divclass="language-shell"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">shell</span><preclass="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><spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">composer</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span></span></code></pre></div><h3id="install-other-tools-automatic"tabindex="-1">Install other Tools (automatic) <aclass="header-anchor"href="#install-other-tools-automatic"aria-label="Permalink to “Install other Tools (automatic)”"></a></h3><p>For <code>php-sdk-binary-tools</code>, <code>strawberry-perl</code>, and <code>nasm</code>, we recommend that you directly use the command <code>bin/spc doctor</code> to check and install them.</p><p>If doctor successfully installs automatically, please <strong>skip</strong> the steps below to manually install the above tools.</p><p>But if the automatic installation fails, please refer to the manual installation method below.</p><h3id="install-php-sdk-binary-tools-manual"tabindex="-1">Install php-sdk-binary-tools (manual) <aclass="header-anchor"href="#install-php-sdk-binary-tools-manual"aria-label="Permalink to “Install php-sdk-binary-tools (manual)”"></a></h3><divclass="language-shell"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">shell</span><preclass="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><spanclass="line"><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> C:</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\s</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">pc-build</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\s</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">tatic-php-cli</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clone</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> https://github.com/php/php-sdk-binary-tools.git</span></span></code></pre></div><blockquote><p>You can also set the global variable <code>PHP_SDK_PATH</code> in Windows settings and clone the project to the path corresponding to the variable. Under normal circumstances, you don't need to change it.</p></blockquote><h3id="install-strawberry-perl-manual"tabindex="-1">Install strawberry-perl (manual) <aclass="header-anchor"href="#install-strawberry-perl-manual"aria-label="Permalink to “Install strawberry-perl (manual)”"></a></h3><blockquote><p>If you don't need to compile the openssl extension, you don't need to install perl.</p></blockquote><ol><li>Download the latest version of strawberry-perl from <ahref="https://github.com/StrawberryPerl/Perl-Dist-Strawberry/releases/"target="_blank"rel="noreferrer">GitHub</a>.</li><li>Install to the <code>C:\spc-build\static-php-cli\pkgroot\perl\</code> directory.</li></ol><blockquote><p>You can download the <code>-portable</code> version and extract it directly to the above directory. The last <code>perl.exe</code> should be located at <code>C:\spc-build\static-php-cli\pkgroot\perl\perl\bin\perl.exe</code>.</p></blockquote><h3id="install-nasm-manual"tabindex="-1">Install nasm (manual) <aclass="header-anchor"href="#install-nasm-manual"aria-label="Permalink to “Install nasm (manual)”"></a></h3><blockquote><p>If you don't need to compile openssl extension, you don't need to install nasm.</p></blockquote><ol><li>Download the nasm tool (x64) from <ahref="https://www.nasm.us/pub/nasm/releasebuilds/"target="_blank"rel="noreferrer">official website</a>.</li><li>Place <code>nasm.exe</code> and <code>ndisasm.exe</code> in the <code>C:\spc-build\static-php-cli\php-sdk-binary-tools\bin\</code> directory.</li></ol><h2id="download-required-sources"tabindex="-1">Download required sources <aclass="header-anchor"href="#download-required-sources"aria-label="Permalink to “Download required sources”"></a></h2><p>Same as <ahref="./manual-build.html#command-download">Manual build - Download</a></p><h2id="build-php"tabindex="-1">Build PHP <aclass="header-anchor"href="#build-php"aria-label="Permalink to “Build PHP”"></a></h2><p>Use the build command to start building the static php binary. Before executing the <code>bin/spc build</code> command, be sure to use the <code>download</code> command to download sources. It is recommended to use <code>doctor</code> to check the environment.</p><h3id="build-sapi"tabindex="-1">Build SAPI <aclass="header-anchor"href="#build-sapi"aria-label="Permalink to “Build SAPI”"></a></h3><p>You need to go to <ahref="./extensions.html">Extension List</a> or <ahref="./cli-generator.html">Command Generator</a> to select the extension you want to add, and then use the command <code>bin/spc build</code> to compile. You need to specify targets, choose from the following parameters (at least one):</p><ul><li><code>--build-cli</code>: Build a cli sapi (command line interface, which can execute PHP code on the command line)</li><li><code>--build-micro</code>: Build a micro sapi (used to build a standalone executable binary containing PHP code)</li></ul><divclass="language-shell"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">shell</span><preclass="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><spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Compile PHP with bcmath,openssl,zlib extensions, the compilation target is cli</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Compile PHP with phar,curl,posix,pcntl,tokenizer extensions, compile target is micro and cli</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/spc</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"bcmath,openssl,zlib"</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-micro</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span></span></code></pre></div><divclass="warning custom-block"><pclass="custom-block-title custom-block-title-default">WARNING</p><p>In Windows, it is best to use double quotes to wrap parameters containing commas, such as <code>"bcmath,openssl,mbstring"</code>.</p></div><h3id="debug"tabindex="-1">Debug <aclass="header-anchor"href="#debug"aria-label="Permalink to “Debug”"></a></h3><p>If you encounter problems during the compilation process, or want to view each executing shell command, you can use <code>--debug</code> to enable debug mode and view all terminal logs:</p><divclass="language-shell"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">shell</span><preclass="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><spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/spc</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"openssl"</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --debug</span></span></code></pre></div><h3id="build-options"tabindex="-1">Build Options <aclass="header-anchor"href="#build-options"aria-label="Permalink to “Build Options”"></a></h3><p>During the compilation process, in some special cases, the compiler and the content of the compilation directory need to be intervened. You can try to use the following commands:</p><ul><li><code>--with-clean</code>: clean up old make files before compiling PHP</li><li><code>--enable-zts</code>: Make compiled PHP thread-safe version (default is NTS version)</li><li><code>--with-libs=XXX,YYY</code>: Compile the specified dependent library before compiling PHP, and activate some extension optional functions</li><li><code>--with-config-file-scan-dir=XXX</code>: Set the directory to scan for <code>.ini</code> files after reading <code>php.ini</code> (Check <ahref="./../faq/index.html#what-is-the-path-of-php-ini">here</a> for default paths)</li><li><code>-I xxx=yyy</code>: Hard compile INI options into PHP before compiling (support multiple options, alias is <code>--with-hardcoded-ini</code>)</li><li><code>--with-micro-fake-cli</code>: When compiling micro, let micro's <code>PHP_SAPI</code> pretend to be <code>cli</code> (for compatibility with some programs that check <code>PHP_SAPI</code>)</li><li><code>--disable-opcache-jit</code>: Disable opcache jit (enabled by default)</li><li><code>--without-micro-ext-test</code>: After building micro.sfx, do not test the running results of different extensions in micro.sfx</li><li><code>--with-suggested-exts</code>: Add <code>ext-suggests</code> as dependencies when compiling</li><li><code>--with-suggested-libs</code>: Add <code>lib-suggests</code> as dependencies when compiling</li><li><code>--with-upx-pack</code>: Use UPX to reduce the size of the binary file after compilation (you need to use <code>bin/spc install-pkg upx</code> to install upx first)</li><li><code>--with-micro-logo=XXX.ico</code>: Customize the icon of the <code>exe</code> executable file after customizing the micro build (in the format of <code>.ico</code>)</li></ul><p>Here is a simple example where we preset a larger <code>memory_limit</code> and disable the <code>system</code> function:</p><divclass="language-shell"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">shell</span><preclass="shiki shiki-themes github-light github-dark"style="-
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">my-app.exe</span></span></code></pre></div><h2id="use-php-exe"tabindex="-1">Use php.exe <aclass="header-anchor"href="#use-php-exe"aria-label="Permalink to “Use php.exe”"></a></h2><p>After php.exe is compiled, it is located in the <code>buildroot\bin\</code> directory. You can copy it to any location for use.</p><divclass="language-shell"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">shell</span><preclass="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><spanclass="line"><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">.</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">\</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">php</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span></span></code></pre></div><h2id="use-micro-sfx"tabindex="-1">Use micro.sfx <aclass="header-anchor"href="#use-micro-sfx"aria-label="Permalink to “Use micro.sfx”"></a></h2><blockquote><p>phpmicro is a SelF-extracted eXecutable SAPI module, provided by <ahref="https://github.com/dixyes/phpmicro"target="_blank"rel="noreferrer">phpmicro</a> project. But this project is using a <ahref="https://github.com/static-php/phpmicro"target="_blank"rel="noreferrer">fork</a> of phpmicro, because we need to add some features to it. It can put php runtime and your source code together.</p></blockquote><p>The final compilation result will output a file named <code>./micro.sfx</code>, which needs to be used with your PHP source code like <code>code.php</code>. This file will be located in the path <code>buildroot/bin/micro.sfx</code>.</p><p>Prepare your project source code, which can be a single PHP file or a Phar file, for use.</p><blockquote><p>If you want to combine phar files, you must add <code>phar</code> extension when compiling!</p></blockquote><divclass="language-shell"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">shell</span><preclass="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><spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># code.php "<?php echo 'Hello world' . PHP_EOL;"</span></span>