Scott Hanselman

The whole of WordPress compiled to .NET Core and a NuGet Package with PeachPie

July 3, '18 Comments [17] Posted in DotNetCore
Sponsored By

Compiling WordPress to .NET CoreWhy? Because it's awesome. Sometimes a project comes along that is impossibly ambitious and it works. I've blogged a little about Peachpie, the open source PHP compiler that runs PHP under .NET Core. It's a project hosted at https://www.peachpie.io.

But...why? Here's why:

  1. Performance: compiled code is fast and also optimized by the .NET Just-in-Time Compiler for your actual system. Additionally, the .NET performance profiler may be used to resolve bottlenecks.
  2. C# Extensibility: plugin functionality can be implemented in a separate C# project and/or PHP plugins may use .NET libraries.
  3. Sourceless distribution: after the compilation, most of the source files are not needed.
  4. Power of .NET: Peachpie allows the compiled WordPress clone to run in a .NET JIT'ted, secure and manageable environment, updated through windows update.
  5. No need to install PHP: Peachpie is a modern compiler platform and runtime distributed as a dependency to your .NET project. It is downloaded automatically on demand as a NuGet package or it can be even deployed standalone together with the compiled application as its library dependency.

A year ago you could very happily run Wordpress (a very NON-trivial PHP application, to be clear) under .NET Core using Peachpie. You would compile your PHP into an assembly and then do something like this in your Startup.cs:

public void Configure(IApplicationBuilder app)
{
    app.UseSession();

    app.UsePhp(new PhpRequestOptions(scriptAssemblyName: "peachweb"));
    app.UseDefaultFiles();
    app.UseStaticFiles();
}

And that's awesome. However, I noticed something on their GitHub recently, specifically under https://github.com/iolevel/wpdotnet-sdk. It says:

The solution compiles all of WordPress into a .NET assembly and additionally provides C# wrappers for utilization of compiled sources.

Whoa. Drink that in. The project consists of several parts:

  • wordpress contains sources of WordPress that are compiled into a single .NET Core assembly (wordpress.dll). Together with its content files it is packed into a NuGet package PeachPied.WordPress. The project additionally contains the "must-use" plugin peachpie-api.php which exposes the WordPress API to .NET.
  • PeachPied.WordPress.Sdk defines abstraction layer providing .NET interfaces over PHP WordPress instance. The interface is implemented and provided by peachpie-api.php.
  • PeachPied.WordPress.AspNetCore is an ASP.NET Core request handler that configures the ASP.NET pipeline to pass requests to compiled WordPress scripts. The configuration includes response caching, short URL mapping, various .NET enhancements and the settings of WordPress database.
  • app project is the executable demo ASP.NET Core web server making use of compiled WordPress.

They compiled the whole of WordPress into a NuGet Package.

YES.

  • The compiled website runs on .NET Core
  • You're using ASP.NET Core request handling and you can extend WordPress with C# plugins and themes

Seriously. Go get the .NET Core SDK version 2.1.301 over at https://dot.net and clone their repository locally from https://github.com/iolevel/wpdotnet-sdk.

Make sure you have a copy of mySQL running. I get one started FAST with Docker using this command:

docker run --name=mysql1 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress mysql --default-authentication-plugin=mysql_native_password

Then just "dotnet build" at the root of the project, then go into the app folder and "dotnet run." It will show up on localhost:5004.

NOTE: I needed to include the default authentication method to prevent the generic Wordpress "Cannot establish database connection." I also added the MYSQL_DATABASE environment variable so I could avoid logging initially using the mysql client and creating the database manually with "CREATE DATABASE wordpress."

Look at that. I have my mySQL in one terminal listening on 3306, and ASP.NET Core 2.1 running on port 5004 hosting freaking WordPress compiled into a single NuGet package.

Wordpress under .NET Core

Here's my bin folder:

WordPress as a single DLL

There's no PHP files which is a nice security bonus - not only are you running from the one assembly but there's no text files for any rogue plugins to modify or corrupt.

Here's the ASP.NET Core 2.1 app that hosts it, in full:

using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using PeachPied.WordPress.AspNetCore;

namespace peachserver
{
    class Program
    {
        static void Main(string[] args)
        {
            // make sure cwd is not app\ but its parent:
            if (Path.GetFileName(Directory.GetCurrentDirectory()) == "app")
            {
                Directory.SetCurrentDirectory(Path.GetDirectoryName(Directory.GetCurrentDirectory()));
            }

            //
            var host = WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls("http://*:5004/")
                .Build();

            host.Run();
        }
    }

    class Startup
    {
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IConfiguration configuration)
        {
            // settings:
            var wpconfig = new WordPressConfig();
            configuration
                .GetSection("WordPress")
                .Bind(wpconfig);

            //
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseWordPress(wpconfig);

            app.UseDefaultFiles();
        }
    }
}

