Linux Server Setup & MERN Deployment

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's going on guys in this video we're going to set up a linux server an ubuntu server from scratch and we're going to be deploying a mirnstack app to production now there's a lot of different ways and there's a lot of different services that you can use when it comes to hosting and there's certain platforms that can take some of the work out of it for you however the downside with that is you don't have complete control over the server so i really want to just dive in here and not just show you how to quickly deploy a mirn app this isn't like uh you know deploy your app in two minutes type video we're gonna create a server we're gonna set up some ssh keys create a user we're going to get our files onto the server with git we're going to set up nginx we're going to set up a process manager to run our node app so it's going to be maybe 30 40 minutes and as far as a server we're going to be using lenode which is a cloud hosting provider and we're going to set up a linux ubuntu container although if you want to use a different distro that's fine i'll be using ubuntu and lenode is actually sponsoring this video however the process is the same for any ubuntu server whether it's from some kind of cloud service or your local machine whatever it is and we got lenode to give us a 60-day 100 credit so that you can follow along with me absolutely free you'll get to use lenode for a couple months you can experiment install whatever you want on your servers so uh you can again use that use your own machine use a different service whatever you'd like so i just want to just quickly go over the plan for this video i do have a gist in the description that has absolutely everything it's basically a text version of the video it has all the commands we're going to run so you can use that as kind of a supplement but just to quickly go over the plan here first thing we'll do is set up a server and again i'll be using lenode with the ubuntu distro but if you want to use something else that's fine then we're going to set up some ssh keys so we have to generate some keys on our client machine and we have to add the public key to the server and we're going to log in using ssh keys as opposed to using just the password all right then we're going to create a new user because i don't want to use the root user so i'll show you how to create a new user for the server then we're going to get our app files onto the server using git and the application that i'm going to deploy is the goal setter app that we built in the mern series that i did about two months ago if you want to deploy something else that's fine but i'll show you how to how to deploy this application and we'll install any dependencies we'll set up a mongodb database real quick in atlas and then once we do that we need to set up the pm2 process manager to manage um the running of the application okay so we'll get it running on whatever port 5000 then we want to set up a firewall to block any ports except the ones that we need which are going to be the http port https and ssh okay and then finally we'll set up and uh configure an nginx server and we'll set up a proxy so that we can access our app through port 80 which is the http port all right and then also show you how you can add a domain through lenode obviously this this process is going to be different depending on what you're using what services but i will talk a little bit about that afterwards as well all right so that's it let's go ahead and get into it all right guys so this is the gist that i was talking about and i'll have the link to this in the description and it basically has everything all the different commands that we'll be running throughout this video so you can use this as a supplement to the video and then this tab is the mirn tutorial repository it's the goal setter app that that i did in the the mern series so that's what we'll be deploying and then we're going to need a mongodb database so we'll just quickly set up an atlas database later on but first things first we need to set up our server like i said i'm using lenode but you could use anything you want and this link right here is actually lenode.com traversie which as i mentioned will give you a hundred dollars credit so you can you know follow along with this tutorial absolutely free if you want so i'm just going to log in here with one of my accounts and then in the dashboard we're going to see this is where you see your list of what are called the nodes which are basically just linux servers and i want to create a new one so i'm going to click this button here and you can choose from all different all different distros here i'm most comfortable with ubuntu so that's what i'm going to use i'm going to use the 22.04 long term support and then the region closest to me i'd say is new jersey and then as far as packages i'm going to use the dedicated 4 gig okay so this is uh it has an hourly cost but it it caps off at 30 a month and remember if you use that link you have a hundred dollar credit so you have a couple months and then for the label let's just change that we'll call this youtube dash just call it youtube dash linux and then the root password so this is going to be the password for your root user so let's go ahead and add that and then for ssh keys i'm going to show you how to set this up you don't have to do this you can just go with the password but it is a bit more secure if you set up ssh keys and i just want to show you how to do it so i do have one stored called windows pc but i want to create a new one for the machine that i'm on right now so if i click add an ssh key i get this text box here and it's asking me to put in a public key so you have to generate your keys on your your machine so i'm going to open up a terminal here and before we generate a key i just want to show you where they're stored so they're typically stored in your home directory whether you're on mac or windows and then in your it's a hidden folder called dot ssh so if i list out here let's say from my home folder slash dot ssh you'll see i have a couple here for github okay and it's going to be a private key and a public key that's what we're going to generate if i wanted to i could use this public key but i like to keep things separate so what we'll do is let's go ahead and run ssh dash keygen and then what that's going to do is ask us where we want to save it and what we want to call it so by default it's going to be like i said in your users folder and then dot ssh and by default it'll call it id rsa now i want to change that so if if you want to call it something different you have to put the whole path here so we're going to do let's say slash users slash and then obviously your path will be different and then dot ssh and i'm going to call it id underscore rsa underscore lenode all right and then you can put a passphrase if you want i'm just going to hit enter and not use one all right so now if i do an ls in the ssh folder you'll see now i have a private key and then the public key has the dot pub all right now the public is what we want to put in here so there's a couple ways we can grab it i'm going to use the cat command which will just kind of print it out in the terminal so i'll do cat and then let's do from the home till day is just a shortcut for your home directory and then dot ssh slash id underscore rsa lenode and then dot pub okay make sure you get the public key so it's going to start with this ssh rsa we want to grab from there all the way to the very end and we can just copy that right from the terminal and paste that in and i'll just call this mac pro because that's the machine i'm on so go ahead and add that now you can see that it's in there and that's it we just click on create lenode and what it's going to do now is just go through and provision everything it usually takes 30 to 60 seconds so i'm just going to pause the video and wait until this says running before we move ahead okay so now you can see the status says running and what we have here is our ip address we have our ip version 4 and 6 and then to log in via ssh we can do this okay that will log in as the root user and then down here it's not available yet but once your server is running for a little bit you'll see some analytics here you'll see some network stats your storage backups so if you want to take backups you can get an exact image of a certain point and then you can revert back to that exact point if you need to and then there's all types of stuff over here volumes node balancers firewalls but we're not going to really get into lenode specific stuff because you could you could do what we're doing here on any any server so let's go ahead and try to log in so i'm going to open up go back to my terminal here and you can see here the ssh root so that's what we want to do is log in as the root user so we'll do root at and then obviously your ip address will be different than mine so make sure you put yours so minus 79158.102 and you'll get this message the first time you try to log in just say yes or type in yes and it's going to ask for your root password that we just we just set a few minutes ago so i'm going to add that i think that was it yeah so now we're logged in and if you want to just use passwords like we just did that's absolutely fine but what i want to show you is how we can use our ssh key and not have to type in a password in fact i want to disable password authentication altogether to make it more secure that way you can only you can only access the server from your client so i'll go ahead and just clear this stuff up and one of the first things i like to do when i set up a new server is just upgrade my packages so if you're logged in as a another user that's not root usually you would do sudo since we're logged in as root we don't have to and we're going to use the we're using the aptitude package manager which is uh the debian uh you know debian ubuntu package manager so we just do apt and we can install packages like this like if i wanted to install curl or something so what we're going to do is just run at apt update and that's going to update our package lists to the most recent and tell us basically we know how many packages can be upgraded and you'll see right here 48 packages can be upgraded so now i'm going to go ahead and run apt upgrade and i'll say yes and then this can take like one or two minutes to do this so i'm just gonna pause the video and i'll be back when it's done all right so now that that's all set i want to create a new user and you can see we're logged in as root it says root at localhost but you can also use the command who am i and that will always show you who you're logged in as so to create a user we can just use the add user command and then whatever we want to call it i'm going to call the user brad and that will create a user we want to add the password that we want to use okay and it's going to ask for some some information but you can just enter through all these yes all right so now we have that user and if i do id and then the user name it'll show me the user id along with any groups that that user belongs to now i want this user to to be able to run commands as with root privileges so we want to put it in the sudo group so we can do that by saying user mod we want to modify this user and then dash lowercase a uppercase g and then we want to put the pseudo group and then the name of the user which for me is brad all right so now if we do id brad again it should say sudo all right now as far as the ssh keys go i want to be able to log in as this brad user with ssh keys and the one we added was for the root so we actually have to add our public key for for this user for every new user you create now every user has a a directory in the home directory so we want to cd into slash home slash brad or whatever whatever you called the user and then from here we want to have a dot ssh folder just like we have on our client so i'm going to make a directory and i'm going to call it dot ssh and then we're going to cd into dot ssh and then from here where we want to put our public key as a file called authorized keys so we can create a file in linux with the touch command so we're going to say touch authorized underscore keys okay so now if i do an ls we should see that file and now we want to edit that file and i'm going to do that with a text editor called nano so i'm going to say nano and then authorized keys oops authorized keys and now i'm in that file so and it's obviously empty because i just created it now to get the public key i'm just going to open up a new terminal just say new window here and from here i'm going to do the same thing where we use the cat command to get the key so i'll say cat and then from the home directory and remember this is my local machine this is not the server so my home directory i'm going to go into dot ssh and i want the id underscore rsa underscore lenode and i want the public key so we want to put dot pub okay then we can go ahead and just grab this just make sure you copy it correctly from front to front to back or front to end so we'll copy that and then i should be able to just paste that in here yeah and then we'll just go ahead and command or control x and then hit y to save and then enter all right then we can close this one up so now we have the keys in in that file so before we move on i want to disable password login and this is optional you don't have to do this but it is a good security measure to take so we're going to edit a certain config file actually i don't need sudo because we're in root but let's say nano and then the file is going to be in slash etc ssh slash sshd underscore config which is like the ssh config file and i'm going to page down here and i'm looking for that right there password authentication which is set to yes right now so i want to set that to no all right so we can set that to no and then there's also this right here permit root login so sometimes people will set this to no so that it's it's not even possible to log in as root but i'm just going to keep it at yes and let's do a controller command x hit y and then enter and now we have to do is just restart the sshd service so we can do that with system ctl and we want to restart sshd okay so now we should be all set to log in as as brad with our ssh key so i'm going to go ahead and log out of the server with logo and now you can see i'm back on my local machine brad traversing at mac pro and now i'm going to try to ssh in but i'm not going to use the root account i'm going to use my new user my brad user so you're probably going to see this permission denied and when you get this i put this in the the gist here so basically we have to run two commands one is to start up the ssh agent and one is to add the the node key to that ssh agent all right so let's go back here and let's say we do this with eval and then some backticks ssh dash agent and then dash s and we should get back a process id okay that's not really important to us but that's what we get back and then we can do ssh dash add and we want to add let's see from the home directory remember i'm on my local machine here so home directory dot ssh and we want id underscore rsa underscore lenode and that's our private key so it says identity added now let's try again to log in with the brad user and there we go so now we're logged in we didn't have to enter any password and you can see it says brad at localhost localhost is is the name that's configured with my server i can change that if i wanted to all right so let's clear that up and now we're in the server logged in as brad or whatever whatever you used so before we get our files onto the server to deploy we do need node.js if i do a node version we're going to see command not found because node is not is not pre-installed there are images that you can use with lenode to get stuff pre-installed but i usually prefer to just start from scratch so to install node is obviously a little different than we would do on windows or mac because we don't have a gui right so we have to do it all from the terminal but it's it's not difficult at all it's basically two commands so the first one is a curl command and you can see this this deb node source this is for version 18.x so the latest version and just to show you where i got this from if we just search for node.js install debian and click on this link you'll see all the different linux or all the different operating systems here and if you see you can see this debian and ubuntu and then if you click on node.js binary distributions takes you to this this readme file here and then installation instructions and it just shows you the commands for each version of node so obviously we want the latest version that's where i got this all right so i'm just going to grab that curl command and let's just copy that and we can close this up and then i'm just going to paste that in here put our password this is your new user password not the root all right so now since we did that we should be able to now just do um use apt so no not node sudo apt install and let's just add dash y here and then nodejs so this will install both node.js and npm if you see this screen just hit enter twice okay so now if i do a node dash version it'll show that if i do npm dash dash version you should see that as well all right cool so now that we've done that we want to deploy this this goal setter app this mirn tutorial repository so what we're going to do is clone it with git and git should already be it's already on the system so if we do get version you should see that it is installed and then right here under code i'm going to go to clone and you could set up your ssh key on the server for github but i'm just going to use https so i'm going to copy that that url and then we're just going to figure out where we want to put this app on on the server so right now we're in the our home directory which is empty uh so what i'll usually do is create a new directory called like sites so we'll do that and we'll cd into sites and then from here we'll run git clone and then i'll paste in the link i just copied or the url i just copied and let's run that and now if i do an ls in my sites folder i now have the mern tutorial so i can cd into mern tutorial and if i do an ls we should see the files and folders now you have to go to the project and kind of follow the steps to to get going now this is a mirnstack app so we do need a uh mongodb database and you can see it says rename the env example to dot env and add your uri so the easiest way to create a mongodb database is using atlas which allows you to create a mongodb database in the cloud so i'm going to go ahead and log in with an account that i just cleared up for this i think it's this this account here and i just cleared everything out so if you don't have an account just create one mongodb.com and now it probably asks you to create an organization which i already have and a project which i already have called traversing media and then we can create a database so i'm going to say build database and this is absolutely free if we use the shared account so let's choose that we're going to just leave aws as the provider cluster 0 as the name and then create cluster okay once we do that we can just add a user i'm going to say brad1234 password i'll do the same thing and click create user already exists oh oh wait a minute oh i forgot i already had that user okay we'll just just create a new user looks like i already have one and then here this is really important because we need to specify where we want to access the database from so we're using a server that's in a cloud environment so i'm going to choose that and then right here under ip access list we'll click configure and then we just want to grab the ip address of the server okay we want to let we want to let atlas know that this it's okay to use this database from this ip address and you could add a comment in here if you want add entry and we should be all set if you're working from your local environment as well you might want to add your current ip address but i'm not i don't need that so i'm just going to click finish and close and we'll click go to databases and then this animated border this is just telling us that it's setting setting everything up this can take like one or two minutes or one to three minutes as it says all right so i will be back when this is done okay so now we're going to do is click on browse collections and you would see any data in your database here obviously we don't have any but i'm going to click add my own data because i want to create a database with a specific name and i'm going to call it goal setter all right and then the collection name if you want you can add a collection we'll create the goals collection and this isn't mandatory but i just want to specify a database name otherwise it'll just create uh it'll create one called test and it'll use that so let's click create and then we're going to go back to overview and then click connect and then from here we'll say connect to our application and we want to copy this string right here now there's a couple things that we need to change here so what i'll do is just open up let me see i'll just open up a new sublime text file doesn't really matter and i'm going to paste this in and there's just a couple things that we want to change here one is going to be the password so this brackets password you want to change that to the actual password and then after the slash we want to put the the name or like i said it's going to use tests so the name is goal setter and that should be good so now that is what we want to use so we want to whoops oh we want to copy that and we can close this up and then if we look at the github uh readme here it says that we need to add it to the the dot env okay we have to rename env example now we don't see in the example here because it's hidden it starts with a dot so it's a hidden file but if we do ls dash a we can see hidden files and you can see env or dot env example so we can rename that with the move command mv so we'll say mv and then dot env example and we want to rename that to just dot env so now if we do an ls-a again we have that dot env file and that's what we want to edit so let's say now we have to do sudo so sudo nano dot env and now a couple things we want to change first is going to be the the environment here i want to change that to production so development we're going to change that to production and then we want to put our uri here so let's get rid of that and then we should be able to just paste right in okay there we go i know it looks a little weird but it is there if i come over you'll see that the whole thing is there so now we want to command or control x hit y to save and enter and now that should be all set so we also need to install our back end dependencies and our front-end dependencies so that's pretty easy let's go ahead and right now we're in the root so that's where we want to run npm install or npmi and that installs all the back end dependencies like express and mongoose and anything else that we used in the api or in the back end and then after that we can cd into the front end and install the front end dependencies so let's do that cd into front end and run npm install now the way that this works let's just take a quick look at the code in this the back end server js that's the entry point on our server so if we're in production right here it's going to check and see if we're in production then it's going to look for the the react build folder which is going to be in front end build because you know when you build a react app you run npm run build and it creates all your static assets in that build folder so what we're saying here is if if we're in production and we hit any route we're going to uh we're going to display the index html that's in that build folder so we're not running like the react dev server or anything in production that's that's for development um if we're in production we're just we're using the built assets so we do have to run npm run build from the front end so the dependencies are installed now we'll just go npm run build make sure you're in the front end on your server and that will create this this front-end build folder and then the server will look for that okay so creating optimize and there's so many different ways to do this you can set it up so that this runs automatically i mean what i'm showing you is just kind of the bare bones um just so you can learn that and then you can look for you know more complicated stuff later or more complicated setups i should say once you get everything set up it's easy i mean once we we do all this all we really have to do is is push to our repo and push to production so let's clear this up now if we cd dot dot back into the root of our app and we run let's say npm start we can see that it's running on 5000 and if we were to go to i'm just going to copy the ip address obviously yours is different and then if we go to that if we if we don't put in the port it doesn't work right because that's port 80. but if we do colon 5000 there it is is our app now there's a couple problems here one we don't want users to go to port 5000 and and just pretend this is a domain you can add a domain easily but we don't want users to go to 5000 we also don't want to have this running like on our client if i stop this you know if i stop this here and i reload it doesn't work so that's where pm2 comes in which is a pro process manager for our application all right so to set that up we want to install it globally so let's say sudo and then npm install dash g for global and then pm2 okay so basically this is going to handle starting stopping restarting showing us the logs all that stuff so to start up our application we simply do pm2 start and then our entry point which is backend slash server dot js all right so now you can see it has an id of zero the name of this is server right so if we want to like uh stop it we would do pm2 actually i'll just show you we can do pm2 and stop and server and now you can see the status is stopped so i'll start it back up pm2 start server now it's online and if i go to local not localhost this ip5000 then it works but we still have the issue of this being port 5000 so that's where engine x comes in um but before we install and set up nginx i'm going to enable the firewall because we don't even want people to be able to go to port 5000 or any port aside from 80 443 and 22 because that's http https and ssh so we already have a firewall with linux ubuntu and it's called ufw so we can say sudo ufw enable and we'll say yes to that so now the firewall is active if i come back over here and reload even though my app is running it's not going to work because we're not we can't access any ports right now okay so everything is closed so to open the ones we want we can just we can put in either the port or we can put in the name of the service that runs on that port so for instance we can do ufw allow and then ssh so that will add that rule or open that port we also want http and we also want https okay so now what we need to do is install nginx because that's what we want to use for our web server we want to have a proxy that allows us to access our application that is initially running on 5000 to access it through port 80 which is the http port which is the the default port when you don't add you know colon whatever so let's install nginx we'll say sudo apt install nginx yes okay so that is we just want to hit enter here twice so now it's installed but we have to configure it so there's a file we need to edit so we'll say sudo nano and this is going to be etc nginx slash and then sites dash available and then slash default okay we're just going to page down here until we see this server block so server and then some open parent curly braces and down here you'll see this location block and also server name now later on when you add a domain whatever domain you choose you want to put here so i have a domain for instance called app002 so i'll put that here we want to do that we also want to do the www version so app 002.dev okay now as far as the location this block right here let's just get rid of all this stuff and then in that gist i have i have what you need to put in here so let's go over to the gist and we already did all this this right here so we want this proxy pass and that's where we put the local hose 5000 that's where our port is our app is running and we want to add this the rest of this stuff for our proxy so what we can do is just copy this everything that's inside of that that location and then let's come back over here and we should be able to just paste that in between the curly braces and we'll just bring whoops we'll just bring that back alright so that should do it as far as this file goes so let's go ahead and commander control x hit y and enter and now we just want to restart nginx and if you want to check the configuration you can do that with nginx t and it says that the syntax is okay the test is successful and now we just want to restart nginx we can do that with sudo service nginx and then we just want to restart all right so we should be good if we go back over here and we go to just just the domain not port 5000 there it is okay so our application is now deployed and you can add a domain name through lenode or through whatever you're using but let's go ahead and just register a user here we'll say john doe john at gmail oops and let's submit there we go welcome john doe just ignore that thing goal we'll just say goal 1 there we go we can add a goal we can remove it we can log out so now it's deployed now as far as domain names if you are using lenode you can go to domains and actually already have two here but actually i won't delete it but if you click create create domain you just put in your domain and then you can choose insert default records from one of my nodes and then you can choose the node you want to use that domain with all right then it will show up here and if you click on it it'll show you all kinds of stuff like the name servers the uh a records all that stuff um actually this isn't for that you know what i'm just going to delete this i'm going to delete that and say create domain i'm going to use that you just need to enter an email address too and then i'm going to choose the youtube linux now these name servers ns1 through ns5.lenode you would put in your domain registrar so for instance i use namecheap [Music] so you would just log in and find your domain which is this one right here click manage and then right here it says name servers i could say custom dns and i could add in ns1 so we actually need five so we would do that and then let's see that would be two oops three four and five and click the check and then that's going to take a little bit to propagate as you can see here up to 48 hours so you might have to wait a little bit but it should uh it should connect and then as far as ssls you could like from name cheap you can see i have a ton of them here um you could purchase one and you could install it or an even easier way and a free way is to use let's encrypt and i actually included the commands down here for that as well okay so you should be able to run these and and create an ssl absolutely free but yeah i mean that's it there's a lot more to you know deployment and devops but i'm not gonna obviously get into it in this video i think that this is a good it gives you a good understanding of the basics of you know how to set up a server and so on um you can get into like docker and using containers and in kubernetes you can see there's actually a you can use kubernetes with lenovo as well um you can set up databases as well we use something different like we used a third-party database with with atlas but you can set it up through your terminal or you can even do it through the interface here alright guys so hopefully you enjoyed this and learned something from it and i'll see you next time
Info
Channel: Traversy Media
Views: 59,993
Rating: undefined out of 5
Keywords: MERN deployment, MERN hosting, node.js deployment, linux server, linode, linux setup, ssh, ssh keys, pm2 node.js, nginx, node.js nginx
Id: 7aRjGIhwyQM
Channel Id: undefined
Length: 38min 41sec (2321 seconds)
Published: Mon Jun 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.