brefphp / bref

Serverless PHP on AWS Lambda
https://bref.sh
MIT License
3.15k stars 365 forks source link

Deploy to Vercel (formerly ZEIT, Now) #368

Open f3l1x opened 5 years ago

f3l1x commented 5 years ago

Hello @mnapoli!

Good job with the whole Bref.sh thing! šŸ‘ I am working at this moment on Bref's integration into the ZEIT Now (https://zeit.co/now). It was really "easy" to provide the working solution.

Web demo: https://now-bref-f3l1x.juicyfx1.now.sh/ Web source: https://github.com/juicyfx/now-bref/tree/master/examples/bref Builder source: https://github.com/juicyfx/now-bref


I was able to deploy the web with 2 changes, here they come.

1. php-fpm.ini location

In the bootstrap file, there's the line $phpFpm = new PhpFpm($handler);.

https://github.com/brefphp/bref/blob/8e67dfd9263adf57fa34d98b0c06ae634275d63f/runtime/php/layers/fpm/bootstrap#L21

In the Now lambda, the location is quite different. It has all sources in the /var/task folder, so the default location of the php-fpm.ini is not /opt/bref/etc/php-fpm.conf. I must changed it. It's ok, but the the env variable would be more handy.

Something like...

    public function __construct(string $handler, string $configFile = null)
    {
        $this->handler = $handler;
        $this->configFile = getenv('BREF_PHP_FPM_INI') ?? self::CONFIG;
    }

2. Missing httpMethod

I'm always getting this exception The lambda was not invoked via HTTP through API Gateway: this is not supported by this runtime.

https://github.com/brefphp/bref/blob/8e67dfd9263adf57fa34d98b0c06ae634275d63f/src/Runtime/PhpFpm.php#L117-L119

Somehow the invocation of the lambda is little bit different at Now. I had to change the PhpFpm class or bootstrap, I've chosen change in the bootstrap at this time.

while (true) {
    $lambdaRuntime->processNextEvent(function ($event) use ($phpFpm): array {

        // Filling the httpMethod manually
        if (isset($event['body'])) {
            $body = @json_decode($event['body'], true);

            if (isset($body['method']) && ! isset($event['httpMethod'])) {
                $event['httpMethod'] = $body['method'];
            }
        }

        $response = $phpFpm->proxy($event);

        $multiHeader = array_key_exists('multiValueHeaders', $event);
        return $response->toApiGatewayFormat($multiHeader);
    });

    try {
        $phpFpm->ensureStillRunning();
    } catch (\Throwable $e) {
        echo $e->getMessage();
        exit(1);
    }
}

I don't know how to solve it in better way. Maybe you would have some ideas.


That's all, after that changes it works like charm. šŸ”„ Let me know, if I should prepare some PR or other way.

mnapoli commented 5 years ago

Thank you for opening this issue with all those details and starting the discussion about Zeit!

After looking into this, at this moment this might be a bit early to invest effort into supporting Now as a deployment target (judging by what you mention here and your repositories). First I want to see how much interest there is around this.

Just so it is more explicit: supporting Now requires some effort to setup and make it work (and @f3l1x did a lot of that work already), but then it requires effort to maintain it in the long run, as well as documentation, upgrades, fixes, etc.

So if you are interested in Now support please add a :+1: on the issue. If you are using Now in production with PHP (with or without Bref) please also post details as a comment, that could be very interesting to understand a bit better what can be done with Now and if it differs from AWS Lambda.

TheCrott commented 5 years ago

@mnapoli fyi, ZEIT Now listed as a "Developer Tool Buzz of a Year" -> https://noonies.hackernoon.com/award/cjxws7fcg0h990b06xlcbkxvj

So I think this is a proof that Now support is a good decision to make. And a lot of people waiting for more support to php builder, since official php builder not able to deploy project with composer.

f3l1x commented 5 years ago

@mnapoli Thanks for your attitude. I like it.


For the rest of you. At this time there is a working ZEIT Now build you can play with it. The minimal demo is here.

Here is an example: https://now-builders-php-bref-f3l1x.juicyfx1.now.sh/

File: now.json

{
    "version": 2,
    "builds": [
      { "src": "*.php", "use": "@juicyfx/php-bref@canary"}
    ]
}

File: composer.php

{
    "require": {
        "bref/bref": "^0.4.1"
    }
}

File: index.php

<?php 
phpinfo();
var_dump(opcache_get_status());

Take a look at the examples (https://github.com/juicyfx/now-builders/tree/master/examples)

fgilio commented 5 years ago

This is awesome šŸ¤Æ, I'll try to deploy a simple Laravel app with Bref to Now this weekend. Thanks @f3l1x!

f3l1x commented 5 years ago

Status update. šŸ™€

I've separated bref deployment to ZEIT into the solo repository - https://github.com/juicyfx/now-bref.

I will be happy if you guys can test it.

Example phpinfo looks is here - https://now-bref-f3l1x.juicyfx1.now.sh

f3l1x commented 3 years ago

Good news before end of 2020. šŸŽ‰

I've reworked previous version of @juicyfx/php-bref and made a whole new runtime vercel-bref following latest Vercel platform. Ofc, based on PHP 8.0.

Source code: https://github.com/juicyfx/vercel-bref

Demo: https://bref.vercel.app/

Screenshot 2020-12-31 at 17 52 41

How to test it? Just simple 4 steps.

File: vercel.json

{
    "version": 2,
    "builds": [
      { "src": "*.php", "use": "vercel-bref"}
    ]
}

File: composer.php

{
    "require": {
        "bref/bref": "^1.0"
    }
}

File: index.php

<?php 
phpinfo();
var_dump(opcache_get_status());

Deploy: call vercel from your terminal


https://user-images.githubusercontent.com/538058/103419444-68797c80-4b93-11eb-9d6d-f7ba8b2bbf8a.mp4

f3l1x commented 3 years ago

PHP 8.0.3 bumped.

image