Running PHP on .NET Core with Peachpie

July 26, 2017 by Anuraj

ASP.NET Core PHP Peachpie

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.

  1. First, you need to install the Peachpie .NET Templates, you can do this using the command - dotnet new -i Peachpie.Templates::*.

  2. Once installation completed, you can create a new project using dotnet new peachpie-web command, 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.

  3. 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 restore command, this will restore all the required dependencies. Once all the dependencies successfully restored, you can execute dotnet run command to start the server.

PHP running on ASP.NET Core

  1. 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.

Peachpie for Visual Studio Code

Note : When I tried to restore the dependencies first time. I got an error like this.

dotnet restore error

Then I had to comment out Import Project element in the msbuildproj file to fix the problem. Here is the updated project file.

<!-- A temporary solution, import C# Visual Studio design time targets in order to be able to load the project in Visual Studio -->
  <Import Project="$(CSharpDesignTimeTargetsPath)" />

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub