Running PHP scripts on .NET Core

gpeipman
917 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Peachpie framework

Peachpie is attempt to get PHP scripts running on .NET Core with minimal effort of work by developers. To convince you there is reason to do it, take a look at Peachpie benchmarks. They have also sample WordPress project available to show that it is also possible to run on .NET COre the real things built on PHP.

Getting started

Run these commands on machine where .NET Core is installed.

mkdir MyWebSite1
dotnet new -i Peachpie.Templates::*
dotnet new peachpie-web
cd MyWebsite1.Server
dotnet restore
dotnet run

Now open browser and move to http://localhost:5000 page.

PHP pages are held MyWebsite1 project. This is referred from MyWebSite1.Server where code is built and run.

PHP script here is simple.

<?php

echo "Hello .NET Core!";

?>

Demo

This is how web server is initialized and run in Program.cs file of .NET Core application.

Click to run the .NET Core Web app.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Peachpie.Web;
namespace phpweb.Server
{
class Program
{
static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration();
.UseStartup<Startup>()
.Build();
host.Run();
}
}
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;
});
}
public void Configure(IApplicationBuilder app)
{
app.UseSession();
app.UsePhp(new PhpRequestOptions(scriptAssemblyName: "phpweb"));
app.UseDefaultFiles();
app.UseStaticFiles();
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

References

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content