brefphp / bref

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

Documentation: How to add layers / extensions? #198

Closed prolic closed 4 years ago

prolic commented 5 years ago

@mnapoli can you give some quick hints on how I can create another layer on top with a custom php extension? I need to have this extension https://github.com/allegro/php-protobuf/ available. If not possible for you, I would need to fork your repository in order to temper with the docker file, so it's included in the main layer.

mnapoli commented 5 years ago

@prolic you very probably don't want to do that. I invite you to run AWS' documentation about Lambda layers. You can use several, so you can have your layer override/extend the Bref layer.

BTW I updated https://bref.sh/docs/environment/php.html it may help you a bit more. But at the moment I don't have time to write a complete guide (I mean on my open source time).

prolic commented 5 years ago

Thanks, I'll have a look.

jt-technologies commented 5 years ago

Hi, i followed the instructions to enable the pdo_mysql extenstion, but i was not able to get it work. I created a directory(php/conf.d) in my project with an custom php.ini file. But it seems like its beeing ignored. Does someone already tried to connect to an mysql db with his project?

my php.ini file contains only one line extension=pdo_mysql

nealio82 commented 5 years ago

I was able to do this earlier, and could confirm by just putting phpinfo(); into my handler script then checking the extension is loaded (it goes away when I remove the php.ini file or comment out the extension=pdo_mysql line).

How do you know it's not being loaded? Are you seeing an error?

jt-technologies commented 5 years ago

I'm using Symfony4 and the first error i got was "could not load driver" , then i investigated a little bit and found, that there is no extension for pdo_mysql. I tried to get the config with ini_get_all("pdo_mysql");. Then i received an Exception "Unable to find extension 'pdo_mysql'". Can you try to get the ini_get_all in your project?

pmayet commented 5 years ago

Which bref version do you use ?

Le ven. 1 févr. 2019 à 17:38, j-tec notifications@github.com a écrit :

I'm using Symfony4 and the first error i got was "could not load driver" , then i investigated a little bit and found, that there is no extension for pdo_mysql. I tried to get the config with ini_get_all("pdo_mysql");. Then i received an Exception "Unable to find extension 'pdo_mysql'". Can you try to get the ini_get_all in your project?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mnapoli/bref/issues/198#issuecomment-459784629, or mute the thread https://github.com/notifications/unsubscribe-auth/ABhO9J5L5mA5hI68XcYPGglUB56Rrakgks5vJG2UgaJpZM4aLG0J .

nealio82 commented 5 years ago

var_dump(ini_get_all("pdo_mysql")); gives me

array(1) {
  ["pdo_mysql.default_socket"]=> array(3) { 
    ["global_value"]=> string(15) "/tmp/mysql.sock" 
    ["local_value"]=> string(15) "/tmp/mysql.sock"
    ["access"]=> int(4)
  }
} 
nealio82 commented 5 years ago

Which bref version do you use

Good point, I wonder if it's a manifestation of this..? https://github.com/mnapoli/bref/pull/219#issuecomment-459670739

@j-tec, what version of PHP is running on your host machine? If it's less than 7.2, you won't be getting the latest version of Bref, and the layers / extensions won't work

jt-technologies commented 5 years ago

im using bref in version 0.3.x-dev, PHP 7.2.11 on my composer-runing machine and this layer: arn:aws:lambda:eu-central-1:416566615250:layer:php-72-fpm:1

and the layer was the issue - it is not possible to enable the pdo_mysql extension with this 7.2 layer. I switched to arn:aws:lambda:eu-central-1:209497400698:layer:php-73-fpm:1 and not its working fine.

thanks @all ;-)

pmayet commented 5 years ago

@j-tec You have to switch to tag ^0.3, and change the layer reference : layer_version

deleugpn commented 5 years ago

+1 On this. I just found #96 and Laravel Telescope requires bcmath to work. Not sure how I would compile it and add an extra layer with the extension.

mnapoli commented 5 years ago

Hey all, there is https://github.com/stechstudio/bref-extensions by @bubba-h57 which is a good starting point.

jt-technologies commented 5 years ago

Is there any documentation about custom layers which describes the structure needed for an custom php extension? I just want to install gd.so but i have no idea how i have to set up the layer. Can someone share his experiance with custom layers?

ppbittencourt commented 5 years ago

I have thee same problem as @j-tec. I need to add the php_gd2 extension to my project, but have no idea on how to do it using lambda layers.

awackate commented 5 years ago

Hello. I'm also having the same issue. Already read everything about the custom layers both in bref and amazon and I've been fighting to get php to load soap.so through a custom layers.

Here's what I understand is the right process:

  1. Compile the extension you need in the same php environment you are using in bref (I downloaded the php source, and used phpize, ./config and make of the extension only. Result was a file soap so.
  2. Created the path structure described in the documentation and placed the .so file there.
  3. Created a soap.ini file also that enabled soap (extension=soap)
  4. Ziped that and created a layer in the AWS lambda interface with that code.
  5. Added the layer after the bref one in my function.

I still get the errror that dinamic library soap could not be loaded, but when I do a function to print the folders and files, lambda actually decompressed the soap.so in the right place, and the php.info page shows that the soap.ini file is beeping read.

I hope it helps you get closer at least.

And if anyone managed to get soap working please please let me know. I'm going nuts here.

Awesome work by the way

mnapoli commented 5 years ago

Hi @awackate, I think this might be because you need 2 things:

which would match the error you describe:

I still get the errror that dinamic library soap could not be loaded

The PHP extension may be actually just a proxy to behavior that is implemented in a shared system library.

Note that this is just a supposition, I'm not familiar with that extension.

awackate commented 5 years ago

That makes a lot of sense. I'll look into that since there's no much info on soap around.

In case that fails, is there a way I can recompile everything but with soap turned on? Making my own php layer won't be enough to do a laravel project right?

mnapoli commented 5 years ago

In case that fails, is there a way I can recompile everything but with soap turned on?

I am afraid you will have the same problem. In the build scripts we explicitly include system libraries that we want to include in the layer (those needed by the PHP extensions we include). So you'll still be missing the soap system library (if they actually exist, because again I don't know at all).

awackate commented 5 years ago

Thank you Matthieu. It's nice to have such a quick reply. I failed miserably at identifing any missing system library missing for this to work. So I'm giving up on this pursuit.

Any chance you'll be including SOAP in the libraries any time soon? Or do you any idea of how can I go forward from this? I need SOAP because Google's Adwords API requires it and I'm trying to connect to that.

Thank you

atrope commented 5 years ago

As i wrote in #283 Soap must be enables when building php, @awackate if you want to you just need to add --enable-soap in the php.Dockerfile and rebuild the layer.

We also use Google Library and all works :)

Sh1d0w commented 5 years ago

Hi, any plans to include gd or imagick libraries?

awackate commented 5 years ago

@atrope and @mnapoli thank you very much. I managed to compile my own php layer with soap and it works perfectly. For anyone having this issue, it's not a big investment of time and it's worth it.

mnapoli commented 5 years ago

@awackate happy to hear that, it might be very useful to some folks in the future if you described in a few steps what you did. At the moment this issue is kind of "the documentation" so feel free to contribute to it ;)

mnapoli commented 4 years ago

You can read how to add PHP extensions in https://github.com/brefphp/extra-php-extensions