Laravel 5.8 – From The Ground Up
Lesson 32 of 52 · Laravel 5.8 Tutorial From Scratch – e32 – Deployment: Basic Server Setup – MySQL, PHP 7 – Part 2 · Education
Laravel 5.8 Tutorial From Scratch – e32 – Deployment: Basic Server Setup – MySQL, PHP 7 – Part 2
welcome back this is part two of deployment of a laravel application if you didn't catch the first part I highly recommend you follow the steps in step
one before moving on to step two we're gonna get right to it we did a big introduction in step one so we don't need to talk too much about what we're actually doing just to recap in the
previous episode we set up our droplet set up a firewall setup OpenSSH and nginx and this is the page that we got so we are hosting this page inside our droplet and you can see it here right in
the IP we are able to fetch this nginx default page all right so let's head back to the guide and keep going up next my SQL of course very important let's go ahead and copy that all right so I'll
paste that in now and let's install MySQL it's gonna ask you for the sudo password let me enter that now do you want to continue hit Y enter and it's gonna install and there we go all right
let's head back to the guide so up next we're gonna run this MySQL secure installation this actually ships with MySQL and the point of it is to ready MySQL for production environment when it
installs as a default there's a lot of extra tables and a lot of open permissions to allow you to get into it as easily as possible but for production we really want to
lock everything down delete all the extra fluff and just leave exactly what we need and that is just what it does so I'm going to copy that command head back to the terminal and run that now so this
is gonna run through a series of questions we'll walk through the questions and talk a little bit about each and then I'll give you my recommendation on what to choose so the
first one is validate password plugin so the validate password plug-in what it does is it tests passwords and sees if your mysql password is secure enough in our case we're gonna say no we don't
really want to have that so I'm gonna just hit no for now remember our droplet is very secure to begin with because we disabled passwords to actually get into it so there isn't a huge risk for
somebody to actually into the droplet itself the risk would be more on my sequel injection and stuff like that so we'll hit no on that so the next one is we need to set a password
for the routes so I will just type a very simple password for mine but this is just for demonstrations in a production environment I highly recommend you make a very long difficult
password with special characters and numbers and digits uppercase lowercase really mix it up for mine I'm just gonna make it password okay reenter password next one is cleaning up
any users that my SQL ships with that we don't really need do we want to remove those click why for yes and then it says do you want to disallow route log in remotely now this is a tricky one I use
table Plus or sequel Pro to access my database from my local machine if you turn this on you will be able to do that but if you disallow it then you will not be able to do that I like doing that and
accessing my database so I'm going to click no on that then the next thing is do you want to remove the extra database that it ships with will hit yes for that do you want to reload the privileges
table now and you can say yes for that that way it applies all of the changes that we just made and then that's it we're all done getting MySQL ready for production let's go on to the next step
so in the guide I do run you through each of these you jump ahead and we are right here so now we're gonna enter the MySQL CLI this is basically the command-line tool from MySQL and there
we are we are inside MySQL awesome in this next step we want to make sure is that our root user actually is able to log in and use the database so I'm going to copy the next command let me paste
that in and so what we see here is that our root user has the plugin of auth socket as opposed to the native password we need it to have a password that way our layer of application can actually
connect through the root user so let's change that now to do that we're going to alter the user root at localhost and we're going to make sure that we set a password so notice here it says strong
password here this is just a placeholder for what actually want to put in there so let me change that now and we'll say that our password is just gonna be laravel all
right and then finally let's go ahead and make sure that the changes that we applied did in fact have an effect and so we see that now this is before and this is after so we see that the root
user has the MySQL native password great all right now let's flush the privileges what this will do is actually apply the changes that we just made and now we can just exit out of here so now we can no
longer do sudo MySQL you can't do that anymore because you need to have a password a password is required from here on out so the way that you would enter into that CLI tool that we just
used is you have to say – Q – u the U stands for your user so my user is root and – P hit enter and now it asks for our password my password I used larvae all lowercase Solera though and there we
are we are back into MySQL you can hit exit and we're back to our shell all right let's keep going up next we need to get PHP and get the basic nginx configuration done so first and foremost
is we need to add a repository where we can find PHP let's add that now and now let's go ahead and install PHP do you want to hit continue hit why this might take a minute we'll be right back and
there we are so the next step actually gets us to create a site in our droplet for our domain so let's create that now I'll go ahead and paste that in and replace your domain comm with what I
know my domain is gonna be you may not have one and that's perfectly fine I'm gonna show you how to set this up without even having a domain but I know I'm gonna have a subdomain in coders
tape dot com so laravel coders tape com that is my domain that I'm gonna use for this project so inside of here we just need to paste everything that is inside this block now there is some placeholder
text such as the server name right here and that's the only thing that we actually need to change let's paste that in and then go through it very quickly so this is setting up a new server in
nginx we're gonna be listening in port 80 remember every HTTP request and goes through port 80 then the root of our files is gonna be under VAR dub dub dub HTML remember we haven't set up our
letter of our project yet so we are just relying on the nginx configuration for this so we are going to be looking for an index or an index that PHP file or an index that HTML that's the order and so
on and so forth so our server name of course will be our IP since we don't have a domain just yet so we'll replace that with our IP I'll go ahead and paste it in and then location we're gonna try
files in the URI or we're gonna return a 404 if we are able to find the file if it ends in dot PHP then we're going to go ahead and use the fast cgi PHP config file and run the files through the PHP
parser before returning it to the user if the files instead start with dot HT as in HT access we're going to go ahead and deny all HT access is not nginx HT access is Apache and that's a totally
different thing so nginx is gonna ignore any requests for an HT Access file awesome so the only thing you have to change is just your IP up here not mine alright let's hit save on that remember
that's : WQ enter and that will get you out of them and save the file so the next step is we have to get that file that we just created and create a symlink from sites available which is
where our file is to sites enabled and this is the way that nginx will know what sites are available so let me copy that code we're gonna have to modify it but we'll paste it in
everything remains the same but of course your domain comm is just placeholder like I said before so here we need to type in layer vocoders tape comm that's what I'm using you would
type in your own domain again you don't need a domain for this this is just what we're really calling this file just use a URL that eventually maybe will be the domain for this
project we'll hit enter on that and then I'm gonna unlink this default so this page that we've been seeing here is the default I want to go ahead and disable that default okay copy that
I'm gonna hit paste and now I've included this command here this command sudo nginx – T will test the entire configuration of nginx if you get an OK that means that you
made no mistakes and everything should work according to plan so let's try that now hit paste and we get syntaxes okay and the test is successful so we are good to go
nginx was not able to detect any problems with configuration great let's keep going so now we're gonna reload nginx that way we can apply all changes and that's restarting so now I'm gonna
create just a simple info that PHP file that we can access and make sure that PHP is actually being parsed so let's do that now inside of here this is gonna be a very simple PHP file and all its gonna
do is just PHP info and that's it nothing else we just want to be able to see the PHP configuration file when we visit our droplet slash info dot PHP so now we can switch over and hit info dot
PHP and with any luck you see your PHP configuration file awesome if you see this page you are good to go we are running PHP nginx is finding it and we're able to parse PHP pages
great the next step is optional but I will run it what it does is removes the file that we just created we really don't need it we just wanted to confirm that everything was good to
go with PHP so let me run that now and get rid of that file great now let's dial in the level ecosystem so the basic setup of the server is done it's time for us to focus on laravel and laravel
specific configurations that we need to do the first one is that there are a couple of PHP extensions that laravel requires that we did not install one of them is MB string
and the other one is XML we also need composer and we need unzip none of these were installed just yet so let's go ahead and complete that hit paste hit enter it's gonna ask you if
you want to continue hit Y for yes and this might take a minute we'll be right back all right that's all done so the next thing I need to do is I need to create a database for
our laravel project so let me log into MySQL and remember the command is MySQL – you for user root which is our user – P and that's gonna ask for our password my password was laravel and there we are
we are inside MySQL so now let's create a new database called laravel with a default character set of utf-8 and unicode CI perfect hit paste hit enter if you wanted a different database name
just change level to whatever you want it to be so now we're going to grant complete access of that database to our user so let's do that now I'm going to hit paste so my user is not level user
my user remember is root so I'll change that to root and we are identified by my password which is laravel we are good to go on that I will go ahead and flush all privileges and exit out alright fair
enough we are good to go in the MySQL side so the next thing I want to do is I want to bring in my project I actually have my project up on github so we're just gonna
drag that in and drop it in so that we can see something on the page now remember our files are in VAR dub dub dub HTML because that's what we set inside our nginx configuration so we
need to follow that convention so let's head to that directory now and then let's make a new directory and I'm gonna call it first project alright let's head into that directory and let's make a new
directory called first it up next we need to change the owner of that project to the laravel user this laravel Arabo this is the user that you created in part one I use the name levo
if you used a different name you would have to change that name right now remember this is the user that we created in part one for our droplet perfect
I'll hit paste and now let's go ahead and clone that repository into that first project let's CD into first project and clone down that repository boom perfect of
course we need to do composer install and then we need to set up our environment file all right let's do that now composer install we'll give that a second to finish up all right that's all
done so now we have that dot e MV dot example what we actually need to have that copied over to a dot env file remember that dot e MV file is what holds all of the different
configurations for each of your servers when you're in local environment you're going to have certain variables in there and then when your production you're gonna have different ones so we're going
to copy the example file to just a dot e MV file and then let's vim into that dot e MV file and there we are there's a couple of things that we need to change here and the first one is this app
environment of course we are no longer in local we are now in production I set that to production and we don't want debug if you leave that to true then whenever you get an error letter I was
just going to spit out a bunch of information making your project at risk so let's definitely set that to false unless you are in a local environment so set that to false and that's it for now
now this app key to set that up we can run a PHP artisan command so I'm going to exit out of this file that's : WQ enter and we can run PHP artisan the same way that we did in our local
environment now there is this key generate right here and the reason why we haven't used that is because when you install level with the level installer it does it for you so we
didn't have to manually do it now when you install everything from scratch you have to do everything from scratch so let's run PHP artisan key generate and that generates a key force if we've then
back into that EMV file now our app key is set great all right let's exit out and let's go on to the next step so I did put part of the file here so the next thing we need to do is we do need
to set up the database for our project so let me actually go back into that same file and here's the database section down here alright let's set this up so my database that I created was
called laravel my username is root and my password is laravel alright so my database is set up so if we did everything correct we should be able to migrate our database now let's see what
happens it's gonna say your application is in production are you sure you want to run this command yes I do and there we are we migrated our database so we have successfully connected to MySQL
installed set up that user in migrated our database great let's keep going so as a next step I do put that key generate we already ran that step so don't worry about it now these next four
lines are required because of a permission issue you got to understand that when you are running letter though through the browser the user that is acting as PHP is a user called
dub-dub-dub – data right now we are under the level user so everything that we add is actually owned by that laravel user but we have to give up those rights and say go ahead and give all
permissions to these directories to the appropriate user and the appropriate user in this case is that dub-dub-dub – data user so we just need to run all four of these commands and we should be
good to go alright so let's do that now paste it's going to ask for your sudo password perfect let's keep going second one just copy and paste and then the next one
copy now this one you may have to modify I name the directory first – project if you named it something else make sure that you change that now same thing for this fourth command it says first
project right here if you use a different name make sure you change that perfect so we are good to go now we need to modify nginx remember when we set up our initial
project we were just looking to get into our project but we did not specify the level first project and all laravel projects actually have a public directory and that's the directory that
you actually access through the browser so we have to change that now you can either copy paste this in or you can just simply change that if you'd like I'm gonna go ahead and copy this entire
thing and change that inside our sites available all right so let's sudo then etc nginx sites available and then remember my site was called Larry vocoders type comm
in this one you would use whatever you were using all right so let's actually erase everything and I'm gonna paste that new snippet back in let me just modify the server name which is the only
thing that we have here obviously your domain comm is not correct you need to put the IP for your droplet I'll paste that in and we are good to go this file looks identical to before with the
difference is that now we are targeting the same directory structure but then we're going into first project and then into public this is a level specific thing that you need to know so every
project again has a public directory and that's the directory that we actually point the browser to so we'll add that and let's exit that's : WQ and let's move on so we're gonna run that test one
more time so that's sudo engine X – t for test and if you get an OK and successful then you everything right so now we just need to restart nginx at this point and if we
did everything right we can actually visit our server and we should get our sample project so we refreshed and we get this HTTP error 500 very cryptic not really sure what's going on but what I
bet it is is a permission issue this is something that a lot of users run into when they're installing layer buff from scratch and that is these steps that I put up here yes we ran through
everything but sometimes you need to do it again so let me just run all four of them again and let's see if we can get this resolved I'll paste that one and then the second one then the third and
then the fourth one more time it just looks like I need to add sudo in front of that last command and now we're good to go so with any luck let's refresh again perfect so we see our laravel
project so if you run into a little bit of a hiccup like that where you get that HTTP 500 go ahead and run those commands again and nine out of ten times these four commands will fix whatever it is
that's wrong with it so we are good to go so this is actually the app that we've been developing and playing with since the very beginning of this course so it's nice to be able to see it in a
production server as opposed to just being in our local development so that's great so the last couple of steps is let's encrypt which will give us an SSL certificate and there's some extra
credit stuff here that I added which basically just makes the prompt pretty now this lesson is getting kind of long so I'll split that off into part three so stick around we got some more work to
do I'll see you in part 3
In part 2, we are tackling MySQL and PHP. The time has come for us to start setting up our own deployment server with everything we need to start hosting our project. Follow along as we set everything up from SSH, Nginx, PHP, MySQL and everything in between.
For the best experience, follow along in our interactive school at https://www.coderstape.com
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
Full Deployment Guide:
https://gist.github.com/vicgonvt/cd0431a5cdc043ebab7f4954f7b4d471
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!