To create a self-contained PHP executable, start with a console application, e.g.:
<?php echo "Hello world!";
and a corresponding project file indicating that the output should be exe:
<Project Sdk="Peachpie.NET.Sdk"> <PropertyGroup> <OutputType>exe</OutputType> <TargetFramework>netcoreapp3.0</TargetFramework> <StartupObject>main.php</StartupObject> </PropertyGroup> <ItemGroup> <Compile Include="**/*.php" /> </ItemGroup> </Project>
Now compile your project into binaries using the following command:
dotnet publish -c release
This will publish the resulting binaries into the
As the final step, select an operating system to target, e.g.
dotnet publish -c release -r win-x64for 64-bit Windows, or
dotnet publish -c release -r linux-x64for Linux.
This creates a self-contained executable PHP application targeting the operating system of your choice.
A few tips on how to minimize the size of the resulting application:
- On Linux, you can get rid of culture-specific functionalities and keep just the “Invariant” culture features (cuts about 26MB of data). More detail at globalization-invariant-mode.md.
dotnet-warpto merge DLL files and remove unnecessary IL and methods. More info can be found at hanselman.com.
Rename the project file so that it has the
.csproj extension, it will work just fine.