Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy to Vercel (formerly ZEIT, Now) #368

Open
f3l1x opened this issue Jul 7, 2019 · 7 comments
Open

Deploy to Vercel (formerly ZEIT, Now) #368

f3l1x opened this issue Jul 7, 2019 · 7 comments

Comments

@f3l1x
Copy link

f3l1x commented Jul 7, 2019

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);.

$phpFpm = new PhpFpm($handler);

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.

bref/src/Runtime/PhpFpm.php

Lines 117 to 119 in 8e67dfd

if (! isset($event['httpMethod'])) {
throw new \Exception('The lambda was not invoked via HTTP through API Gateway: this is not supported by this runtime');
}

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
Copy link
Member

mnapoli commented Jul 18, 2019

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 👍 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
Copy link

@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
Copy link
Author

f3l1x commented Jul 18, 2019

@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
Copy link

fgilio commented Jul 18, 2019

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

@f3l1x
Copy link
Author

f3l1x commented Sep 16, 2019

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 f3l1x changed the title Deploy with Now to ZEIT Deploy with Now to Vercel (ZEIT) Jun 12, 2020
@f3l1x f3l1x changed the title Deploy with Now to Vercel (ZEIT) Deploy to Vercel (formerly ZEIT, Now) Dec 31, 2020
@f3l1x
Copy link
Author

f3l1x commented Dec 31, 2020

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


bref.mp4

@f3l1x
Copy link
Author

f3l1x commented Mar 31, 2021

PHP 8.0.3 bumped.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants