W3docs

Convert a PHP script into a stand-alone windows executable

It is possible to convert a PHP script into a standalone Windows executable using tools such as PHPCompiler by Bambalam or PHPC.exe.

It is possible to package a PHP script and its dependencies into a single distributable file using modern tools such as Box. Box generates a PHAR archive, which can be executed on any machine with PHP installed. For a true standalone Windows executable (.exe), you will need to bundle the PHP runtime or use a wrapper.

To use Box, install it via Composer (composer global require box/box) or download the PHAR file. Once installed, follow these steps to package your PHP script:

  1. Open a terminal in your project directory.
  2. Run box init to generate a configuration file (box.json).
  3. Edit box.json to specify your main PHP script and the desired output PHAR filename.
  4. Run box compile to start the compilation process.

Once the compilation is complete, you will have a .phar file. You can distribute this file to users who have PHP installed, and run it with php your_script.phar. If you require a true standalone .exe that runs without PHP, you will need to bundle the PHP runtime or use a custom stub/wrapper.

Generate a PHAR archive

box compile --output="output_file.phar"

Replace "output_file.phar" with your desired output filename. This packages your PHP script and dependencies into a single PHAR archive.

Note on compatibility: Box generates a PHAR archive, not a native Windows executable. If you need a true standalone .exe that runs without PHP, consider bundling the PHP runtime or using a wrapper tool. Always verify that your dependencies match the target PHP version.

Create a Standalone Windows Executable (.exe)

To produce a native .exe that runs without requiring a separate PHP installation, use a dedicated compiler or bundler:

  1. Use PHPCompiler (PHPC.exe): Download the tool by Bambalam, point it to your PHP script or PHAR archive, and compile it. It embeds the PHP runtime and outputs a standalone .exe.
  2. Custom Stub with Box: Configure box.json to use a custom stub that includes the PHP DLLs. This requires manually bundling the PHP Windows binaries alongside your PHAR and adjusting the stub to locate them.
  3. Installer Packaging: Package your PHAR and the PHP Windows distribution into a single installer using tools like Inno Setup or NSIS. This creates a single .exe that installs PHP silently and runs your script.

Choose the method that best fits your distribution requirements. Always test the compiled executable on a clean Windows environment to verify runtime dependencies are correctly bundled.