Deploy Laravel Project On Live Server - Digital Ocean

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey guys, welcome to LaraTips. In today's video  i will be showing you how to upload the laravel   project to a dedicated server here in this  tutorial i will be using digitalocean but you   can use aws ec2 instance or anything whenever  you log into a digitalocean dashboard you can   see on the left hand side a list of projects  if you have created or if you haven't created   then it will show here empty just click here  on the new project and click the name of the   project let's say laravel like this and in a  purpose we'll just see here website or blog and   we'll just create a project it is saying here move  resources into laravel we don't need to do this   let us just click on skip for now now here we will  see this page just click here on the get started   with the droplet and here we will just choose the  basic one the regular intel ssd and dollar five   per month with one gb of ram and one core cpu  25 gb ssd and 1000 gb transfer or bandwidth and   let me just scroll down and if you are located in  any of this country or you are nearby this country   just select the one that is nearest to you but in  this case i am just selecting new york because it   is just for testing purpose and we'll just keep  this unchecked but if you want these additional   features then just click on them so here we'll use  ssh key will not use the password and here it is   showing me this lara tips because i have already  created ssh key before so i'll just uncheck it   and i'll create a new ssh key here and you can  see on the right hand side here we can just   copy this code and we'll go here in the terminal  and go inside this .ssh directory just do cd   like this and just enter into this .ssh directory  and just paste that ssh-keygen and hit enter   and here it will ask for the file  name and if you have multiple   projects then just give it the name  of that project so here we'll keep laravel like this and for now will not add any  password the ssh key has been created now we   have to just copy the contents of the public  ssh key into this box so here we'll say cat   laravel.pub like this and here we can see the  public ssh key content so here we'll paste it   and here i'll give here laravel like this  and i'll just add that ssh key over here   and this is automatically selected and just  keep everything as default here also you can   change the name if you want i'll just keep  them as default and if you want to enable   the backups which is dollar one per month then  you can do that so here i'll create a droplet now it will take some time to create a droplet and  i'll be back when it is created the droplet is now   created and we have this ip address now we have to  log into this server using ssh now let's see how   to do that so here let me just clear it and in  the same .ssh directory just create a file name   config so you just do touch config like  this i have already created this i'll just   show you the content of this file so i'll say vi  config like this and it will open that file and   now here i'll just paste some code over here  so you can see here host hostname user and   identifier file so this hostname you can give  whatever name that you want we'll use this to   ssh into the server so this ip address is  the same ip address that we can see here   the user name is root because whenever we create  a droplet our user root is automatically created   and this identity file is this one from the  home directory .ssh/laravel and this is the   private key path. whenever we executed the  command ssh-keygen it created two files laravel   and laravel.pub the content of the public file we  added to the droplet in the digitalocean dashboard   and the private key will add here and you should  not share your private key to anyone just keep it   to your computer so now i'll just save it now i'll  just do ssh and then the name of the host which is   laravel and it will ask me are you sure you want  to continue connecting then i'll just say here yes   then you can see here we have successfully  logged in to the server i'll clear this and   whenever you run who am i then you can see here  the current user is root or you can also see here   the user is root so we have now successfully  logged into our server whenever we log in first   of all we need to update it so we'll see here  sudo update and sudo apt upgrade -y whenever   we write -y then it will not ask us yes or no  question everything is now updated now let me   just copy this ip address from here and just paste  it over here and here we'll see this site can't be   reached because we haven't installed any server  so we'll install nginx server so let me go here   in this link and i'll be leaving this link just  below that like button this is the link to the   ppa of the nginx and whenever we install this we  will be able to get the latest version of nginx.   i'll just copy this and let me clear it and  paste it over here and hit enter the nginx   stable ppa has been added now we can run this  sudo apt-get update but before that let us also   add a ppa for the php if we do not add the ppa  then it will take i think 7.4 version of the php   but if you add this ppa then we can install the  php version 8 so i'll just copy this and i'll   be leaving this link as well just below that like  button and here i'll just paste it and hit enter and again we have to hit enter now for the  php also ppa is added now we can run this   sudo apt get update so now everything has been  added now let us install the nginx and guys i   will be leaving all these commands step by step  just below that like button in the description   nginx is also installed we can  verify this by saying here curl   localhost like this and we can see this html  over here and if i go here in the browser   and reload then you can see here welcome to nginx  which means that our server is running now also   you can just do here sudo service nginx status and  you will see here this active running status so   now the nginx is installed all the configuration  file for it is located inside this etc nginx folder and if i show you all the files and  you can see here all the files is located over   here so this is the file that we need to look into  so let us just see over here nginx.conf so you can   see here all these things are over here and here  at the bottom so you can see here it is including   all the files inside this conf.d directory that is  ending with .conf and also it is including all the   files that are located inside this sites enabled  so we can add a new file in this directory sites   enable directory or this conf.d directory and  we can just configure it as for our requirement   so there is also one default file located in this  directory and we can modify that as well but let's   say we have multiple websites in the same server  then we have to create multiple files but in this   tutorial i'll be only showing you for one website  so i'll just exit this and let me just go to   sites enabled and list the files so you  can see here the default file so this is   not actually the file but it is pointing  to the file which is located inside this   sites available and whose name is also default  so if i just go back one directory and sites   available and if i just show you the list file  so you can see here this is the actual file so we   have to edit this then everything that we change  here will take effect so i'll just open it by vim   so you can see here all the content of this  file so let me just go to the top for now so here you can see here it is listening to the  port 80 which is http and this is the default   server which means that whenever we enter the ip  address or the domain that we add to our digital   ocean it will listen to that and this weird syntax  is for ipv6 and i haven't configured it so i'll   just delete it and you can also delete that but if  you need that then you can just add it over here   and everything over here for ssl certificate  configuration but we'll not see that in this   tutorial and you can see here this root is  pointing to a folder which is var/www/html and   the thing that you are seeing here in the browser  is coming from this html folder and in this folder   it is looking for these files and by default  whenever we install the nginx this file will   be present in this html folder so it will just  display this html file which is this i'll show   you that as well and you can see here the server  name is underscore so if you only have one domain   then you can just keep it as it is but if you have  multiple domains then you can write the name of   the domain over here so let's say our domain name  is laravel.com so we'll write here laravel.com   and www.laravel.com but here i'll just leave  it as it is so this location slash is saying   that whenever we visit our website it will try the  url whatever we have entered over here so for now   it will just try this ip address and if it is not  present then it will look inside the directory and   if there is also it is not present then it will  just show the 404 file so let's say here slash   random then it is showing us this 404 because  first of all it looked at this file but it didn't   find and then again it looked at this directory  then the directory is also not present so it is   showing us this 404. we'll modify this a little  bit later and you can see here all this code over   here and i'll explain you whenever we'll configure  it for the laravel so i'm just showing you briefly   what these are so here i'll just close this  file for now now let us install all the php   extensions that we need to run laravel i'll paste  the code over here and as i have said earlier i'll   be leaving this code in the description  so i'm just doing here sudo apt install   -y and i'm adding this -y so that i don't  have to write yes yes for all these extensions   and all the name of the extensions that we need  for laravel so i will hit here enter apt install   command not found. so i mistakenly added this -  over here we don't have to write this - so i'll   remove this - and press on enter now that all the  php extensions are installed now we we need to   install composer but before that let us see where  php is installed so it is inside cd slash etc php   and if i just list out the files then you can see  here 8.0 and if i just list out 8.0 then you can   share everything that is installed over here  i'll not go deep into all the folders but you   can see that it is installed over here now let us  install composer so this is the composer website   and you can install the composer from here just  going to the downloads i'll be leaving this   link just below that like button as well  so i'll just copy this code the first line   and i'll go here and i'll just go to the  home directory cd ~ and i'll paste it   over here it will not show any feedback but  this code has run successfully and i'll just   copy all the content in the second line and i'll  paste it over here the installer is verified   whenever i ran this first command then it just  added this composer-setup.php file in this folder   so if i show you here composer-setup.php is over  here now we have to run this third command but   don't just run this command directly i'll just  paste a code over here so you can add it like   this if you do it like this then you can just  access the composer by writing just composer   otherwise you have to write composer.phar and  just install this in the user bin directory so   that you will be able to access the composer  from anywhere now you can see composer is   already installed and i forgot to show you the  nginx version and php version so let me show you   that also nginx version is 1.18.0 and php version  is you can see here 8.0.9 and composer version is   i think you have already seen that so let me  just run it as a sudo 2.1.5 so it is just saying   us not to run this composer as a sudo user  because whenever you install any third party   packages then they will all have the root access  so for that it is saying us not to install the   composer as a root and here we are only looking  at the version so it doesn't matter okay now we   have already set up nginx which is the server  the php which is required to run laravel   and the composer which is required to install the  dependencies of the php and again let me show you   the content of that nginx file so here i'll say  vi etc nginx sites available and default and you   can see here all the files and folders are located  in this var/www/html directory and since we were   the root user whenever installing everything  everything is only accessible by the root user   but now we'll create a new user which will be able  to access all the website related data only so let   me just quit this we'll create a username laravel  i'll give it a password and we can just hit enter   on all of these and we should also give the sudo  privileges to this user because we want that   user to access the all the root permissions  by using sudo command now if you go inside   slash home and if i show you the content then  you can see here the user is present over here   and whenever this user logs in then he/she will be  automatically sent inside this folder if we create   a ssh key for them as well and provide it to them  then they'll use that ssh key and whenever they   log in they will just be shown whatever is inside  this laravel directory so let me go inside that so this is the user and now i log in  as a laravel user so i'll say here su   laravel like this whenever i am logged in as  a root user then i can log in as another user   by using the su command and after su you should  write the name of the user and if i just type pwd   then you can see here we are inside this laravel  directory now let us install the project over here   you can just clone a laravel repository and  add it over here but i'll add default laravel   project. so i'll just go to the browser and i'll  just copy this and paste it over here and i forgot   to copy c and let me give the name as laravel as  well but it can be anything it can be let's say   xyz.com or anything that you want so now i'll  install it like this now if i show you here   the content you can see here laravel and it is  created as the laravel user and laravel group so   if i go inside this laravel folder and show you  the content then you can see here all the thing   that is related to the laravel is present  over here but if i come here in the browser   and just reload over here by going in the  ip address then we are still seeing this   welcome to nginx now let me show you how we can  show that laravel website so i'll just do here   pwd like this and i'll just copy this directory  now let me login as a root user now so now let me   do the changes in that nginx config file so  i'll say here vi slash etc nginx sites available   and default now here in the path i'll just  paste whatever i have copied from that pwd   and i'll add extra public over here because the  root of our level project is inside the public   folder so i have to add this public over here  as well and also let me remove this ipv6 address   now we also need to do some other changes because  we are dealing with php files in the laravel   so instead of this index.html and every other  thing over here let us just write index.php   and let me keep this index.html  over here and i'll write here   index.php so it will look fast if there is  index.html available and if it is not available   then it will look into this index.php suppose  let's say at any point of time we want to show   something static page in our website then we  can just create index.html file then it will   automatically show that instead of this index.php  so let us keep it now what should we write here so   let me go here in the documentation and i couldn't  find that in this eight point x but if i go here   in the seven point x and this installation then if  i just go here at the bottom then we can see here   we can add this thing it was provided  here in the 7.x but i couldn't find in the   8.x so we'll just copy this and i'll just  come here and i'll just paste it over here and i'll just remove that one okay we have done  this and here also we need to uncomment something   so i'll just go here in the beginning and  i'll just remove this one and i'll just   remove this comment and one thing that we  need to remove is this comment as well as this one will keep every other thing just as it  is and this is all the php-fpm configuration so   there will be two things one will be php cli and  one will be php-fpm. the php cli is the php that   is executed whenever we run php on the terminal  and php fpm is whenever we visit some page in our   website then it will use the php-fpm. so this is  this one but we installed php version 8.0 so we'll   use here php version 8.0 not 7.4 now i'll just  click on escape key :wq it will write and quit   so we have updated that configuration file now to  check whether that configuration is correct or not   we'll just write here sudo nginx -t and you can  see here the syntax is okay which means everything   is good now let us reload the nginx sudo service  nginx reload now if i come here in the browser   and just reload this now we are seeing something  that is related to laravel which is the stream   or the file and it is saying the permission is  denied so let me show you why is this happening   so here if i just so if i just go  to cd slash home laravel and laravel   and if i just show you the content then  the user and the group over here is   you can see here the user is laravel and  the group is laravel but if i show you here all the running processes which is php then you  can see here it is running as www-data user so   because of that it is showing us permission denied  now let us change this www-data to laravel so let   us go to this etc php 8.0 fpm pool.d www conf file  and let me just go to the bottom and you can see   here user is www-data and group is www-data  so now let us change this so here we will say laravel and here also we'll say laravel like  this and we'll just write and exit now we have   done changes in the php fpm configuration then  we need to restart that as well so i'll just   write here sudo service php8.0-fpm restart and  hit enter now it has started the php fpm now   let me again show you that processes now you can  see here we can see this laravel over here before   there was www-data and now here it is laravel now  if i come here in the browser and again reload   now you can see here we can successfully see our  website now we can successfully see our website   but now we also need to configure the databases  now let's see how to do that for that we need   to install the mysql so let me clear it now  let us install the mysql server like this   mysql server is now installed  now let us see the version of mysql and now you can see the mysql version  is 8.0.26 we have installed the mysql but we   need to secure it as well so now let us run this  command sudo mysql secure installation like this   now it is asking me in order to set up this  validate password since i am just doing it for   testing and i will just remove this droplet  after this tutorial so i'll just hit no but   in your case just type yes and hit enter and just  add a very difficult password so here i'll say no   and i'll just add a random password over here but  in your case just add a very difficult password now i'll retype the password and hit enter remove  the anonymous user yes disallow the login remotely   yes we don't want any want to remotely log  into our mysql server but if we want to log in   from a specific ip address then we can set up that  so here we'll say yes and remove the test database   yes reload the privileges yes so everything is  now installed now we have to create database and   a user in a mysql as well and we have to link them  together and we'll use that in our project now   i'll log into the mysql server so here i'll say  mysql -u root -p and here i'll enter the password   that i have used whenever i ran the mysql secured  password installation so i have successfully   logged in now let me create a database so here  i'll say create database laravel underscore db   and if i run show databases then you can see  here laravel db has been created successfully   now i'll create a user and the username will be  laravel_user@localhost which means it is inside   this server and identified by a password this  is the actual password of the user but in your   case you must write very difficult password  over here the user is also created now we   have to give permissions to this user to access  the database that we had created so here i say   grant all privileges on laravel_db.* so only to  this database we will give access to this user   now that's also done and finally we'll flush  the privileges so this is done now let me exit   from here and now let me again login as that  laravel user so i'll say su laravel like this   and if i just show you the list of the files i  am already in this project so you can see here   we have this dot env file i'll just open this dot  env file and we'll change the data over here so   the database is laravel underscore db and the user  is laravel underscore user and the password is   password also one thing that you need to  do is whenever you are adding something   in the production just turn this app debug to  false otherwise you'll lose all the sensitive   information that are present in this .env file  and in this app url also you need to write the   domain that you have and here app name you will  just write the name of your project and here in   case of local you'll write production now let me  save it i'll just click on escape key :wq you can   see here at the bottom over here now here i'll  hit enter also let me do a config clear one time and it is saying could not open input file  or sorry the spelling of the artisan is   wrong so i'll say here artisan config  clear and let me also do cache clear now if i come here in the browser and just reload   you can see here everything is working just  as before now if i run php artisan migrate   and you can see here all these users table  password resets and failed jobs tables that are   by default present in the laravel are also now  migrated and now let me show you once again by   logging into the mysql and here i'll say  use laravel underscore db and i wrote user   so here i'll say use laravel underscore db so  tables like this and you can see here all the   tables that are present in this databases are  shown over here so now let me just summarize   what we have done in this video we just created a  droplet in our digital ocean then we created ssh   key and then logged into that droplet using our  terminal using the ssh then we installed nginx   and php using the ppa and we saw the default nginx  page whenever we entered the ip address in the   browser then we installed all the extensions the  php extensions that are required to run laravel   then we installed composer after that we created  a laravel user and just uses the su command to   login as that user and then created a new laravel  project over there and in the nginx configurable   file we added the path of the new project that you  have created and as well as slash public in the   end because that is the root of our larval project  and then we restarted the nginx and then reloaded   the pace but it showed us permission issues and  we saw that php fpm was running as www-data user   and our project was using laravel user then we  changed that www-data user to the laravel user   from the php-fpm configuration settings and then  we restarted php fpm after that we were able to   see the laravel default page and then we installed  mysql and we installed mysql secure installation   and then we just added the password to the root  user of the mysql then we created a database and   a user specific to our project and then added that  in our env file and on the php artisan migrate and   finally we saw the list of the tables that were  migrated so this is just the beginning of your   journey of adding a project to a live server there  are so many things that you need to learn guys   so here we haven't added any domain we are just  using the ip address then we also need to add the   ssl certificate we also have to look into caching  gzip http 2 so many things that we have to learn   and if i have enough time then i'll create  a tutorial for those as well in the future   so just stay tuned and that's it for this video  guys thank you for watching have a great day bye
Info
Channel: Laratips
Views: 2,781
Rating: undefined out of 5
Keywords: laravel 8 tutorial, laravel 8, laravel tutorial, laravel advanced, laravel 8 advanced, laravel advanced tutorial, laravel 8 advanced tutorial, php, php tips and tricks, tips and tricks, laratips, laravel, server, nginx, deployment, laravel deployment, ssh, mysql
Id: ww_oVrL9ohA
Channel Id: undefined
Length: 26min 59sec (1619 seconds)
Published: Sun Oct 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.