DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Build a Simple Chat Server With gRPC in .Net Core
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Building Cross-Platform Apps: A Complete Guide With .NET Core
  • PHP vs React

Trending

  • A Complete Guide to Modern AI Developer Tools
  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  • Start Coding With Google Cloud Workstations
  • Automatic Code Transformation With OpenRewrite
  1. DZone
  2. Coding
  3. Languages
  4. Running PHP Applications on .NET Core

Running PHP Applications on .NET Core

Curious about running PHP applications using .NET Core as its platform? In this post we take a look at how you can make this happen!

By 
Gunnar Peipman user avatar
Gunnar Peipman
·
Aug. 03, 17 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
14.4K Views

Join the DZone community and get the full member experience.

Join For Free

ever wondered about mixing up the simplicity and flexibility of php with the power and performance of .net core? the solution is coming and it is called peachpie, the successor of phalanger. this blog post introduces peachpie – the tool to compile php code to .net core – and helps you to get started with it.

peachpie

why peachpie?

for years there was a thing called phalanger that lived a silent life somewhere on the internet. at least it didn’t make its way to my work desk. now, years later, it is reincarnated as peachpie and now it looks really promising. peachpie allows us to build and run php code on .net core . for example, they made wordpress run on .net core (fairly successfully).

important! peachpie is not about writing .net code using php. peachpie is about running php code on .net.

is there any point to a solution like this business wise? it looks very promising if full php compilation starts working in the near future. the question is not the benefits of using peachpie and visual studio tooling. the question is what we get when building php code in .net core and run it as compiled code. the answer is simple: take a look at peachpie benchmarks !

getting started

peachpie is easy to install. all we need is the .net utility that comes with .net core. open your command line and run the following commands:


mkdir mywebsite1
dotnet new -i peachpie.templates::*
dotnet new peachpie-web
cd mywebsite1.server
dotnet restore
dotnet run

if ms build reports errors then open the project file in your mywebsite1 folder (not mywebsite1.server) and comment out the following line at the end of the file.


<import project="$(csharpdesigntimetargetspath)" />

after this, the application should build and run without errors. this is how a peachpie solution is created, built, and run fully on the command line.

peachpie solution

peachpie solution in visual studio when running with .net utility, peachpie creates a solution that can be opened in visual studio. there are two projects (i created a solution folder called 'phpnet' and this is why my solution is named as phpnet):

  • phpnet – this project hosts all php files that make up my php solution.
  • phpnet.server – this project is for building and running my php solution.

the program.cs file is interesting one. it contains program and startup classes, as both classes are very small in this sample application. an assembly of code built with php files is included very nicely in the configure() method of the startup class.


public void configure(iapplicationbuilder app)
{
    app.usesession();       

    app.usephp(new phprequestoptions(scriptassemblyname: "phpnet"));
    app.usedefaultfiles();
    app.usestaticfiles();
}

those of my readers who come up with serious questions at conferences want to ask: what happens when i add mvc to the solution?

adding asp.net core mvc to php solution

although i don’t see much point in doing this, we can actually use asp.net core mvc the same was we used php to get a solution. as nuget has issues with adding and updating packages in php solutions, i had to modify my phpnet.server project file manually. this is what the nuget packages section looks like in my project file.


<itemgroup>
  <packagereference include="microsoft.aspnetcore.server.kestrel" version="1.1.2" />
  <packagereference include="microsoft.aspnetcore.session" version="1.1.2" />
  <packagereference include="microsoft.aspnetcore.staticfiles" version="1.1.2" />
  <packagereference include="microsoft.aspnetcore.mvc" version="1.1.3" />
  <packagereference include="microsoft.extensions.caching.abstractions" version="1.1.2" />
  <packagereference include="microsoft.extensions.caching.memory" version="1.1.2" />
  <packagereference include="peachpie.netcore.web" version="0.7.0-*" />
</itemgroup>

i restored packages from the command line after the change and got no errors. after this, i added mvc to the request pipeline.


class startup
{
    public void configureservices(iservicecollection services)
    {
        // adds a default in-memory implementation of idistributedcache.
        services.adddistributedmemorycache();           services.addsession(options =>
        {
            options.idletimeout = timespan.fromminutes(30);
            options.cookiehttponly = true;
        });           services.addmvc();
    }       public void configure(iapplicationbuilder app)
    {
        app.usesession();           app.usephp(new phprequestoptions(scriptassemblyname: "phpnet"));
        app.usemvc(routes =>
        {
            routes.maproute(
                name: "default",
                template: "{controller=home}/{action=index}/{id?}");
        });
        app.usedefaultfiles();
        app.usestaticfiles();
    }
}

i built the solution and ran it. the following screenshot shows the results.

peachpie: php and asp.net core mvc in same application

this good conference crowd wants to know now one more thing. what happens when mvc is added to the request pipeline before php? it can be easily done, and, in this case, mvc processes the request first. if the application root is requested, then “hello from asp.net!” is shown. when index.php is requested, then “hello from php” is shown. so, peachpie also knows about the php file names that were used in the php solution.

i didn’t find much information about .net core and php interoperability and therefore i don’t know to what point it is currently possible. i’m sure they will provide interoperability in the future, as it opens one new option: handing execution over to .net code in some points of the code.

decompiling compiled php code

using dotpeek by jetbrains , it is possible to decompile .net executables. i opened my phpnet.dll file from the bin folder of my web application and decompiled the index_php class. this is what the compiler produced from my php.

peachpie: php code compiled to .net

using dotpeek we can also see il code of this class.

peachpie: compiled php code in il

using dotpeek, it is also possible to explore the contents of other libraries.

wordpress example

peachpie uses their version of wordpress as an example of php compilation to .net. here is how to set it up.

  1. download the project from https://github.com/iolevel/peachpie-wordpress
  2. extract files to some folder.
  3. install mysql if you don’t have one to use and run it.
  4. change database connection settings in wp-config.php.
  5. open the command line and go to the folder where you copied wordpress project files.
  6. run the following commands: dotnet restorecd appdotnet run .
  7. wait a few minutes until the application is compiled and started.
  8. open http://localhost:5004 in your favorite web browser.

if everything went okay and there are no unexpected issues you should be able to see wordpress running in your browser. don’t be surprised if you face problems – this is on-going work you are playing with.

wrapping up

peachpie is an interesting project. php code is translated and compiled to .net core and then run on .net runtime. although the project is in alpha right now it looks very promising. especially when you look at current benchmarks where it is compared to zend. it would be a good fit for cloud environments where php applications get high work loads by customers, for example. it is not very clear from the documentation how we can bridge php and .net core code but i hope that peachpie offers something for this in future. i think it is worth keeping an eye on the peachpie project and to try out what benefits it gives to the php applications we currently build and manage.

resources

  • peachpie home page
  • peachpie benchmarks
  • peachpie @ github
  • peachpie wiki
  • peachpie for visual studio code
  • wordpress on peachpie
  • peachpie – open source php compiler to .net and wordpress under asp.net core (scott hanselman)
Web application .NET PHP code style ASP.NET Core

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Build a Simple Chat Server With gRPC in .Net Core
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Building Cross-Platform Apps: A Complete Guide With .NET Core
  • PHP vs React

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!