Laravel Docker Nginx + PHP-FPM + op_cache

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back guys today we're going to learn how to build top Performance Engine X servers for laravel we will build the nginx server with PHP fpm and op cache to demonstrate the performance difference I have created three sample projects in here one is using PHP Artisan serve the other one is using nginx and the last one is using engine X Plus op cache for the PHP Artisan if you take a look here this time 49 milliseconds is how long it took this page to load which is the default learnable web page let's refresh this page few times and you can see it's averaging around 42 to 45 milliseconds for the nginx build if we refresh our web page you can see it's about the same ranges from 42 to 45. now for the engine X Plus op cache if we refresh our web page you can see here it takes around 5 milliseconds up to nine milliseconds looks like it averages out to six if we compare the engine X Plus op cache with let's say the larval artist and serve it's about seven times faster than here and about the same for just the engine X so using op cache with engine X is going to be seven times faster than your standard way of building your Docker containers before diving into the course guys you should know your way around Docker for laravel if you're new to Docker I highly recommend you watch my previous videos I have one called laravel Docker course complete laravel dockerization if you're using Windows I highly recommend you watch my other video Windows wsl2 Docker tutorial both links are in the description our first step we're going to create a new level project I am inside my workspace folder I'm going to create a new directory let's call it laravel Docker engine X op cache now let's go inside our new directory and in here you want to go ahead and open your code editor I'm using vs code all right now in here we're going to create a new level project using composer so I'm going to write composer create project level slash laravel and a DOT now once you have the project created let's go ahead and modify our EMV file let's set the database to laravel for the username you can set it to whatever name you like I'm gonna use Homestead for the password make sure you add a valid string I'm going to use Secret in here let's change the app URL to 127.0.0.1 and we're gonna expose port 8000. now we're going to create our starter Docker files inside your project go ahead and create a new folder call it docker inside here create another folder call it engine X inside here create another file and call it nginx.com inside the docker folder go ahead and create a new folder let's call it PHP in here you want to create two new files one called PHP dot ini another one called PHP Dash fpm.com and inside the docker folder you want to go ahead and create a new file call it entry point dot sh let's go ahead and minimize all these now we're going to create our Docker file and let's make sure that this file is placed in the main directory let's go ahead and create another file and let's call it Docker Dash compose Dot yaml yml now let's go ahead and build our database server let's go to our Docker compose file and let's add our version so we're going to use version 3.8 and let's create a new service so we're going to write Services I'm going to add a comment and I'm going to say database service let's go ahead and call it database and let's give it a name so I'm going to say container name and we're going to name the service database for the image I'm going to use MySQL 8.0 for the ports let's go ahead and expose Port 3306 23306 now we're going to set our username and password and database name so in here we're gonna write environments and I'm going to open my EMV file so in here we need to select the mySQL database name and that we're going to grab from our EMV variable right here database so we write DB underscore database let's go ahead and copy this few times now the second variable is going to be called MySQL underscore user let's copy this from here and let's go ahead and add MySQL underscore password that's coming from here and the last one would be MySQL underscore root underscore password and let's set that to equal the same password that we have now if we run this it should work but we don't have a volume if we don't have a volume the problem is once you restart this container you will lose all your data so let's go ahead and add a new volume so we could persist our data I'm gonna call it DB Dash data and I'm gonna store this inside my main project folder and in here I'm going to use that volume so we write volumes and we're going to use our DB data volume and we're going to point to our MySQL data and that's going to be inside VAR lib MySQL and this should be ready to go let's give it a try so I'm going to write Docker compose up Dash D let's go ahead and write Docker compose PS and you should see your database working now we're going to be building our PHP service let's go to our Docker file and in here let's go ahead and pull our image so we're gonna write from PHP and we're going to use PHP 8.1 fpm and we're gonna create this as PHP now let's go back to our Docker compose file and in here on top of the database let's go ahead and add our PHP service so I'm going to add a comment in here and say PHP service and in here let's call it PHP for the container name let's call it PHP and for the build steps we're going to reference our Docker file so let's go ahead and write build the context is going to be this location so we write Dot and the target is going to be PHP that's the one that we created in here so whatever is under here is our Target for the working directory let's set it to VAR www so what this does is makes it every time you sh into your container by default you will start in this directory this is where we will be hosting our website for the ports let's go ahead and expose Port 8000 to 8 000 and we want to make sure that this service is built after our database so in here we write depends on and we want to go ahead and write this database service in here now we want to specify our volume so I'm going to add it right under the working directory in here let's write volumes and I want to map this current location to VAR www now let's just give this a quick try let's write Docker compose up Dash D now this is a very common error if you see this permission denied if you have this you want to go ahead and write ls-la inside your Docker folder and let's see what permissions we have here so if you see our entry point doesn't have any permissions so let's go ahead and give it read and write permission so I'm going to do chmod 755 and we're going to go to our entry file now let's try to run Docker compose up again now let's do Docker compose PS and see the status of our container in here it looks like we have an error let's view our log I'm going to use my Docker interface and here if you click on your container you should see this error here and this is fine because we haven't really built our entry point file yet let's go back to our code editor and let's go to our Docker file now in here the first thing I want to do is I want to create a new user you don't want to be running things with your root user let's go ahead and create a new user so we write run user mod Dash u1000 and the username so I'm going to call it www.data right under here let's go ahead and run apt-get update and let's go ahead and run apt get install Dash y let's install unzip lab PQ Dash div lib curl for the g n u t l s Dev nginx now let's go ahead and install some PHP extensions so I'm going to run Docker PHP extension install so we're gonna install PDO PDO for MySQL BC math curl and OB cache after we have all the basic extension and software installed on our server let's go ahead and copy our files from this location to our working directory location so let's set our work directory to VAR www and also when we copy our files let's go ahead and set the user permission so I'm gonna write here shown equal www data now this part is very important especially this part too when we start running laravel you will get permission errors when laravel tries to write for logs so this would be how we fix it now let's go ahead and install composer so we write copy from composer and the version is 2.3.5 and we want to copy this from the user bin composer folder to the user bin composer folder in our server and might as well run few commands let's run PHP Artisan cache clear run PHP Artisan config clear and in here let's go ahead and make sure that we have the correct permissions set on our storage and bootstrap files so we want to run chmod recursive 755 inside rvar www www storage folder this is where laravel is going to be writing our logs let's go ahead and do the same thing but for our VAR www bootstrap folder this is where laravel will write in Cache things now under here we want to call our entry point file so we're at entry point and the file was inside our Docker folder it's called entrypoint.sh now let's go to our entry point file we place that inside our Docker folder we call the entry point.sh in here we want to run composer install no progress and no interaction and we want to also copy our EMV file if it does not exist now I'm going to wrap both of these in an if statements so for the first one I'm going to say if the file vendor autoload does not exist then we want to run the composer install otherwise we don't want to keep reinstalling it every time we build our container and for the EMV file we want to do the same thing we're going to say if the file EMV does not exist then we want to do this let's go ahead and close our if statement in here actually I'm going to add an echo here and just say creating EMV file now let's go ahead and go to the bottom and here we want to write PHP fpm-d and we want to write engine x dash g d a e m off Damian off and this will start our engine X in PHP fpm servers now let's go to our terminal and here you want to write Docker compose build use the no cash flag and we want to also run Docker compose up Force recreate Dash D in dash mode so what this will do is make sure that we rebuild all our Docker containers and not use the cache and this one will force to recreate our containers let's just give this a quick try it shouldn't work because we haven't created our nginx server configuration yet but let's just see what we have so far now once that's done let's go ahead and write Dot compose BS and here looks like we have an error so let's go back to our Docker interface I'm gonna go to my PHP container and you see this error here it says execute Docker entry point execute format error whenever you see this what it means is that inside your entry point you're missing your bin bash comment in here so you need this for all bash files so you're right here a number sign explanation mark and you want to point to your bash executable usually it's inside the bin bash folder now there's just one more issue here in my file I just need to fix this typo in here so for here you want to add the dash to the if and that should be pretty much it let's go ahead and give this a try in our terminal let's go ahead and write Docker compose up Dash D to run in detached mode once that's done let's go ahead and write Docker compose PS and here it looks like both of my containers are up and running we have it configured in Gen X yet but let's just go ahead and open our browser and let's make sure that engine X is installed so we write one two seven point zero point zero point one at Port 8000 and in here we're getting empty response and this is a good sign because we haven't really configured nginx now we're going to set our PHP fpm configuration and our PHP init configuration for the PHP fpm configuration I have added the default file inside my GitHub account so you want to go to the browser you want to write github.com emad emad z a a m o u t Imad zamut this is my GitHub account you want to go to repositories and here you want to search for PHP Dash fpm.com and you should see I have this project right here and there is the configuration file for PHP fpm this is mostly the default configuration with some adjustments let's copy the whole thing so I'm just going to copy the whole thing we right here there's a button called copy let's go back to our code editor and let's add it in here so the big part is in here if you scroll down this listen is what we're using for our PHP fpm we added this inside our nginx configuration if you guys remember right here so this has to be the same and one more note so here there's the user we set it to Triple W data the group to triple double data let's scroll down for the PM we're using Dynamic but the biggest part is this one right here the max requests this number should always equal to this number here the keep alive requests if these two are not equal then your requests will randomly give you 502 errors because either fpm or engine X will end the request so they have to be equal now let's go ahead and go to our PHP init file and in here we want to set our op cache configurations actually before we do that let's go back to our Docker file and I'm going to create some variables for OB cache this way we don't have to have multiple configuration files you can just adjust it in here so you're going to write EMV our first variable is going to be called PHP underscore op cache underscore enable and we're going to set it to one we're gonna create another one PHP underscore op cache and let's call it enable CLI and let's set this to zero and we want to write another one EMV PHP op cache validate timestamp and let's set this to one EMV PHP underscore op cache revalidate frequency and let's set this to one now let's go back to our PHP NF file so that was inside our Docker PHP PHP in it and here let's go ahead and use Optical generate Zen underscore extension equal opcash.so now under here you want to write opcash.enable and this will enable or disable op cache and we want to specify that from our EMV value that we created so that was PHP underscore op cache underscore enable let's go ahead and copy paste this and let's add one for the CLI so in here beside enable you add underscore CLI and we created the same variable here underscore CLI now we're going to set our op cache validate timestamp and validate frequency so we write opcash dot validate underscore timestamp equal and let's reference our variable so we called it op cache validate timestamp actually I'm just going to pull my Docker file and put it in here and let's make sure we're using the right variables so let's go back to our PHP init so for the validate timestamp we have it right here let's copy paste it let's duplicate this and let's add one for the frequency so in here it is called revalidate frequency now guys I'm not going to spend too much time explaining what all of these flags do but to give you guys a general idea let's just go to our browser and in here you want to search for PHP or P cache configuration and here you have these results let's go ahead and go to the first one and in here if you take a look there is all the flags that we used plus there's all the other ones that you can also use so the enable ones are very simple they're going to enable and disable now for the revalidate for frequency and valid timestamp let's go here so for the timestamps if enabled basically opcash will update the scripts every frequency seconds so this is the amount of seconds that we specified in here in our variable we said every one second and here we're validating the timestamp and here the CLI we have is set to zero you can set it to one that's fine now let's go back to our Docker file and in here we want to use our configuration files so right under here let's go ahead and copy our configuration files so I'm going to write copy and we're going to reference our Docker PHP PHP dot init file and we want to place this inside user local Etc PHP and PHP init these are the default locations for PHP configuration for our server let's do the same thing for PHP fpm so that's inside Docker PHP we called it PHP Dash fpm.com and we want to place this inside user local Etc PHP dash fpm dot d slash www uw.com that's where we want to save it this is the default for the fpm configuration file let's add one more copy and we want to copy our Docker engine X slash enginex.com and we want to place this inside BTC engine X engine x dot configuration that should be pretty much it let's give it a quick try and here let's go ahead and write Docker compose build no cache and run Docker compose up Force recreate with the detached flag now in here for some reason it looks like my Docker composed up command didn't run so I'm just going to copy this and run it manually so we want to run Docker compose up first we create Dash D okay looks like we made a mistake here so it's supposed to be Force recreate not create now looks like that's good to go let's write Docker compose PS in here it looks like our PHP container has an error so let's see the log so we write Docker logs PHP and here it's saying unknown directive keep alive requests and there's a typo right here we're missing the S on the requests let's go back to our engine X configuration file that's inside Docker engine X engine X configuration and let's fix that so here requests let's try to write Docker compose up Dash D again and write Docker compose PS now looks like there is another error let's see what it says so we're gonna write Docker logs PHP now if you see here this error is the same error that we had previously but we already fixed it in here the reason why this is not updated is because when you write Docker compose up it will try to rebuild from the cache so Docker caches everything that's why we always write Docker compose build no cache and then you can execute Docker compose up with the force recreate flag now let's give this a try now once that's done let's go ahead and write Docker compose PS in here we look like we still have an error in our PHP container so let's write Docker logs PHP and here it's saying unknown directive client Max size body let's fix that it's a typo as well so the issue here is that there is no Collins in here for these two again you guys don't need to have these I just added it for reference because one day once your website skills you might need to adjust these so let's go ahead and write Docker compose build no cache and Docker compose up Force recreate with the detached flag now let's write Docker compose PS looks like they're both running let's go back to our browser and let's refresh our home page actually I'm going to open developer tools here and let's go ahead and refresh so you should see the layer of a web page here take a look at our amazing load performances here so we have five milliseconds refresh 10 7 7 6 6 7 6 7. so this is very very fast and the reason for this is that we're using nginx and op cache so what opcash does is pretty much cache all our project files now this is not good to use locally so for locally you need to make sure that you actually turn it off because sometimes you want to make a lot of changes to your files if you do they will not be reflected that's why if you go to your code editor and let's go ahead and go to our Docker file and here what you want to do on local is you want to make sure that you set this to zero and you want to set this to zero as well and these two will make sure that we always update our files and here you want to set this to zero as well now for Productions you want to set all these flags to one and that should be pretty much it now just a side note for all your future projects if you go to my GitHub account so that's a github.com emad zamut z-a-a-m-o-u-t that's the last name and here if you go to repositories and I want you to search for laravel DASH Docker Dash engine x dash PHP and or this one right here so I created this GitHub repository it has all the code that we just did in here but what you could do with this is when you create a new project locally just copy this and pull it inside your project directly and you should be able to immediately just run get init pull this repository set your EMV variable for the password and database name and all that stuff and you can run Docker compose build and Docker compose up and it should work so if you want to double check your work you can check it out here thank you guys for watching if you found this tutorial useful I would highly appreciate it if you guys hit that like button otherwise we'll see you guys on the next video thank you
Info
Channel: Emad Zaamout
Views: 5,408
Rating: undefined out of 5
Keywords:
Id: so50k0t7qWo
Channel Id: undefined
Length: 22min 7sec (1327 seconds)
Published: Sun Oct 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.