I think the app.UseWordPress() is such a nice touch. ;)

I often get emails from .NET developers asking what blog engine they should consider. Today, I think you should look closely at Peachpie and strongly consider running WordPress under .NET Core. It's a wonderful open source project that brings two fantastic ecosystems together! I'm looking forward to exploring this project more and I'd encourage you to check it out and get involved with Peachpie.


Sponsor: Check out dotMemory Unit, a free unit testing framework for fighting all kinds of memory issues in your code. Extend your unit testing with the functionality of a memory profiler!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Wednesday, 04 July 2018 07:40:33 UTC
Wow, we need a benchmark for this.

And... Who are the crazy guys behind that?
Wednesday, 04 July 2018 08:26:24 UTC
That's awesome! But can WP modify its files? People often use WP admin editor to tweak theme files, upload stuff, etc. Is that still possible? Also, im guessing auto-update will not works, since it needs to be recompiled into dll.
Wednesday, 04 July 2018 09:24:54 UTC
Thanks for sharing this. Awesome! Great to be working in the .Net space, exciting times :-)
Wednesday, 04 July 2018 11:38:58 UTC
Stupid question but... Can i install propreitery wordpress plugins (that I purchased) in the this .NET CORE version? Is it possible to utilize those php based plugins or do I have to compile them to .NET plugins?

Any advice will be appreciated. Thanks

TM
Steve Johnson
Wednesday, 04 July 2018 15:19:03 UTC
@Steve Johnsen they address this here: https://www.peachpie.io/2018/06/tutorial-plugins-wpdotnet.html
Jer0enH
Wednesday, 04 July 2018 17:35:09 UTC
@Steve @Hudo @Jer0enH

There is a lightweight demo project https://github.com/iolevel/peachpie-wordpress that uses that compiled WordPress - you can add your custom plugins and themes and just hit F5 in VSCode.

Use of the WP Dashboard to modify your sources is not allowed (and not gonna work), however; I think use of a regular code editor like VSCode gives you much better experience.
Wednesday, 04 July 2018 19:45:37 UTC
Sorry for the stupid question, but is this empty C# source code comment thing on lines 20 and 40 of the example "peachserver" code some new .NET Core coding convention I missed?
Steven Lathrop
Wednesday, 04 July 2018 20:01:47 UTC
That you cannot modify the files through WordPress directly is probably a good thing.

Most of the hacked WordPress sites I've seen are due to weak password on admin account. By weak, I mean crazy weak. Think 1234.

But then again, the people who will run WordPress through PeachPie are bound to have better password practices than that.
Thursday, 05 July 2018 12:42:04 UTC
Cool, though I'd not want to take over someone's critical commercial system written using this technology.
Ted
Thursday, 05 July 2018 13:04:37 UTC
That is really cool, but man it feels SO SLOW after using Orchard Core for so long now.
Thursday, 05 July 2018 14:29:20 UTC
@Peter: have you tried a setup of WP on .NET? Unless you're using some plugin that's causing issues, it should be pretty fast - the WP.NET SDK has the ASP.NET Core response caching built in, and sites load virtually instantly for us.
Friday, 06 July 2018 02:48:05 UTC
@Ben: Yeah was running it on localhost. Mainly was a comment to do with working in the editor being so slow. Site renders fine and snappy. The output response cache should make it almost as fast as Orchard Core renders without caching.
Friday, 06 July 2018 09:59:49 UTC
What about updates to core WP and/or to plugins? Do we have to download archives separately and rebuild the whole thing?
Friday, 06 July 2018 12:42:52 UTC
@Peter:
Gotcha. There are definitely some limitations to the CMS, albeit to be fair we haven't actually done many code optimizations, so we still have a bunch of room for improvement.

@David:
Plugins - yes, when you add a new plugin, you will have to recompile, but with the new SDK you will only be compiling the actual plugin, while the main WP app remains an already compiled NuGet reference.
Updates - actually one of the big advantages we can offer is that the constant WP updates can be performed seamlessly in the background from our feed of already compiled and CI-tested builds, so you won't have to worry about updates. Plus with .NET you don't really care about most of the security fixes, which is the bulk of why WP updates so often.
Friday, 06 July 2018 14:55:58 UTC
I hardly ever comment, or post, but I have to protest.
And I have huge respect for Mr. Hanselman.
At first, I thought, "oh, this is interesting".
I mean no disrespect to the obviously talented people that pulled this off.
But IMO, suggesting that .NET programmers should consider this, instead of one of the many .NET CMS or blog engine projects is just plain bad advice.
Bill
Tuesday, 10 July 2018 12:23:07 UTC
期待楼主的分享,赞赞。
Sunday, 15 July 2018 15:45:17 UTC
that was great, find more information about this in our website
vpn in uae
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.