Now Reading
Saying WCGI: WebAssembly + CGI

Saying WCGI: WebAssembly + CGI

2023-04-06 10:03:41

Welcome to the way forward for server-side improvement with WebAssembly!
Right this moment we’re introducing WCGI, a know-how that marries the facility of WebAssembly with the
versatility and ease of CGI.

Listed here are a few of WCGI’s highlights:

  • You possibly can reuse your current CGI purposes by compiling them to WASI
    (AssemblyScript, C, C++, Go, PHP, Python, …)
  • Ship ultra-small packages that can solely comprise your enterprise logic
    and static belongings, no HTTP stack or cumbersome Docker containers
  • Fully sandboxed execution: WebAssembly code runs in a sandbox, with one
    remoted occasion per request

Image working WordPress and never having to fret about attackers breaking into
your system. With WCGI, now this a actuality!

Strive WordPress domestically with Wasmer

# Set up wasmer beta 2
curl -sSfL | sh -s "v3.2.0-beta.2"

# Execute WordPress
mkdir db
wasmer run-unstable wasmer/wcgi-wordpress-demo --mapdir=/db:db

We’re trialing the brand new runner structure through wasmer run-unstable.
Sooner or later you’d merely use wasmer run

The Why Behind WCGI

When venturing into serverless options at Wasmer, we confronted a vital query:
ought to we create our personal framework and threat locking builders right into a walled
, or ought to we undertake an open commonplace that enables them to make the most of current

CGI’s alignment with the aim of executing a program per HTTP request makes it a
compelling selection. Apparently, CGI can outperform many different options (reminiscent of WSGI in Python/Ruby or NodeJS) by way of scalability and latency in serverless environments.

Moreover, it additionally intently mirrors the employees proposal from the Winter

Contemplate the problem of working PHP applications on servers. We now have two main

  1. Wrap the PHP interpreter with a layer that devices every HTTP name
  2. Use the prevailing php-cgi program and easily compile it to Wasm

Possibility 2 just isn’t solely sooner, nevertheless it additionally allows any internet software on Wasmer
extra effectively.

By embracing WCGI, these searching for to realize better effectivity, safety, and adaptability in server-side improvement can actually profit from this method.

Making a WCGI Utility with Rust

To create a WCGI software utilizing Rust, first add the cgi crate as a
dependency in your Cargo.toml file:

$ cargo add cgi

Subsequent, write your Rust server, like the instance beneath:

// src/

use cgi::{http::StatusCode, Request, Response};

fn important() {
    cgi::deal with(handler);

fn handler(request: Request) -> Response {
    let who = String::from_utf8_lossy(request.physique());
    let who = if who.trim().is_empty() {
    } else {

    cgi::text_response(StatusCode::OK, format!("Howdy, {who}!"))

After writing your implementation, compile it to the wasm32-wasi goal.
You might want to put in the WASI goal should you haven’t already (rustup goal add wasm32-wasi).

$ cargo construct --goal wasm32-wasi --launch

Create a wasmer.toml file that describes your bundle and comprises a WCGI
command that the wasmer CLI will execute.

title = "wasmer/wcgi-rust-template"
model = "0.1.0"
description = "A template for WCGI purposes"
license = "MIT OR Apache-2.0"
readme = ""
repository = ""

title = "server"
supply = "goal/wasm32-wasi/launch/wcgi-rust-template.wasm"
abi = "wasi"

title = "server"
runner = "wcgi"
module = "wcgi"
annotations = { wcgi = { dialect = "rfc-3875" } }

Now you can begin the server and browse to
http://localhost:8000/ to see it in motion:

$ wasmer run-unstable .

Making a WCGI Utility in PHP

PHP-CGI was first launched by Rafael Fernández López.
Because of his unique model of php-cgi compiled to WebAssembly & WASI,
Wasmer can now run PHP web sites utilizing WCGI.

First, create a brand new repository and replica
into it. Then, create an app/ listing and add some PHP code to it.

$ mkdir app
$ echo '<? print("Howdy, World!"); ?>' > app/index.php

Now we have to create a wasmer.toml file.

It is a bit longer than the Rust one as a result of we have to set some atmosphere
variables that inform php-cgi which script to invoke. We additionally need the app/
folder to be bundled with the bundle after we publish it.

title = "wasmer/wcgi-php-template"
model = "0.1.0"
description = "A PHP template for WCGI purposes"
license = "MIT OR Apache-2.0"
readme = ""
repository = ""

title = "php"
supply = "php-cgi.wasm"
abi = "wasi"

title = "php"
runner = "wcgi"
module = "php"

dialect = "rfc-3875"

atom = "php"
env = ["DOCUMENT_ROOT=/app", "SCRIPT_FILENAME=/app/index.php"]

"app" = "app"

Now you can run this with wasmer run-unstable:

$ wasmer run-unstable .

Opening up our internet browser reveals the “Howdy, World” message as anticipated.

PHP hello world

You too can use the wasmer CLI’s --mapdir flag to make particular directories
in your host machine obtainable to the WebAssembly software.

See Also

That is actually helpful throughout improvement as a result of it means you possibly can mount your
app/ listing and make adjustments on the fly with no need to restart the WCGI

First, begin the WCGI server and inform it that the /app listing contained in the
WebAssembly software corresponds to the ./app listing in your host.

$ wasmer run-unstable --mapdir /app:./app .

When you open http://localhost:8000/ up in your browser,
it is best to see the “Howdy, World!” from earlier than.

Subsequent, modify app/index.php to print phpinfo() , hit save, and refresh your

PHP phpinfo()

This bundle can be obtainable on WAPM.

$ wasmer run-unstable wasmer/wcgi-php-template

You’ll find in Github the supply code for the WordPress Wasmer pacakage, and the PHP WCGI template.

We will not wait to see what you create subsequent with WCGI!

In Abstract…

WCGI represents a refined method to server-side improvement, integrating the
flexibility, safety, and efficiency of WebAssembly. This modern
know-how has the potential to reshape the panorama of serverless
purposes, offering builders with a strong and versatile answer for
their tasks.

Watch this house ????

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top