To create a self-contained PHP executable, start with a console application, e.g.:
<?php echo "Hello world!";
<Project Sdk="Peachpie.NET.Sdk/0.9.42"> <PropertyGroup> <OutputType>exe</OutputType> <TargetFramework>netcoreapp2.1</TargetFramework> <StartupObject>main.php</StartupObject> </PropertyGroup> <ItemGroup> <Compile Include="**/*.php" /> </ItemGroup> </Project>
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-x64 for 64-bit Windows, or
dotnet publish -c release -r linux-x64 for 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 here.
dotnet-warpto merge DLL files and remove unnecessary IL and methods. More info can be found here.
Rename the project file so that it has the
.csproj extension, it will work just fine.