How To Deploy NodeJS API (Linode)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is up everyone and welcome back to another video in today's video we are going to be looking at how we can deploy a node api to a server so there are many different ways of doing this you can use services for example heroku that kind of does the things behind the scenes for you you just follow a few steps have some config file and then heroku handles everything from there but today we're going to be looking at how we can deploy our own node api to a virtual private server i'll be using lenode for this but you can use any other service for example there's digitalocean there's aws there's a zero google plug cloud platform many different that provide you with a virtual private server so when i started off doing a development and i wanted to deploy my own things it was kind of a daunting topic so i hope today i will show you how to do this very easily and hopefully it makes sense so i'm going to bring out my little drawing tool and i'm just going to make some drawings to illustrate something so basically when we deploy something we take something from our computer so this blue box is our computer to some server this is the server computer so basically when we talk about a server it's just another computer that's connected to the cloud and um or to the internet and we can then upload our program and run it from here so everybody can access it so the idea would be if we have a windows machine over here so i'm running windows so we have a windows machine and we want to deploy to an ubuntu linux machine so we can say ubuntu how would this work so first off our api or whatever programming language your api uses we are using nodejs so we need to install node.js on this server as well so i'll show you how to do this but i'm just trying to explain some higher view concepts here so basically we need to transfer our entire environment from windows to ubuntu and this is actually very simple so i'm going to show you today how to do this so first off let's actually go and this is going to be really quick so i'll put the link in the description below for this api as well but this is going to be a quick rundown of building a simple express node api so i'm going to first initiate my npm package just default everything could have added npm-y but anyways so we can say um we want to install express and we want to install install.env so this is going to be a really simple npm i express and dot env this is gonna be a really simple api it's just gonna have two endpoints logging out some json and nothing more okay so i'm gonna create a index.js file so this would be the the starting point of our server and here i'm just gonna import express i'm gonna say express equals require express and i'm gonna set it equal to the app variable and then i'm gonna on this app variable i'm gonna listen on some port and i'm gonna console.log server is running on this port server is running on this port and then i'm going to add that port in there so we don't have this port variable yet so at the top let's create it the sport variable we are either going to get this variable from the process.env or we're going to default to 5001. okay so here we have a basic express api up the next package i'm just going to import here is that env package we installed so dot env equals require dot env and then on this dot env package we can just run that config function directly config just like this so now we actually don't have a config file yet so let's go ahead and create that i'm going to say touch dot env i'm going to create a dot git ignore as well so git ignore okay so we have these two files and in this env file i'm just going to add the port of 5000. so i'm making it different from the one i'm using as default so that i can show you how exactly this works okay so i'm going to create two end points i'm going to create endpoint 1 and this is just going to do some basic things and i'm going to create an endpoint too both of them are get requests so in here i'm going to say request response and next and then in the first endpoint all i'm going to do is respond with a status of 200 an adjacent that has success of true data and instead of data we're going to have a message you can respond with any json type structure here i'm just used to doing this structure because yeah it's good practice to always have a status and then data with inside of the data you have whatever data you want to display so here i'm just going to say hello from endpoint one i'm going to copy this and i'm basically going to send the exact same for endpoint 2 but i'm going to say hello from endpoint 2. so that we just have two end points on this api to play around with so now we have a basic api set up in express so we have that set up here usually we'll add a node env property as well and set this to development so we can do different checks based on this env property and then in the get ignore we want to ignore two files the env because we never want to commit this to github the env file and the node modules because node modules become very big so we don't want to commit the secret file with all of our env variables or the node modules so let's actually initiate initiate a git repository inside of this project so we can say git init and we've initiated the git repository we can see these two this folder with the node modules and this file is grayed out meaning it won't be read by git so here we can say git add git commit initial commit okay so there we have a basic api up and running so we can actually test this by saying node index.js and we should see server is running on port 5000. so it's actually reading from this env file so now we know that's up and running okay so now the main thing we want to do is get this exact environment on the virtual private server we want to use so for this video i'm going to be using lenode the node is a very good service for creating virtual private servers i actually have my websites back and hosted with them and they're very cheap they have this month-to-month payment um so pay as you go you don't really subscribe to something and they're very cheap um so yeah for this video you're gonna have to um open up a lenod account you get a hundred dollars free credit if it's your first time using um lenode and which is actually a lot you can do a lot of um a hundred dollars but anyways let's go and create a node so the node calls their virtual private service a lenode when you use digitalocean it's calling their virtual private service a droplet when you use heroku they call their private service a diner so all of these services have like their own unique name to to their virtual private servers but basically it's just a server we're hiring from lenode and we're basically going to set up our environment and our api on that so we're going to say create a node and we're greeted with this menu so first we get our distribution so what operating system do we want to use for this server there are many to choose from but the most common one to go for is ubuntu it has good documentation you can search a lot of things for it and if you actually want to download ubuntu and use linux on your own computer then ubuntu is a very nice one to go for so we can use the ubuntu 20.04 the latest stable version of this time of recording so this might change in the future but i'm going for this one okay then you select the region so lenovo's had lenode has multiple different locations where they host their servers so you can view all of them from here in europe in asia in north america so the closest one to me and you should probably choose the closest one to your location i usually go for frankfurt because it's in the line um on the on the world chart of where i am so i'm gonna choose frankfurt germany and then we have a lenote plan so we are not gonna take a dedicated cpu um virtual private server we're gonna do the shared one because we want a cheap one just to test things on and i actually use this cheapest one for basically um all of my freelancing and um projects i want to host so it's five dollars a month which is very cheap you get um one gig of ram you get one cpu core and 25 gigabytes of storage so i'm going to select that one the next thing we need to give all the node a label to identify it by so i'm just going to call this youtube node deploy tutorial just like that and we don't need to add any tags you can set tags up later when you start learning how to use the node but that's not very important and then we need a root password so first i'm going to add a root password as follow remember that root password um but the the actual way we want to log into the server that we are hiring right now is by using an ssh key so if you're unfamiliar with ssh keys i would suggest go check out a blog article on how to create an ssh key it's very easy and i'm actually going to show you how to do this inside of our server once we connect to github so um i actually have a sshq already set up um with le node but if you the first time using the node and you want to add your ssh key you can click on add ssh key you give it a label i gave my mine a label of windows pc and then you add your public ssh key in here and then you just say add and then you select that so i'm going to be logging into the server using my ssh key because this is the quickest and most secure way to do that okay that's basically it now we have everything selected from this menu and we can just say creately node this is then going to move us to the following screen where we can see it's provisioning and building a virtual private server for us that we can use so once this is done i'll be back okay so now we can see that this little marker here is green and it's saying this lenode is running so we have just made our first virtual private server using lenode so the way we can access this server is by using this ssh access so we're going to copy this command right over here so basically what this command is saying we want to ssh into the root user of this server okay so we're going to copy this i'm going to go back to visual studio code and i'm actually going to open up another terminal so that basically why i'm opening it up in another terminal like this is just to show you that it's basically just another computer we're using it's it's like using your own development environment locally here but we're we're accessing a computer that has access to the internet so i'm going to paste that in here so we say ssh root and then the ip address of the server okay and it's going to ask us do you want to add your fingerprint whenever you log in the first time it's going to ask you this it basically just white lists your ip address and i'm going to say yes you need to type out yes and then we will be logged in we can see we are logged in and the reason it didn't prompt me for a password is because i have that ssh key set up on my local computer and then i gave lenode my public ssh key so they were able to compare the two and they know that i am the user that created this lenode okay so now i'm just going to zoom in a little bit like this i'm going to close the side menu so now we have our two computers so the computer here is the lenode and this is our local computer so first off we whenever you start using a new server we want to update all of the programs on the server and when we use linux specifically ubuntu i think ubuntu and a few other distributions of the of linux uses a package manager to manage all of the programs and packages on the computer so the first thing we want to do is actually update everything and upgrade any packages or software that needs to be upgraded so ubuntu uses the app apt-get or apt-get package manager so we can say apt so on the node server we type in apt-get and we say update and this will run a few commands it will prompt you possibly i don't know let's see so this is just downloading a lot of things upgrading the server that we just had which is built and it's very important when you start a server to always upgrade and update all of the packages for security reasons okay so here it's doing a few things and we're back to our prompt so this finished successfully the next thing we want to do instead of using update we want to say upgrade so if there's any new versions of things that needs to be installed so it's going to prompt us we're just going to say yes and it basically just updates and upgrades everything so that's the first step when you land on your new server i'm gonna let this run once it's done okay so that one took a little bit longer but now that we're done we can clear the terminal again and now our server is up to date upgraded but it doesn't have node so let's get node installed so we can actually check it doesn't have node by run by running the node v command so it's telling us node was not found so the same for this side if you want to run node it would actually give so i just want to show you that it's still a computer we still do the same commands that's why it's actually good to learn the command line and how to navigate in the command line because when you set up servers usually you would work in the command line okay so let's actually get node installed so my preferred way of getting node installed is using the appgit packets manager to first install npm because when we have npm we can install a global package called node version manager and this allows us to install any node version very easily so let's actually do that i'm going to say apt get install npm it's going to prompt us again are you do you sure you want to continue yes we are it basically just tells us you know this is gonna take this amount of memory or space on your computer and we're just like okay all good go for it so once this is done i'll get back to you okay so now that is done we can actually run the node command because usually when you install npm node comes with it so if we run node v we can see node version 10 is installed and if we run npm-v we can see the npm version that is installed okay so these two software are now on our server but i actually want to upgrade this node version so a very easy way to do it now is by running npm i g to install globally and then we want to install a package with the name of n just in but this is the node version manager so we installed this now it will install it globally very quickly and now we can use this n command so if we say end dash help we can see a few of the commands we can run with this m package i'm just going to make this a little bit bigger i'm going to clear and run that again so we see it more clearly listed so the commands we can run here or the following we can say in install and then the version install a node version so this is very easy we can have multiple node versions installed on this computer and then switch between them i actually use the node version manager on my local computer to switch between different node versions as well so this is my preferred way of getting node installed so i'm gonna use that in command with the install and i'm just going to say lts for latest stable if you go to the node.org website you'll see this version as well and here we can see it installed node version 16.14.0 so if we were to run node das version we can see we're still using version 10. so in order to switch to the new version we can say n use the 16.14.0 and then we can see it's switched into the ripple for that node version we can exit this ripple if we run node v again we can see it's still version 10. the reason for this is we need to restart the server or not restart the server we need to log out to the out of this terminal and then log back in so i'm going to log out by saying just exit and then i'm going to log back into the server with my ssh root and then the ip address okay we're back in now i can say node dash v and we can see that we are now running node version 16. so this is pretty cool it was very easy and the next part of getting our api running on the server is actually to get all of our code so i'm just going to ls-a here so this is what's the contents of this file directory so the current one we're working in here we can see we have the dot env file the dot git ignore the node modules the package lock.json and the package json over here we have the the git repository being tracked and then we have our index.js which is our server so how do we get this file onto our server well there's two ways first on windows we can use the the scp command which i'm not going to do or we can use git and github so we're basically going to push this project we have here to github and then use our server and connect with github and then pull down the project onto this machine so basically like you would do if you were to work between two different machines it's basically the same idea here so let's actually get this project connected to github so i'm actually going to go to github over here and then i'm going to create a new repository here i have a new repository on github i'm just going to call this youtube deploy node tutorial okay node api deployed to lenode vps okay it's going to be a public repository or private doesn't really matter i'm going to say create and we're greeted with this screen if you're unfamiliar with git and github i have a tutorial on git and github it would be linked in the cards up above and you can go and watch that if you don't understand anything i'm doing with git and github okay so we've actually run this kit in that command already so we already have on our local machine we have a git repository so if we say git status we can see we already have a git repository visual studio code also marks it out over here nicely okay so back to the browser we now need to commit this by adding a remote first so let's add the remote so we can say git add remote and we're going to call this remote origin and then we add it to this ssh uri so i'm going to quickly do that and now i'm going to say git push dash u origin master because it's the master branch and we want to push it to this origin ssh okay it's going to then push our project we can see it's done and we can go back to the browser and refresh now we can see we have our project up and running over here okay so the next part we need to do is actually to get this server to connect to github so currently if you were to go and actually copy this link so the ssh link copy you can use the https then you don't need to set up ssh keys but a lot of the functionality is deprecated i think in 2020 so use the ssh key instead i'm going to say git clone and then i'm gonna paste in that ssh uri now it's going to tell me do you want to add your fingerprint this is exactly the same as we saw when we logged into the server so this kind of gives you an idea of how github works it's basically the same idea okay so we're going to say yes and we can see that permissions was denied and the reason for this is because currently our github account does not recognize the server and we need to set up an ssh key on this server and then connect that ssh key to github so in order to do that we first need to create a ssh key so let's actually go and create an ssh key and it's pretty simple because most of these command line tools come built in with open ssh which just has a few easy commands you can run to create your own ssh key keys okay so we're going to say ssh dash key gen for generating a key with a t flag and then we're gonna have the rsa algorithm and then when you run this it's gonna ask you where do you want to store this ssh key on our server so this is the node server i'm just going to keep everything default so i'm going to just press enter i'm not going to give a key a pass phrase and then we can see our ssh key was created at this location so we have the two we have the the private ssh key and the public ssh key both in the same directory so i'm actually going to navigate into that directory so that you can see it okay uh it's not a directory so we just need to cd into the root okay which it and we would just say ls a to get all of the the hidden folders as well and here we can see that ssh1 so i'm actually going to cd into that ssh and if we then list out everything we can see these two key pairs that was just created the the public this is the private one because it doesn't have the pub extension so we're going to take this pub ssh key and add it to our github account so let's first actually print out this id rsa.pub and i'm going to take this key i'm going to copy it and then i'm going to go go to github and i'm going to go under settings ssh and gp keys gpg keys and i'm going to add a new ssh key i'm going to say lenode and i'm going to call this node api youtube just like that and i'm going to paste in that ssh key so don't use the one i have here create your own this is not gonna work anyway for you but then i'm gonna say add and it's all added it would actually prompt you for your password if it's the first time you do that so then you just have to add your your ssh key your your password for github and then you have your github and our server connected so now if i were to go back here again just go back to the root and if i were to run that github clone command again it should work now so i'm just going to up arrow key till i get this command and we can see it actually cloned down the project so if i were to ls on my root folder i can see the youtube deploy node tutorial youtube deploy node tutorial so we can see it's the exact same as our local computer so now i'm going to cd into that folder and i'm going to list everything out we can see if i list it out on this side as well we can see we don't have the env file on this side so that's something we're going to check in how to do that we still have the git repository the git ignore the index.js package.json we don't have the node modules so obviously we need to install all of the node modules so we just run npmi this will then go and install all of the project dependencies that's just a message to tell us that npm has a new version so if i were to run ls dash a again we can now see we have the node module so basically everything the same on this side is the same on this side now except for the env file so in order to get the env file from our computer there are two ways we can actually go and create a new env file using the nano editor or the vm editor and then add everything in there but if your env file becomes very large and there's like a lot of keys and stuff in there then this approach i'm going to show you now is a little bit easier so on windows i'm not sure what this command is on mac but it's very easy to just google this but the scp command allows us to copy something from our local computer to some server okay so i'm gonna actually do this now i'm gonna go into the desktop slash youtube deploy tutorial so this file directory in here and i want to select the env file so inside of this directory we saw that there's a env file and then we want to add a space and we want to go to the the machine that our server is hosted on so once again like we said previously the root at and then we need our server ip so i'm just going to grab the ip over here that's the ip address i'm going to paste that down there and then we want to go to the the root directory of this server and we want to go into the youtube tutorial youtube deploy node tutorial so youtube deploy node tutorial just like that and we want to paste in that env file so if i run this command we'll see this is going to show us it copied this env file 100 for this kilobyte per second onto the server so let's actually check if it's there so if i say ls dash a we can see that the env file is in this server right now so that was pretty quick and pretty easy so if i were to actually check the env file using the nano editor we can see that node environment is currently development and the port is 5000 so let's actually change this development to production and save by pressing ctrl s and then ctrl x you don't need to do this to host in the api i'm just showing you how you can navigate on the server and then update your env file okay so now we have everything up and running on the server so we can actually say node and then the index.js file now we can see server is running on this port on the 5000 port so this is exactly the same as we would have done here so node index.js we can see it gives us the same output but now instead on our local computer if we wanted to hit that api we would say localhost and then the port number now we're not going to say localhost anymore we're going to be using the server's ip address so if we navigate to a browser i can now use this let's just move that into the same tab we can use this server ip paste that in there and then just say port 5000 and then we had one endpoint we had an endpoint with the name of endpoint 1. so if we go back to our project open up the index we can see we had an endpoint of endpoint 1. so we can now go to that port and then to endpoint one and we should see this json being logged out so we can see this api is now hosted on this machine and we can access it over the internet so this is great so the next step i want to go to is that usually when when things are hosted online we don't leave the port like this we want to access the the server by just saying the ip address and then the the api endpoint in order to do this to to navigate away from the the port that's there we need to use something called a reverse proxy and this is i know it's a big word but it's actually very simple if you see what it does it's basically just going to route any traffic that comes into this port and it's going to forward it to port 80. if you see i typed in 80 and then it just it disappeared so usually the port 80 is http and the port um i think it's 443 is https but we'll get to https in just a sec so let's get back to our server over here okay so we're done with this side so on this side i'm going to stop this project like you seen right now i have to go node index.js but we don't want to run the project like that so we're actually going to use a package called pm2 which is a process manager it's a very cool thing and it basically will run our node api by its own we don't need to run node index or nodemon or anything like that the pm2 will handle that so let's actually quickly install pm2 it's a npm package we can say vm or npm npm install and we're going to install it globally the pm2 package so this is pretty straightforward you can go to the documentation or the website of pm2 it's a very nice website it has all of the commands listed out there but i'm just going to show you all of the basic things you need right now so how this would work once this is done we can actually use that pm2 command and we can see pm2 this is the pm2 cli tool we have a few things that we can test if you ever want to know what commands there is you can just use the h flag for help and it would log out all of these commands for you so i'm just gonna do that again so it's neatly formatted there's many commands with pm2 but at the top we're only going to use very basic ones so let's actually use them so i'm going to say pm2 and i want to start and what do i want to start so in this folder we're currently we're currently in this folder and i want pm2 to start the index.js file and i'm going to give it an in flag to give this pm2 project a name i'm going to say api and i'm going to press enter now we can see that this api is running so the name is api so this is formatted badly okay here we can see the name is api namespace is default we can see a few things this status is online it's consuming this amount of memory and cpu and the root user has access to it so now we can actually go back to our terminal and then go to port 5000 and refresh now we still see it's running and we did not use the node index.js command so now this pm2 will keep on managing process manager this will manage our node process the next command we want to use is pm2 start up and ubuntu and all this is going to do it's going to create a script so whenever we reboot our linux machine or our virtual private server pm2 will then go run the script and restart our api so whenever our server were to crash the api would restart itself with the pm2 project so now we have our project running on its own and we have it on this machine so let's get that reverse proxy set up so for this reverse proxy we're going to be using engine x engine x is a very popular web server technology it can be used as a web server for many different things it can be used as a proxy a reverse proxy but it's most common use that i've seen is probably for using it to route in a server and to use it as a reverse proxy so in order to install this on our server we're going to be once again using the apt-get package package manager so we're going to say apt-get and we want to install engine x so just like this and we can run that i'm actually going to close this local computer terminal and then we can view everything here and then they are prompting me to do you want to continue yes i do it will run and once this is done i'll get back to you okay so now engine x is installed now we just need to configure a few things so these are the type of things services like heroku kind of does for you it configures these reverse proxies and and setup type things for you so this might be a little bit daunting for people going into config files and setting up things but the more you do this and the more you play around the more you break things obviously in small projects the more you learn this is honestly how i learned i didn't start off learning engine x and these type of stuff by just knowing it i i played around and built a few things deployed three things rebooted a few servers and stuff okay so we now that nginx is installed i'm just gonna navigate to the root folder again and in here i'm gonna cd into the etc engine x okay so when we installed nginx this folder and files was created so if i were to list out everything in here we can see a lot of folders and files so the one we are interested in is this sites available so i'm going to cd into sites available and then ls again and in here we can see there's a file called default so we are now going to be using the built-in text editor of all linux distributions and this is the nano terminal uh nano editor so i'm going to say nand no and then we're going to go into that default file and here we can see the contents of this file so i'm gonna use my arrow keys to navigate down and here is something i mentioned previously is this listening on this port so this port 80. so 80 is the default for http and whenever you have ssl setup which we'll get to at the end of this project is it's going to run on port 443 okay so currently it's basic setup is for http so let's actually continue using this http okay so in here if you scroll down this is the position we want to be at we want to be in where you see server underscore name and then this underscore again and then we have a location block so inside of this so instead of that underscore we're going to add our domain name so if you don't have a domain name yet then it's fine you can add your server's ip address in here so the one we use to log in with the root at and then the ip you can add that in here but i already have a domain purchased and i have a domain purchased with namecheap so if you're unfamiliar with namecheap it's basically a domain name service that gives you access to buy different domains so i've bought this domain a while back life manager.site and i basically just use it to play around like to set up servers and do some testing and stuff so it's basically just a dummy domain to play around with it's very cheap the dot sight extensions are pretty cheap i think it's like one or two dollars a year so that's very very cheap okay so once you've purchased a domain name then you can add your domain name at this server name property if you don't have just remember add your server ip address but then if you don't have a domain name one important thing to note is you won't be able to set up ssl or tlc tls sorry not tlc so you need a domain name in order to set up the https so we're going to add our domain it's live manager dot site and then space and then www.lifemanager.site so we need to add in both the the extension the the domains without the ww and with the ww okay so once that is done always remember the the semicolon at the end engine x is written i think in c or c plus plus i'm not sure so it it does take in account the semicolons on the at the end okay so i'm just going to remove everything inside of the the location block here and then i'm going to add in a few things so remember i told you that engine x we're going to be using it as a reverse proxy so we're going to set up a few commands inside of this location block in order to tell engine x to revert or reverse some incoming request to a specific port so currently we're going to that that that port 80 which is the default so that's good but we want to revert all of the traffic from port 80 to a specific place so we can say proxy pass underscore pass and you want to specify the url our our api is running on so on this local machine so currently this vp vps we're using it also has something referred to as localhost because it's also you know if you run something on it it also has its own localhost ip so this would be the ip address or you can actually type in the ip address here as well but i'm just going to add localhost and then port 5000 which we've got our api running on so we've got our api running on port 5000 of this localhost computer okay so now it will proxy pass so reverse proxy anything coming into the port 80 to this specific url on the server the next thing we need to add here is proxy http version and we're just gonna forward that in as well so the next few commands are just basically commands whenever you make a request with http these parameters or these settings are needed so what version of http do you have any upgrades for tcp connections and so forth so i'm just going to add in a few more i'm not going to be explaining them in detail because i'm i'm not that familiar with explaining them but basically you can see it it's just it's passing everything that's coming in from a request to our api so if our api had some cookies or stuff it would basically pass everything along from that request to our api so we can say proxy set header and we want to upgrade up grade and then we pass in a variable which is set by nginx which is the http upgrade not up update upgrade okay the next thing we want to do is the proxy set header and then connection upgrade with a string next one is proxy underscore set underscore header and then the host and then once again we pass in an nginx variable which is the host the last one we need to add is proxy underscore cache bypass so basically this is just nginx caching whenever so it caches some of the information on the upgrade so upgrade http upgrade and that is it so if we view that now we can see we filled in the server block over here and then we added in these information pieces so i'll add this inside of the the description of the video as well with all of the commands i used but yeah basically we need this so we can then say control s to save this file and then control x to exit this file so now we run the following command to check if that nginx file is okay so it will tell us if we did something wrong so we can say engine x nginx with the dash t flag and this would tell us all of the syntax in this config file was okay and the test was successful so this is all good now we need to restart nginx so we can say system control so this is also something used with ubuntu is the systems control tool and we want to restart and then the process nginx and that is restarted now and now we can go back to our browser and test something out so if we were to run this on the the port 5000 we can see it still works so if i do this if i go to end point two and run we get hello from endpoint two this is all fine but now we've set up engine x so if we were to go to just slash and then run we'll still get a response so this is cool now we don't need that port anymore and the next thing we're actually going to do is set up firewalls to only allow access to the server via three different ways via the ssh port via the https port and via the http port so let's do that we can actually exit and go back to the root and here we're going to use the ufw which is the firewall of ubuntu so we can see ufw enable and it's going to ask us are you sure you want to proceed yes once again so now it is um our firewall is active and enable on system startup okay so now we want to enable a few things so we're going to say ufw enable ssh oops so we want to say ufw allow and we want to allow on ssh and it added that rule then we want to ufw allow on http it added that rule and then we want to allow an https and added that rule so now we've added that rules and if we actually go back to our browser refresh we can see it still gives us our json response but if we now try and go to the port 5000 it should just hang it won't give us any response because we now have locked our server to only give responses via the 8080 port or not the 8080 board just the 80 port and it gives us our response and this is very cool because now we can see actually how engine x is proxying our incoming request to our server so when we ran it on the port 5000 we got that request but if we run it on 80 now we get the same request because it's redirected by nginx so this is very cool i hope you guys are still following i'll just watch this video a few times do this project a few times and i'm very sure it will it will become second nature or you'll understand it better okay so now we've got everything set up but the one thing we don't got set up is this not secure so if i were to actually go to to view this and say https and we want to go to www on that domain or just add in the the ip it's actually not going to respond to us because we don't have ssl or dls setup on the server so that is the next thing we're going to do so in order to continue with this part you would need domain name so if you don't have a domain name then hopefully you find this tutorial helpful and you got to this point and everything is working but the next part we're going to be setting up is ssh so that we can access our server on the ssh port okay so let's actually go and do that in order to do this we're going to use another program or package called let's encrypt and let's encrypt basically gives us free ssl certificates for our domain okay but we're going to need to use a search board a cert bot a certificate bot in short cert bot which is a tool that helps us you know connect this let's encrypt certificate or generates a let's encrypt certificate for our domain so let's actually do that now by running the following command we're going to run snap install dash dash classic and then cert bot okay so snap is another package manager but you need to use the snap package manager in order to to download certbot and use cert bot so we're going to run that and here we can see it starts downloading so once this is done i'll get back to you okay so cert bot is now installed now we're going to add this following command so i'll add the link to the to the article i used to get all of these commands it's actually certbot website and it gives you all of these commands to run so ln dash s and then snap bin cert bot space the user bin search bot okay this won't give us any feedback but that command is now set this basically just allows us to execute certbot commands which we're going to do now so we're going to say certbot and we're going to run the engine x command so we want to connect engine x in our cert pod that's why we added our domain name with the server name so cert bot is now going to run through our nginx config it's going to set up the ssl part of this engine x process and it's going to ask us a few questions so let's run this command it's going to ask us to enter an email address that will send us an email whenever our ssl certificate needs to be renewed but certbot will basically run and renew the the certificate on its own so we're going to say the full stack oops you can't go back so i need to rerun that so if you type that incorrectly you just need to control c and run it again okay so add in your own email address there then it asks that we read the terms and services obviously we didn't so just say yes and would you like or be willing to add or provide let's encrypt with different information let's just say no so they actually ask you do you want to provide us with information so let's say no and here it picks up something interesting account registered which name would you like to activate the https for and we want to add it on both so if someone were to access our web or our api using life manager.site or www.lifemanager's site both of them works and if you read this it says if you want to select both just leave it blank and press enter which i'm going to do i'm going to run this and now it's going to request for a certificate this might actually fail because we didn't set up our domain on lenode and our server yet so we can see it failed here and let's actually go and do that now so what we need to do is go to the node so i'm gonna actually navigate over here with my node tab that i close it it's over here okay and we wanna create a new domain so under these domains let's go to domain and create a new domain so we're going to say create new domain and we want to specify the domain so here i already have that domain i'm just going to select that life manager.site we want to have an email address connected to that so the full stack junkie at gmail.com and then lenode gives us this option to insert default records for one of milo nodes so i'm gonna select that one it's gonna fill in all of the a records and stuff that it's very overwhelming to work with initially so let's just use the that default and then we're gonna choose the node we want to set this domain for so the youtube node deploy tutorial so that's the little node and we're going to say create domain then we want to go once this is done we see all of these name servers so there's five name servers then we want to go to namecheap so where do i have my namecheap browser over here so we want to go to the name registrar we are using and we'll see an ability to add name servers i'm going to choose the custom domain name server and then i'm going to add in all of these lenod name servers so if you view it on the um just going to actually go ahead and close up some of these tabs okay so here we i just went and add all of these the node name servers in here okay so i'm just gonna actually set.com and i'm gonna i'm gonna save it and then we can see this made might take up to 48 hours so this is actually really true sometimes it does take 48 hours i had situations where it took a long time to to propagate but uh yeah let's actually see um how long it takes so now we have connected all the node to this domain name okay so this is all set up we can reload just to make sure everything's fine we can go back to our lenode and now if we actually were to go and try instead of going to https let's just try http and then um life manager dot site and that endpoint so yeah it doesn't look like it has propagated yet so i'll actually get back to you to this part of the video once this has propagated and works okay so after about six or seven hours it finally propagated and now if we do the refresh we can see that we are now fetching our data from the api with this domain name so we only have one thing left to do then this entire api is set up so we need to add the ssh so we actually took a look at how to do this but then we got stuck because we haven't done that domain name set up yet so i'm going to actually rerun the cert bot engine x command now i'm just going to close this side menu up i'm going to rerun this and let's see what we get so first of all it's going to ask us which names would you like to activate the https for and we did see this process before and here we just need to keep everything blank and press enter and hopefully search bot will now generate a let's encrypt certificate for us so let's wait and see what it logs out for us so okay deploying certificate successfully the deployed certificate okay so this is all great so now we can see that the certificates were successfully deployed now let's actually go back over here and let's reload then we can see we now have this lock we now have a ssl encrypt communication with our api so the last thing we need to do is run this command to test the renewal of our ssl certificate with certbot so we're going to run this command certbot renew dashed as dry run and basically what this is going to do it's going to simulate simulate a renewal so after i believe 90 days 90 days before our ssl certificate expires let's encrypt or cert bot in this case will regenerate a new legendre certificate for us so now we have everything up and running we have our api working on ssl and on lenode on our own virtual private server so i hope you've guys found this video insightful and that it helped you if you did please leave me a comment letting me know give give this video a thumbs up and then in the next videos i would actually show you how you can deploy a react app so the front end of your application to lenode server as well so if that's something you would like to see leave me comment down below telling me that that's something you'd like to see and then i'll see you guys in the next one cheers
Info
Channel: The Full Stack Junkie
Views: 16,561
Rating: undefined out of 5
Keywords: Delpoy NodeJS App, Hosting NodeJS App, Node API deploy tutorial, Tutorial on deploying nodejs api, Express API deployed, Linode NodeJs Deployment, Linode Hosting, Tutorial on Linode Hosting, NodeJS API on VPS
Id: sD8X4CApdpo
Channel Id: undefined
Length: 55min 56sec (3356 seconds)
Published: Sun Feb 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.