Laravel Tutorial - Deploy any Laravel App in AWS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we're going to learn how to deploy any laravel application using aws to get started you will need to register for an aws account if you have not done that already then head over to aws.amazon.com click on create an aws account fill out the required information to register a domain and manage your dns you will need to use aws route 53 for the purpose and scope of this tutorial route 53 allows you to manage your dns traffic and to register and manage your domains on the top search for route 53 click on register domains click register and now search for the domain you want to buy once you find the domain that you want to buy click add to cart continue and now you want to basically complete a transaction fill out all the information follow the steps once your domain is fully registered go to hosted zones amazon automatically creates a hosted zone for you when you register a domain with them a hosted zone is a container for records these records contain information about how you want to route traffic for a specific domain to host our website we need to map our domain to an ip address we can either use the server built-in ip address or we can assign an elastic ip address an elastic ip address is a persistent ip address that you can assign to any server or domain to create an elastic ip address search for ec2 we will be using ec2 to create cloud instances elastic ips and security groups under networks go to elastic ip allocate a new elastic ip address and select from the amazon pool of ip addresses once that's created let's give it a name to use this elastic ip address we will need to associate it and assign it to both our server and domain to map it to our domain we will first copy the ip address and we will head back to row 53 we want to navigate to dns management into hosted zones domains can be accessible in two ways first way your domain for example your domain name dot com the other one is with www.yourdomainname.com we will need to map our elastic ip address to both versions to do that we will create a record and the first record will be just basically your website name and we will add the ip address it's an a record now we'll create the second record for the other variation to host our website we will need to allow our instance access to the internet we will also whitelist and grant our dev machine permission to remotely connect to the ec2 instance to do this we will be utilizing security groups security groups act like a virtual firewall for your instance to control traffic security groups allow you to fully control all inbound and outbound traffic and permission to begin we will head back to the ec2 page so on the top we search ec2 now click on security groups under network and security notice that we have a default security group the default security group allows all traffic access for all your server ports this is insecure since it accepts any incoming ip address we will create a new security group on the top right click create security group let's name our new security guru public website internet access and let's say for the description it opens ports 443 and 80. to allow https and http access or connection for our server so we're going to add two inbound rules one for http the other for https now we want to select the source anywhere for both of them and we want to click secrets now we will create another security group to grant our dev machines ssh access to our server instances go back to security groups click on create new security group for the inbound rule you're gonna select ssh for the source i'm gonna select my ip let's name the new security group dev access so dev ssh access and this group will allow ssh access for our devs and we'll click create now we have all what we need to get started to launch our instance now head back to security groups and let's give these names so dev ssh access an rds instance is simply your database server instead of running it locally on the same server where we host our website we will utilize aws rds to launch a separate server for mysql database on the top we're going to search for rds before we create our instance i would like to note that rds is expensive there is no reason to get an overpowered instance if your website has very little usage for this demo we will use a tiny instance with minimum storage which is more than enough to keep most new websites with no traffic up and running we will also use cloudwatch to set up monitoring and email notifications in a case where our server's usage exceeds a certain threshold that way we can scale our database when we need rather than pay for unused storage and operations we're gonna click on create database for the database creation method we're gonna select standard the engine is going to be mysql here you can select your desired version for our case we're gonna keep it eight we can also use the production template for the name let's name it production database and let's auto generate the password and select the smallest instance available that's going to be a burstable class and micro we can also reduce the size we're going to be using solid state general purpose and we're going to use only 20 gig that's the minimum we can also remove the auto storage scaling actually you can keep the auto source scaling what basically this does is if your database exceeds this number it will scale up to this number we're going to remove the multi-az deployment it's optional if you want to keep it but i think this one alone costs around 1250 usd a month so in my case i don't need it which is uh not recommended to remove it i'm gonna also for public access we're gonna make sure that this rds does not connect to the public only our instances and devices inside our vpc can connect to our database and that's pretty much it click create database to use our new database we will need three pieces of information we're gonna need the instance endpoint we're gonna also need the master user and the password now to get the instance endpoints you're gonna click on your identifier and you're gonna find it right here endpoints let's copy this and let's keep it in our notepad for the master user you can just go to configuration and you should be able to find it under availability master username so in our case it's admin let's add that to our notepad now when you first create an instance we selected the option to auto generate the password aws will only show you the password once once the instance is created if you missed that notification then all what you need to do is change your password again so you can do that when you go to modify and right here we can create a new path a new password so let's say happy world one two one two three continue and modify that's done now we are ready to use our database to create an ec2 cloud server instance on the top search bar search for ec2 go to instances and let's launch a new instance scroll down we're going to be using ubuntu server for the size we're going to be using micro this is more than enough in our example it's up to you to use whichever size you would like now we're going to configure the instance since we're using an elastic ip address we will disable the auto assign public option if you don't want to use an elastic ip address keep this option turned on now we're going to configure our storage and 8 gig is more than enough for our example and you can increase this number or decrease it for the security groups we're going to be using the security groups we created that they've accessed in the public website internet access and finally we're going to be launching the instance to connect to our instances we're going to be needing a pem file this file is only generated once we create a new instance we can either use an existing file or we can create a new one so let's name this green cleaning services ssh or actually dot ca and let's download this key pair now we can launch our instance note if you lose this file you will not be able to connect to your instance so it's very important that you secure this file now let's name our new server green cleaning services this is just to be consistent with our domain this name isn't doing anything just to help us identify what the server is for all right and we're all set to go just one more thing if you guys left the option turned on to generate the public ip address then you will see it populate in here we will need to map our server to our elastic ip and we do that by going to elastic ips and here we click on action and associate and then we're going to select our instance which is this one green cleaning services and we will associate now we go back to instances let's refresh and we can see that our public our server got assigned the ip address before we connect to our ec2 instance let's just gather some information our domain name is green cleaning services.ca our ip address is let's go back here and copy this address to transfer our project over to our server we will use git to clone our project it's up to you if you're planning to use github or bitbucket but we will be using bitbucket since github is focused more on public code where in bitbucket your repositories are private if you don't have a bitbucket account i highly recommend you create one to get started we're gonna open bitbucket we're gonna create a new repository let's name our project green cleaning service and the director the repository same name and let's remove the readme file and that's it create repository now we're ready to transfer over our files from our local computer so here's our website and we're gonna open the terminal the first thing we're gonna do is initialize a new git directory now we're gonna configure our user email and the user name so we could push our changes now we're going to add our remote origin so we do that by writing it remote add origin and we're gonna paste the url we're gonna add all our current work and we're gonna commit our changes and now we're going to push our changes so we're gonna use the force flag so origin master let's try that again and while that happens let's open up the bucket let's refresh so see now we got all our work in here now note we don't have the emv file it's all in get ignore you don't want to transfer over your emv file and store it in your repository ever before we can configure our server we will need to find the php version requirement for our project we will also need to find the laravel framework requirements to find out the laravel framework server requirements we will go to the laravel website from here we're gonna go to documentation you're gonna select your level version here in my case i am 5.8 now notice here these are the php extensions that we need to have enabled on our server for laravel to function they're fairly the same between all versions there are some minor differences but see this is uh so for eight let's go deployment see they're fairly the same to install any of these php extensions it's fairly simple in ubuntu so the command would be sudo apt install php the version so we're using 7.4 dash and the extension name so let's say bc math for c type for example it's the same thing p h p 7.4 c type or let's just make this lowercase c type here is a complete list of all the php extensions that i'm gonna need for my project you will have to adjust the php version for your uh requirement in my case 7.4 so it's all added here the last thing we're gonna do right now is to modify and to prepare our emv file for production so we're gonna go back to our website directory and we're going to modify our emv file we need to change the app.emv to production the debug to false and we're going to serve our app url through secured https for the database we're going to use our rds instance so the endpoint would be the host we have not created the table we do need to create a database the username and password admin and happy world that's it we should be ready just one more thing let's remove the app key because we're gonna auto generate that once we install the website on the server in order for us to remotely connect to our server using ssh we will need to use the pem file that we downloaded earlier now let's open the terminal and the syntax is simple so we're gonna use ssh dash i so we could use the file uh green cleaning services now we need the username ubuntu and the elastic ip address that we created paste now let's see yes now we are connected to our server to prepare our server we will be installing few things first we're going to be updating and upgrading our server second we're going to be installing apache and common software then we're going to also install php and all the php extensions that we need we're also installing composer and python certbot search spot we're going to be using it to enable https and assign basically ssl certificate now it's completely optional if you want to install npm i don't recommend you install npm in production you should not be building your project in production it should be already built prior but if you want to be using mpm all what you have to do is just add it in my case i'm gonna remove it so i'm gonna copy all this let's go back to our server let's execute all this we do have one package that failed but that shouldn't be an issue let's check if apache is installed let's use our ip address see we are getting the default ubuntu page so right now we have apache installed we need to uh configure and pull laravel so back to the terminal so we're going to go to the directory for our website this is the root directory for all our websites if on the server so if you want to add a website basically you create a folder here we're going to remove the html [Music] now we remove the default apache page so now we're going to create our web directory folder so let's call it green cleaning services let's navigate to it now we need to change the user permission to ubuntu and we're going to also give for seventh permission 775 which will allow us to read write and execute so so now let's check the permission so see here right now we can read write and execute it's for the user permission ubuntu so let's initialize git and now we're going to be pulling our project so let's copy our url for the bitbucket all we have to do is get pull let's try that again all right let's create our emv file now we already have it here so we're gonna copy going to paste it let's double check everything yeah before we can install our dependencies we will need to install composer 2 so the latest composer can be found at their website here's the instructions how to install it we will copy this paste it here now once that's done and you try to run composer it doesn't work and the reason is because it was installed inside this folder so we want to move it from here into here so to do that simply sudo mv paste this and we want to copy the directory here all right so now that's the composer and here we go composer two let's install our dependencies composer install now it looks like here we're missing some extensions so we can easily install those sudo apt install php 7.4 and here we want to install in b string so mb string let's try that again and we're going to also have to install the curl extension so let me just terminate this and composer all right so let's try now composer install so we're gonna also install another extension now we are going to generate our app key so we do that by writing php artisan key generate let's check that worked see here now we have generated our key now we're going to be running our migrations before we can run our migrations you will need to make sure that you have the php mysql extension installed to find all the installed extensions just use php dash m and here we have all the modules so in my case i have my sequel installed if you don't have my sql installed or what you need to do is sudo apt install php and my sql once you have the php module the for mysql installed then let's try to run our migration now if you get a connection timeout error then this means that your ec2 instance does not have access to your rds so we're going to have to create a security group for that and allow our ec2 permission to give permission to our server the first thing we need to know is our ip address so here i'm going to type ipa and your ip address should be right here we will copy this and we're going to go back to the amazon home page from here we're going to go to ec2 security groups we're going to create a new security group and this one is to allow production database access for the description we're going to write tcp for the inbound rules we're gonna add custom tcp and the port is 3306 now this is the same thing as just selecting here my sql for the source we're going to add our ip address now we need to assign our rds instance this security group so we're gonna on the top search for ids from here we're going to go to databases and we're gonna select this one and modify for the connectivity we're gonna remove the default security group and we're going to add the new one we just created so production database access let's save that we want to apply immediately now let's try to run our migration again now we're getting the error unknown database this is a good error as it indicates that the connection was uh successfully established now we need to create our database just a note if this if you still get the timeout error it usually takes few minutes for it to refresh so i would just recommend trying again after a few minutes to create our database we will need to connect to our rds instance to do that we're gonna use my sql so it's gonna go like my sql dash h to specify the endpoint then we need to specify the port so dash upper p and the port was 3306 and the user is admin and we will add the lowercase p to specify the password so we're going to copy this and let's paste it in here to show the current existing databases in our instance we type show databases we can see that our database is not created so we're gonna have to create it to do that it's simple create data base and we're gonna call ours green cleaning services and the terminator all right so now let's see if that works so we'll run php artisan migrate before we configure apache let us check the status of our website so our domain was greencleaningservices.ca and we are getting the error page because not found page because we deleted the html folder let's check the ip address all right so let us go back to our terminal and we're gonna go to the root directory for the apache so that's gonna be inside etc apache and inside here we have two folders sites enabled and sites available to get our website working we will need to create a new virtual host for our website in apache each website on your server will need its own virtual host configuration we will create and store our configuration file inside the site's available folder the sites enabled folder is used by apache 2. once you enable your website apache 2 creates a system link to your enabled site and it will store basically a copy of the configuration file here you should never modify the files here directly now let's see what's inside sites enabled we currently have the default website enabled so let's disable that now let's check what happened inside the same folder sites enabled so see right now that file is no longer there this is because this is automatically created let's go inside our sites available and let's create our configuration file so first i'm going to remove both of these files they're the default website configuration and let's do the same thing for the ssl all right now we are ready to create our virtual host file i have already prepared the virtual host file for port 80. port 80 is to allow for http access the server admin i have specified our email the server name is the same as our website name the server alias is the same but with w with triple w the document root is where we stored our website the public folder is where we declare our root now for the error log in the custom log they're both located inside the apache default log folder you can find the default log folder for errors for apache inside var and log apache so you can see here you have the access log in the error log in our other virtual host access so if we go back to our file we can see the login access here the only difference between my configuration file and your configuration file should only be the domain name so you will need to change the domain name and you will also need to change your server admin email otherwise this is what you will need to run your variable project so we're gonna copy this and we're going to create a new file let's call it green greencleaningservices.com and let's paste it let's save this now let's enable our website so we do this by sudo a2 en for enable then site and it was called grain cleaning services so let's restart apache here we go so let's check what's inside sites enabled so see our website now is enabled so let's see if it works now if you see this page which is basically your index file it's showing as html text this means that php is not enabled for your apache to enable php we simply run sudo a2 enable en and then mod then php and your php version so 7.4 now if the mod doesn't exist then we need to install the apache php module to install the php module for apache we simply type sudo apt install lib as in library then apache 2 dash mod then rphp version so 7.4 once that's done we want to enable the new module so you do that simply by writing sudo a2 enable en then mod and that was php 7.4 we will also need to enable the rewrite module so sudo a to enable mod and rewrite now let's restart our server and let's see what happened to our website so now we're getting this site cannot be reached so this is also a good sign we now know that our website is working this is most likely a laravel issue with the storage permission we will resolve this in a bit so let's try the ip address this is a very common problem that you will encounter when deploying level applications it basically boils down to your storage folder not having the correct permissions if you're also using local session storage then you will need to create a sessions folder to check if you are using a local session storage we will need to edit the emv file here we can see for the file driver it is local so if it is local then you will need to have the sessions folder so inside storage we will go to frameworks and then here see we're missing that folder so we will create it now the second thing we need to do is set the permissions for all the storage files and folders to set the correct permissions the first thing we're gonna do is change the user access so we're gonna do sudo and we're gonna do the same thing for the frameworks folder now we're gonna also change the permission so that's gonna be g plus w and that's recursive for the storage folder and for the frameworks and also for the framework session and for the logs folder we're gonna also have to install an ssl certificate to install an sss certificate we're gonna be using certbot so we're gonna write sudo cert bot now if sortbot is not installed we will need to install it to install certbot we will write sudo apt install python3 dash certbot dash apache once that's done let's run it so first we need to specify an email address we have to agree to the terms and services and for this option i'm going to select no and we want to activate https for number one and we're gonna redirect our traffic and now we will need to also enable https for the triple w version for our website to do that we're gonna expand our certificate so sudo sudo search bot and we're gonna use the expand flag and dash d for domain so green cleaning services dot ca comma www dot green cleaning services dot ca and we're gonna select no redirect because we already have that setup now we're gonna check our website green cleaning services.ca and here it is it's working this is an optional step but cloudwatch will enable us to monitor our servers we will use cloudwatch to set up notifications to monitor both our ec2 server instance and our rds mysql server so on the top first we're gonna search for simple notification service and on the left here we're gonna expand the menu and we're gonna select topics you can create as many topics as you wish for our demo we're gonna name our topic so we're gonna create one and we're gonna name it we're gonna select standard name it uh custom alert and we'll create a topic oops we cannot have a space now we want to subscribe our email to the new topic so we're going to select this topic and we're going to select email and i'm going to add our email here once you subscribe in email you will need to verify the email so there's going to be a code that's going to be sent to that email once you confirm your email you should be able to see the status here confirmed now we're gonna go to cloudwatch now in cloudwatch we're gonna go to create alarms and we're gonna create a new alarm and we have to select a metric so the first one will be for ec2 now there's a lot of metrics you can select here for our demo i'm going to select the cpu utilization so where is that right here select and i'm gonna leave the name as is and for the statistics i'm gonna basically say when the cpu is greater or equal to 60 now you can select whichever number you want here and let's go next let's select an existing topic so that's the custom alert and next let's call it cpu 60 create alarm now we're going to create another alarm for the metric we're going to select rds across all databases and again you can see here there are a lot of options you can even track how much free space you have so that would be the freeable the free storage space for this purpose for this example i'll just select the cpu utilization and let's say once the cpu is greater than 60 percent greater or equal to and for the period five minutes next and let's call this uh there we go now you can create as many as you would like thank you for watching our tutorial for any questions or concerns please leave a comment below
Info
Channel: AHT Cloud
Views: 6,124
Rating: 4.9459457 out of 5
Keywords: laravel, laravel tutorial, laravel aws, laravel ec2, laravel hosting, deploying laravel, laravel project, laravel php, laravel Production deployment, laravel 8, aws ec2, aws rds, laravel rds
Id: W2fQFbkEQo0
Channel Id: undefined
Length: 44min 39sec (2679 seconds)
Published: Mon Apr 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.