Will Cloudflare support a PHP runtime for workers at the edge?

This is going to sound a bit nuts, but I don’t lose anything asking this question!

At present, workers are written in JS and run in a V8 isolate container, etc.

Does Cloudflare plan to enable workers to run/execute a PHP runtime at the edge?

There’s a mega-ton of code out there executing PHP, and re-writing it is not possible.

Maybe there’s a way to transpile it or similar - and if so, ideas would be welcome!

I realize that caching would be a far easier solution - but running this PHP has to be done on-the-fly.

There’s two layers that I’m trying to move to the edge - the execution of PHP, and the database itself. The database is a totally different problem (a distributed/ACID RDBMS is not easy) - so I thought I’d start by thinking about the EC2/compute piece of it.

Amit (Tallyfy)

I might be able to answer my own question as a workaround for now.

If there’s EC2 instances that are globally distributed, the easy solution would be to route requests from certain origin countries to their nearest known EC2 service nodes e.g. a US request would serve from a US-based AWS region.

This leaves the ACID RDBMS to mull over e.g. cloud spanner, etc.

I imagine supporting a new language could be problematic since the current security model of workers is built around V8’s isolation, and supporting PHP would probably require Docker which would introduce cold start issues. See

But, I believe it should be possible using webassembly. I’m no expert, so just going to reference these blog posts:

https://blog.Cloudflare.com/webassembly-on-Cloudflare-workers/

https://blog.Cloudflare.com/Cloudflare-workers-as-a-serverless-rust-platform/

Also CC @KentonVarda

4 Likes

Yeah, a PHP runtime in WebAssembly would probably be able to do this pretty effectively.

1 Like

@ExE-Boss @Judge thanks for the pointers to webassembly! Do you know any such efforts so far, specifically for execution of PHP in CF workers? I’m finding it difficult to start somewhere.

Likely some real low-level stuff still needed to get started I’m afraid, Web assembly in itself is very new (2017? based on the webassembly GitHub) so there is limited documentation as well as little pre-existing code.

Some resources that might help:

One last ping (i promise!) @KentonVarda

If anyone else would like to share, feel free!

2 Likes

That was supposed to be for @KentonVarda, right? The Cloudflare @KentonVarda? Yeah, @KentonVarda might be a good idea here.

:roll_eyes: :thinking: :smile: :innocent: :crazy_face:

2 Likes

We don’t plan to provide any runtimes other than JavaScript+WebAssembly, because essentially all other language runtimes require process-level isolation (e.g. containers or VMs), which is too inefficient to allow us to distribute your code to all our 165+ locations at once.

In theory, you could indeed run PHP inside WebAssembly. However, there is a lot that would need to be done there. The PHP runtime would need to be ported to WebAssembly, which is no small task (it’s like porting to a new operating system). Additionally, PHP is a dynamic, garbage-collected language. WebAssembly as it exists today works well for classic C, C++, and Rust applications, but poses some big challenges to languages that rely on JIT compilation or garbage collection. The WebAssembly community is working to address these issues, but it will take time. I expect this is a few years away from being a reality, unfortunately.

Ironically, it may actually be more feasible to transpile PHP to JavaScript, and indeed there appear to be some tools on the internet that do this. However, I don’t know how practical and robust they are; you’ll have to experiment.

4 Likes

Just found this ( Kornel / babel-preset-php · GitLab ) and think it’s relevant to this convo.

Hope it’s useful to someone.

Thanks - appreciate this! We use Laravel as a framework. Have you seen/done a transpile using Laravel, via babel-preset-php?

Most welcome!

I’m sorry to report that I have not yet done this kind of work!

There’s a wrangler template using babel-preset-php linked from Languages · Cloudflare Workers docs

1 Like

Found that! Forwarded to a colleague! Thanks!!!

Hi there, can you please elaborate more on that!

Hi there, can you please give more guideline on that at [email protected]