How to deploy a Laravel application on Amazon EC2 - Laravel Nagpur Aug 2021 Meetup

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay since i started recording again so i'll just quickly uh recap what i said uh since gabriel is facing issues with his internet what i'm doing is i'm continuing in his place and i will show you all how to install laravel application in aws uh so first we will use this aws service which is called as ec2 which is elastic compute cloud ec2 what is hc2 ec2 is basically it gives you a virtual server uh if you have used any other uh shared hosting web server like uh hostgator or bluehost or dreamhost whatever then you know how what are servers so we will go to ec2 dashboard and we will launch a new instance in aws terms a server is called as an ec2 server is called as an ec2 instance so we need to launch a new instance the first step it asks is what operating system should the server use so we are going to select ubuntu 20.04 lts now next it will ask you for the size of the server since we are this is just a demo we will go with the smallest possible size which is t2.micro but if you're running a production server which is let's say compute heavy or memory happy then you will definitely want to use a different size so what does size means uh size will determine uh what will be the cpu speed and what will be the ram on the server so smaller uh size means definitely a lower uh clock speed cpu speed and and lower amount of ram available so let's go to the next page we will select k2.micro we will move on next is it asks you for some configuration uh details for the server how many number of instances we want that is number of servers we want so we will select one we only want to create one server what network we want so uh when you create an account in aws it will automatically create a network for you network means internal network uh in which all your services will run so this is a vpc means virtual private cloud so this is the default network that aws created for us so we will use the same network whatever subnet again it selects something by default we will use the same subnet i'm not explaining in great details what all these things are you can just go with the default value selected for now then we will let them i'll let it assign any public ip capacity reservation we want open then we will select we'll keep everything else as default this is one important setting shutdown behavior this determines that when you shut down at a server when you stop a server what happens so we it can either stop or you can interterminate terminate means it will destroy the server so what if we choose this then what will happen whenever we stop the server instead of shutting it down aws will uh will terminate it destroy it completely so we want the behavior to stop on shutdown not terminate we'll keep all of the settings as is and then we will go to next step which is add storage storage means uh when the aws server creates the instance what size of disk we want to attach to the server so let's go with let's say 16 gb for now we want general purpose ssd hard disk general office ssd to be attached to the server next is tag you can add a tag let's say maybe tag is just to add some text to the instances if you have a big aws account wherein you are running multiple many instances many servers you can add some text to the server to identify them so i'm adding a tag called as name let's call this as uh laura will test laravel now put test next is configuring a security group so what is this security group now let's try to understand this first security group is sort of firewall which either opens or closes various ports on the server so if you are running a web server if you want to install the application on any server what all ports do you normally want to open on the server one is http that is port 80 so that it can serve the http request the other port is uh https port 443 so that it can serve the web over secure protocol that is tls https protocol and then you also want ssh to be opened up so that you can actually connect to the server log into the server and run your commands so by default it shows sshs is opened what we will do is we will add http port 480 this should request from anywhere this means anywhere request from anywhere http request from anywhere should be able to connect to the instance it should be able to connect to the server similarly we will configure https from anywhere people should be able to send http request to the server okay now let's review and launch so we are creating an ubuntu server the size is t2.micro it will have one cpu it will have a webbery of 1gb y1 cpu and 1gb because we are using t2.micro if you choose a higher instance higher size instance then there are more cpus available and more memory available ram and this is the security group that we configured this means all these ports will be opened for incoming connections to the server so http https and ssh will be opened and if you look at storage then we are also attaching a hard disk ssd to be specific size 16 gb to the server let's launch when you try to launch it will ask you to create a new key pair now what is this key pair so once the server is created what happens is you will need to ssh to the server login to the server to install the application now if you create a new key pair this will put an ssh key on the server so that you can actually access it without using any password okay so let's uh create a new key let's call it laravel test this is very important step you if without downloading this key file you won't be able to access it to the server at all so let's download the key this will download the key and we will save it i've saved it in downloads folder actually what i will do is instead of creating a new key i'll use an existing key i have an existing key called as uranium so i will use that okay then i'll launch the instance so now the new server is being created your instances are launching in the meantime what we will do is we will look at another aws service called as rds rds means relational database service so with this service it also creates a new server and it runs mysql database on that server and this new server is configured to run mysql databases in an optimum way that is why we are using a separate service to run the database we are not installing the database on the ec2 instance itself we are not installing the database on the web server is itself ec2 will be our web server and rds will be our database server so let's create a database server we will choose mice where is we'll choose mariadb which is mysql compliant then let's name it this will be the name of the rds server then it is asking for master username and password this is the root user that it will create in the database so let it call admin is fine for now master password let's put anything i'll just put laravel upload for now okay this is again selecting size for the database server again similar concept if your larger large size means it will have more memory memory more cpus for example you can see db.m5 has 48 virtual cpus and 192 gigabit of ram we don't need such a huge server so we will go back the smallest possible size which is dbt3.micro which has two virtual cpus and one gigabyte of ram now for database server again we need to allocate some space so we will allocate let's say 100 gb disk space and we'll go with general purposes d and locate 20 gb is fine we don't want auto scaling if you enable auto scaling then what happens is here we have selected 20 gb space for the database if our database grows up to 20 gb then aws will automatically scale it it will automatically add more space to the database server so this is that setting what it does don't need it for now for this demo uh we will keep this as is connectivity we'll use the same virtual uh private cloud that we used for ec2 instance whatever network we use for ec2 instance we will use the same network for database as well so that both of the web server can connect to the database server then subnet group public access we don't want public to access it subnet group we'll choose default vpc security group choose existing existing vpc security secretary for security group we will choose the one that we created which is whatever security group uh our instance is using let me see the name of the security group so here security launch wizard1 so we will also use the same launch wizard1 here it shows you the costing monthly costing as well for the settings i chose here above the disk space the size of the rds server and everything it calculates the estimated monthly costs and shows it here so currently with my settings it will charge me around 30 dollars per month once this is done i will go ahead and create the database and now database server is it is creating a database server this takes some time so in the meantime let's go back to the ec2 server where we will run the web application so you will see that our laravel output test server is up and running it is showing us running now let's ssh to the server and see whether it works or not so i will select this server i will then copy the public ip it assigned to the server so that i can ssh i'll copy it and then i will ssh to that server since we chose the ubuntu based operating system ubuntu based image the default ssh username will be ubuntu so ssh ubuntu at the rate servers ip and then i have to select i have to specify the private key that i created when i launched the server so it is this key so here you go i have successfully created the server and i now i am logged into the server this is the web server that we created inside ec2 now let's tally start installing a various softwares that we need to run a laravel application what all things do we need to run that well well first we need the web server which is nginx so before that what i will do is if by default when you create a new ec2 server and if you look at the ram this is one gigabytes of memory approximately it doesn't create any swap space now what is swap if you are someone who is coming from a linux operating system then you will know that linux has this concept of swap where in the actual memory actual ram if it gets filled then it puts the applications in the swap so since it is zero bytes what we will do is we will first create some swap space on this linux operating system so let me run few commands quickly to create swap space so i am creating a swap space of 4gb at slash swap file path then i will change the permissions to 600 so that only a root user can read and write to this file then i'll run this linux command which will convert this file to a swap file mk swap so it shows this information that this file has now been converted to a swift file and then i'll run this swipe on command to instruct the operating system to treat this file as swap space and now if i do free minus edge you will see that apart from our 1gb ram it is showing swap space of 4 4gb because we created 4gb swap file here so now what will happen if we are installing softwares if it's if you are running some process which is taking more than 1gb it will automatically start using swap and not give us the memory exhausting error now let's install nginx so for that we will first update apt app is the package installer for ubuntu app has been installed then let's run upgrade command quickly wearing all these commands inside the ec2 server this will take a minute please bear with me uh so once apt has been upgraded then we will install nginx web server okay now we will run the command to install nginx and then we will run command to install php after engine x we need to install php so we will be installing php8 so for that we will add this repository which is called as own rich slash php and then we will run update once again and then we will run upgrade once again so that the newly added repository updates the packages that were already installed and then we will run command to install all the php packages we are installing php8 so these are all the dependencies required by laravel okay so now if you see you you we should find php install if i do php minus v i i can see php 8.0.10 installed okay now let's create a directory somewhere now first let's point the domain to the server so for that you need to change your dns i'll quickly move the dns window in here okay no not this one okay so my dns manager is cloud fair so from here uh i'll be changing the dns for laravel.com i need to put the same ip public ip that was assigned to my ec2 instance so i'll copy this ip and then i will assign it here this i am changing the dns of the domain so now laravel dot com domain name will point to this ip which is this ec2 instance this is that is ultimately that request will come in to this server [Music] so let's open a page and see if it works or not now if i put level 4.com i should see something yes it worked so this is the default nginx web page how this came because when we installed nginx on the server it creates a default landing page as well so now let's install our laravel application we are done with installing all the dependencies now we just need to install the laravel application so for that what i will do is first i will create a directory in wedding views laravel.com this is the directory we will keep a laravel application and then i will go to slash www we hosts this is the directory we will be keeping our web application this was created with root user so let's change the owner for changing the owner the command is ch own i want to change the owner of the directory to ubuntu i need to do it with sudo now if i look at the permissions the directory is now owned by ubuntu user now i can either clone my laravel application in this directory or if i'm starting a new project then i can install the laravel application through a via composer into this directory so let's install composer quickly as well because we need composer so [Music] this is the command field writing body why okay because i am in a directory which was not which didn't have permission for ubuntu user to write so now i'm inside the directory i will get this file and then i'll run the composer setup then now i should have the composer command installed so here you go next what we will do is we will using composer we will create a new laravel project in the current directory so a new fresh empty larvae project will be installed inside this directory in the meantime let's open laravel's documentation and copy the configuration that we will need for nginx to point the domain to that directory so if you go to a documentation deployment section here you will find the configuration that we need to put in the nginx config file so i'll just copy the configuration as is okay then i'll go back here and i'll first you will see the composer create laravel project is successful now i will see all the laravel directories in this parent directory now we will configure nginx to point the domain larvaldropper.com to this directory so let's go to nginx directory sites available we need to create a new config file here let's call it laravel we will paste the configuration that we copied from literal documentation we need to change a few things server name server name is a domain that we will be using so we are using laravel nagpur.com so we will put that same server name will not put dot com we want to point this domain to which the directory slash square www application is inside where www we host laravel dot com slash public will keep everything else as is and i think we should be yeah one thing uh since we are using eight php eight we need to change this path to php 8.0 so we are using php 8 fpm i'll quit the configuration then i need to go to sites enable directory and create a symbolic link from sites available to sites enabled sites available laravel to not so now if you see in the site's enable directory [Music] what happened was it not showing assembling let me okay okay i didn't create a symbolic link because i made a mistake so let me remove the file first s2ln minus s insides available lot of it to current directory so now we have copied over ours with a symbolic link of the laravel configuration file to size enable directory here you go so now if we restart nginx it should pick up this site and it it should then point laravel.com domain to our folder to our web root so let's restart nginx as you do service nginx restart and now if i go to this page this should show laravel's default landing page okay so it is showing some error because this file is not writable so we need to make storage folder writeable by the web server so we will go back here we will go to the web route that is wwv host laravel.com [Music] and what we will do here is we will make this storage folder editable by web server for that we will make web server as the owner of this storage folder so see your own minus data storage operation not permitted because i need to do it with sudo so now storage server is owned by www data user which is the nginx user so now the web server should be able to write to this storage folder let's see voila so now our laravel application is up and running on an ec2 instance and if i now go back to rds our database should have been created so our database is also available now if i want to connect to this database from the laravel application then i will need the hostname so this endpoint is the hostname so i'll go back to laravel application i will edit the enb file and db connection the host name is the endpoint shown for the rds instance that was created so this will be the hostname database name whatever database name we want to create username will be the username that we put in when we created the database rds database so we had used for database username we had used admin of course this is the demo that is why i'm using the admin root user as is otherwise what you should normally do is once the database server is up and running you should create another dedicated database user for your application with very limited permissions and that database user should only be able to write and read from the application database and nowhere else but for this demo purpose i am to save some time i am just using the root user directly and the password we use was laravel nagpur so with this we have configured our application to use rds database as well so i think that is it i know i moved very fast i'm not sure if you guys were able to understand what i was saying but if you have any questions please feel free to ask me now anyone any questions there seems to be a such a lengthy process that i can say i just wanted to ask one thing today we were about to discuss the testing part right uh yes uh yes ingame actually what happened is uh today's uh talk was on uh was to be delivered by gabriel from nigeria but for some reason he he was facing some internet issues and he dropped out in the middle and he wasn't able to connect back so what i did is to fill in i just chose this topic to present okay that's great and do we still have time or uh are we gonna wrap up now no we are going to wrap up now okay unless uh someone has anything to say or present and yes your concern was that this is such a lengthy process and yes it is a lengthy process so the reason i showed this is just to uh just to demonstrate and uh how all the tools can be installed on ec2 instance otherwise nowadays what happens is we get ready-made uh services available like for example laravel forge so in laravel forge what you need to do is you just need to clear an account a little forge and click one button and it will create the server for you it will install all the required dependencies softwares web server everything for you so most people choose that nowadays but being a laravel developer or being a php developer one should know what all things goes behind the scenes if you're using a ready-made service so that's what i showed all right thank you very much okay then if no one has if there are no further questions then let's wrap up and once again i'm sorry for today's fiasco uh i think there was some issues with gabriel's internet connection and that's why he dropped out and i had to fill in and since i was not prepared for this so i went very fast and uh yes we were uh last last meetup i said that we were going to uh the best php he was going to present a topic uh this month but uh for some reason he was unable so we have pushed that meetup to the next month so luke luke downing from the best php team he will be presenting a topic uh on testing using a pest php uh in september's meetup now just wanted to ask one thing that might have you about right now is that like being a developer what do you prefer do you prefer to write tests as you write code or you do the testing and write test cases after you are done with your coding part uh well ideally people do tdd that is test develop a test driven development they first write tests and then they write code but that following that methodology it is really difficult really difficult to write test to think of everything upfront write the test for that and then start coding so even though as much as we like to follow tdd but in practical world it is very difficult and most of time what we do is we first write the code we first implement the feature and then we write the tests around it please please complete yeah that's the sad truth about it uh i spoke to uh i spoke about the same with one of the developers who's my friend actually he's not a developer he's a tester but he said if you want to be a developer you shouldn't be focusing on tests that much rather you should be focusing on development so being a beginner or let's say an as an intermediate level if anyone could answer this like what should be my approach or what what exactly should we go ahead as an beginner or intermediate should we try our hands on with tdd like test driven development or should we focus more on our development part and like think as a testing is not our piece of cake or not our job to do this is some tester that will do it um however any any kind of you know suggestions are welcome please sangam so uh there are two kinds of testing actually one is functional testing wherein uh in large projects you see developers as well as qa engineers working on the same project and these qa qa engineers they are not developers at all so they are responsible for functional testing that is they open a web browser let's if we if we are talking about let's say any web application so how will they test they will open a web browser they will do everything manually so that is normally what is done in most of the projects and in tdd if you are talking about tdd then tdd is solely developers responsibility there is no qa separate query engineer involved in that because for tdd it is the developer who knows how to write tests in the code a qa engineer will not be able to write tests at all they will they will only be able to execute the tests so uh for beginner if if someone is picking up web development then i would suggest of course they can directly learn programming first they can focus on that they can keep tdd for later on but if you're working on a large scale project and an enterprise project or let's say any medium size project as well then ddb tdd becomes very important if not tdd if not in true sense but at least some kind of test so that you can make changes without worrying about breaking anything else i was able to answer your question yeah yeah completely i think i i have my answer at least for me i do have my answer what i need to do yeah so if uh if someone is saying that uh testing is not a developer's job then they are partly true and partly wrong testing the code using code coverage and writing unit tests that is definitely developer's job and doing functional testing manual testing that is a separate qa person's q engineer's job yeah and just one last question i'm sorry i'm taking it just wanted to act like a recently there's a lot of noise about github co-pilots so uh being a beginner should i be afraid of it like um i mean will there be a job in future for web developers or this is going to collapse will become this is very difficult to answer sangam and to be honest uh i don't know the answer at all uh it will just be my opinion uh you want to make uh if let's say the ai is coming into picture then we need developers to run the ai program as well so but i think that is an entirely different domain don't you think it is it is but i don't think it will it is going to replace uh manual programming anytime soon the next 10 15 years it is i don't think it's my personal opinion it's not possible to replace a human entirely yeah at least in the next 10 15 years so if you're talking about next uh a decade or two then it's uh we shouldn't worry about it and we should learn programming yeah okay i think let's wrap up thank you everyone who joined see you next month bye bye have a nice day nice evening
Info
Channel: Laravel Nagpur
Views: 586
Rating: undefined out of 5
Keywords:
Id: flQ-KtaV6HU
Channel Id: undefined
Length: 43min 20sec (2600 seconds)
Published: Tue Aug 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.