Building a Laravel app binary using Franken PHP - standalone dependency free binary

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys welcome to my channel and today in this video I am going to show you what Franken PHP is and how this modern PHP app server has some very interesting features which can really help us deploy our app in production and also in our normal development flow before we dive into the code let's understand what Franken PHP is well Franken PHP as I said is an app server which is written in go it uses caddy as you know the behind the scenes to run our application uh any PHP application okay and these are some of the features so if we look at it it says that uses the official PHP executor embedded in a stateof art web server caddy it supports HTTP 1.1 2 and 3 it also generates our https certificates automatically using let encrypt or zer SL uh so you don't need to worry about the renewals and stuff and that is quite useful and I have seen that we can even have a local development certificate or something that we call a self-signed certificate what which also helps us in having a https certificate on our application when we are doing the development and it is very simple as this line says we just need to copy our PHP files into the document route and that's about it everything will just work but now comes the best part at least that's what I have felt about this which is Franken PHP allows us to create a standalone self- executable and dependency free Binary of our PHP application so the question is what is a self- executable dependency free Binary and what what is the advantage well if you are coming from the world of net or Java then you will already know that those applications compile into a final the build file okay whereas in any typical PHP application like whether it is WordPress based website or a Lal based application the application runs on runtime right and the files are executed when the request LS are coming in so there is no compiled version which means when we are shipping our code the actual code is getting shipped and when we are running our code on production the actual code is setting there we need PHP we need an a server like Apache or enginex stuff like that right but what Franken PHP does is it allows us to create one binary file which is self- executable and it has everything in it which is required for the PHP application to run and the big advantage of this approach is that on production we don't need to install PHP we don't need to install Apache we don't need to do a lot of stuff around extensions and stuff like that which we would need for a PHP application to run rather in production what we need is a Docker binary because obviously we will run a Docker container and we just need the executable file and that's where I got really interested and I thought I should definitely share this video so with this understanding let's get started and see how this can be done just so that we are on the same page what I will do is first create a simple Lal application using compos create and I will create one route so that we are able to ensure that routing also works all right I have a new Lal clean installation let me go inside it and typically if I want to run my LEL application I can do something something like PHP serve or rather PHP Artisan serve with this command ideally my application should be running on Local Host Port 8000 and I can see that is the case now let's quickly add one simple web API which will just return a string so as you can see I just created a simple route and when I'm going to that particular route I see this message being displayed so our application right now has two web routes and now what we are trying to do is run our application using Franken PHP now although I will show you in details how to do that but I would want you to know how to find your way around if you are stuck so this is the documentation and what I am going to do is go through this guide where you know this getting started what this does is run our application on Local Host Port 443 right and uh it basically takes whatever is there in my current application so I'll just copy this I'll make a few changes our code base is the current code base so I don't want it to be inside the public folder I'll rather have it over here it is exposing Port 80 and 440 you may choose to disable any one of them if you want to uh I'll just keep it like this and with this in place if I go over here paste it and I'll just let you know that I already have a few images including the Franken PHP Docker image so it won't take time but when you are doing it for the first time it might take some time to download the image which is about 540 MB so let's hit enter now my bed I will also stop the port 8,000 where the application was served so now if I go over here this doesn't work work whereas if I copy this and move away from the port and directly go to Local Host welcome it shows an https warning but if I ignore it I can see my welcome route the error is coming because this is a self-signed certificate so Chrome will typically complain rather not only Chrome most probably Safari and Firefox will also complain about that but we can ignore it right now and yeah so this is the bare minimum which we can do with Franken PHP because it just took us one simple command to run our application okay but now this is not much in terms of what we would want to do on production right this is not the way we we would like our code to run on production because right now what I have is basically pushing the entire current directory dollar PW means current working directory right so I'm basically pushing my entire working directory into the apps folder and then Franken PHP is running it from that particular folder so if I go to Docker the container that I'm running okay and if I go to the terminal I'll see that there is a PHP version installed 8.3.0 which is right now the latest stable version but it doesn't have composer installed in it yes it doesn't find composer so it can run the application but maybe if you want to do some package editions and stuff like that you will have to do it in a a different way because this image is primarily to run a PHP application it doesn't have all the softwares okay so then the question is how do we build the binary because what we have achieved so far is something which we can do with any other Docker image as well for example there is the bitnami instances there is the official Lal in uh Docker image as well uh using sale that you can run it so what what's the difference well the building of the binary is that is something which we will look at right now again I would say that know there are documentation available create Standalone self- executable PHP thing which you can refer to but I have a simplified version which I would like to show you I mean this is one of of the way to do it but yeah I know the way which I'm showing you is a little simpler I would say and it also helps you to create the binary in a build process a multiple Docker build process which is useful if you are trying to do a cicd okay so the first step is obviously I'll create a Docker file and let me copy some code all right so now we have our basic code in place let's understand line by line what is happening over here so on the first line I have the platform definition because I am going to build the binary on a Mac and if I don't add this it will kind of create it from a in a way where it may not run on a Linux machine okay so I'm basically saying that the binary which I'm trying to build is for a platform which is Linux amd64 um so sorry not the binary but the image that we are trying to bring in is for the platform and we are naming it as Builder this is the convention when we have multiple stages in our build process now we are setting a working directory which is/ go Source app dis app now you can have many ways to do it but yeah I mean this is one folder which I saw in the documentation that can be used now this particular image does have composer okay it's a big one uh I even have a tweet where I was asking people if really this image needs to be so big um so yeah static app this is what I'm talking about initially I mean it's a very uh this image is getting updates pretty frequently when I tweeted it was about 7.1 GB of uh image but right now it is about 6.47 so maybe they have done something I'm not sure if the entire um file size is required if I'm know pulling something which is way too big I don't know I'll figure it out but maybe because I'm know building through a different operating system it needs certain kinds of things but yeah this is for the build only so the only good thing about it is that we don't need it in production our server doesn't need to have a 6.47 GB Docker image okay so we pull that in set our working directory and then what we do is copy star. star sorry uh dot to dot which basically means copy everything from here and copy it inside the working directory now the next command over here is for the build process which does a composer install ignore platform requirements and this is specifically required because the extensions that I'm installing is in a later stage okay and I couldn't get this to be done before the composer installed because this needs to run and it this particular build static. sh depends on certain things over here so I'm first doing a composer install it's a noev okay and once the in the composer binaries or the composer packages are installed after that we go to our new working directory which is still here and then we run embed this which is part of the documentation and this is where we are defining what are the different PHP extensions that our application needs now one thing to note is if you you specify certain extensions then only those extensions will be installed but if you're not sure you can get rid of it and all the extensions which Franken PHP now has compatibility with or it supports it will install them okay and with this line 12 is basically where our build will start okay so we pull that image we set a working directory we copy our source code inside the image we run composer install right now I have vendor folder because I created the app but if you understand in a cicd process inside the uh deployment pipeline you will not have the vendor folder so you'll have to bring in the vendor folder and Know download all the packages and then you know install the extensions and then start building the binary once the binary is ready for us what I'm doing is taking an Alpine 3.1 9.0 image it's an Ubuntu image very small one I think I will be able to show YouTu okay I think it will come in when I run it there is the Ubuntu instance which is 69 but I think the Alpine is even smaller okay and it does work so on the Alpine Docker image we set our working directory to/ app then this is important we copy from Builder and if you remember we had named our this particular stage as Builder so we are copying from Builder this Franken PHP Linux 8664 file which is nothing but a file that this build static sh file will generate which is our Standalone binary file we name it as Lal app we expose Port 80 and we execute a command which is Lal app we execute the binary and we run PHP server so a lot of talking has been done let's execute it and see what happens so I'm executing Docker build /t and this is the name and I'm going to take the current Docker file so everything is fine I just hit enter so this particular item it looks like it is going to take some time they may have created a new Docker image so I'll wait for it and with the power of editing I will jump to the section where the image is built all right so so finally everything is ready for us it took me a couple of retries because my internet connection was a bit flaky and it was know breaking up but then I was able to get it to work and now if you see my Docker situation is like this this is a dangling image because this is the 6.28 GB Docker image that is used to build our binary and this is the final thing and as I said once you have the image downloaded right it's a pretty quick process if I run this thing again which will build the entire thing if you see this is the only part right it is quite quick okay everything is ready because it's already is able to understand that everything is kind of ready uh sometimes it may take a little bit of time to do a composer install now I have my image ready if I want to run it I'll just show you through to the GUI I point to maybe 8,000 so my my 8,000 Port is mapped to the port of 80 of that container now if I go to place let me do Local Host 88000 and I'm able to load the Lille application and if I do welcome it is also doing that is showing me the route that we had created and you can see I am not serving it okay it is through a different port sorry a different thing the container is running on Port 8,000 on my machine okay and this container has certain things if you look at our code I'll show you my working directory is this and I'm copying the Lal app binary to that folder so inside my working directory okay sorry LS I can see I have a binary so if I do something like this I will see I have all these commands available however I don't have PHP on my machine okay this is a clean Ubuntu server you can see this is what I'm using to run my Docker container okay and one thing which you should know is if you are trying to run the application manually you can use the basically this PHP server and it will run on a particular port and if you want to execute certain Artisan commands then what you need to do is do LEL app and then you have something called as php-cli so I'll do PHP CLI and then with this I can run artisan and so it gives me all the commands which are available so if you want to do view cache clear or know migrate stuff like that you can use that function to run those commands so that's about it guys uh that's what I wanted to show you with Franken PHP if you like this video click the thumbs up icon and yeah if you have watched so far then I would definitely request you to click on the Subscribe Button as well
Info
Channel: Amitav Roy
Views: 4,784
Rating: undefined out of 5
Keywords: php, laravel, docker, filamentphp
Id: 718qrjqCv7M
Channel Id: undefined
Length: 20min 46sec (1246 seconds)
Published: Sun Dec 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.