This post is about running PHP on .NET Core with Peachpie. Peachpie is an open source PHP Compiler to .NET. This innovative compiler allows you to run existing PHP applications with the performance, speed, security and interoperability of .NET.
In ASP.NET Community Standup July 25th, 2017, Jon Galloway announced Peachpie Compiler Platform is now part of .NET Foundation. In this post I am explaining how to run PHP in .NET Core.
First, you need to install the Peachpie .NET Templates, you can do this using the command -
dotnet new -i Peachpie.Templates::*.
Once installation completed, you can create a new project using
dotnet new peachpie-webcommand, similar to other project types, this command will creates all the files that the initial solution needs. Once created, you can open the index.php file from the inside folder and start adding code.
To run the project, navigate to the folder with the created web server. This is the entry point of the web application, working as a bootstrapper for your PHP website. Its name may differ – look up the folder with suffix .Server. And run the
dotnet restorecommand, this will restore all the required dependencies. Once all the dependencies successfully restored, you can execute
dotnet runcommand to start the server.
- You can navigate to http://localhost:5004 to view the helloworld text.
There is an VSCode extentsion available from the Peachpie team - Peachpie for Visual Studio Code, which is in Preview, it helps you to debug PHP code with VS Code. You can install it using
ext install peachpie-vscode command.
Note : When I tried to restore the dependencies first time. I got an error like this.
Then I had to comment out
Import Project element in the msbuildproj file to fix the problem. Here is the updated project file.
Happy Programming :)