How to Deploy a Node.js App to Digital Ocean (Nginx, Ubuntu)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay what's up guys I am going to show you how to upload a node.js app to a VPS it's going to use nginx um Ubuntu and yeah let's also show you uh how to use https uh with let's encrypt although I don't actually have a spare domain so I'm not actually going to set it up but I'll show you guys how to so the first thing we're going to do we're going to need to go to digitalocean or whatever VPS you want to use then you can sign in so once you're signed in you're gonna create a new project in this panel then you're going to click get started with droplet so you can choose whatever Ubuntu you want there's 22 came out recently when I started it was on like 20. um so go to regular go to four dollars or whatever you want so this is not going to be like a big security video I will make one in the future but you should be using SSH keys if you care about security which you should probably learn the basics of but for now I'm just going to do password what I would recommend is downloading keepass or having some kind of password manager because if you use like password one two three dollar sign it's really not good so I'll show you what you can do on here you'll make one so that's a digital ocean droplet and then you can even set this to like 32 characters span this a bunch of times really helpful but we're gonna need to make sure that this password actually is okay so we're gonna copy this in and it is valid so if you need any of this stuff you can set it up and you can just give your thing a name URL session upload back end this is a part of a larger video series that I'm doing so this is going to take a few minutes it's just going to initialize and everything so I'll be back in a second or actually while this was doing this so you're going to want to go into network if you have a if you have a domain you're going to want to go into networking and then domains and you're going to want to enter the domain and you're gonna have let's just do this so you're gonna have to put in the text record so you're going to do an at and then just uh your server and you're going to create the record and then I believe you're also going to do www . then select the server yeah so you need your a records and then also in your domain setup you need in your dot in your domain registrar you need to point the name servers to digitalocean so it's going to be like one NS digitalocean.com or something like that okay so since this is created we have the IP address so you're going to want to open a terminal and you're gonna SSH root at this IP bad owner oh okay I had some weird permissions um that you won't have um so SSH root at your IP you're gonna click you're gonna type yes and then you're gonna give it the password that you gave to digitalocean a few minutes ago so now we're in our server and what we're gonna need to do now is transfer our node.js project to the server so we're going to want to make sure a few things so we have node modules here and we have packagelock.json and we don't want to upload either of those because it's a lot of data we're gonna do npm install first just to make make sure everything is proper and all that and then we're going to do RM slash RF node modules and package slash lock Json make sure it's not the package.json okay so we're going to do open dot so open the current directory it's going to give us this so what we're going to do is just compress the file and then we're going to open a new terminal we're going to CD to our our working directory with the zip file so what we can do is we can do SCP URL session.zip root at our IP and then we're going to do a colon a tilde and a forward slash it's going to ask for our password now if you're using SSH keys this is a good helpful tip you would use SCP I tilde.ssh and then your key and then you would do the rest so you would do the file and then the username and all that so now if we go back to our terminal we should see our file here so we're going to install a few packages so first we're just going to update everything sudo apt update and actually as you can see here I have 15 packages that can be upgraded so I'm gonna do sudo apt upgrade so update and then upgrade I'm going to do yes so once it has updated I'm just going to do it one more time you're going to want to go Google node source and go to this binary distributions this GitHub and we're going to want to go to installations and I would probably recommend using the long-term service one so see right here node.js LTS 16 point x it's going to be more stable and what this is is this is I guess it's a company or an organization who makes it easier to distribute node.js and what this is going to do is it's going to put it in our apt Ubuntu packages resource kind of thing so we're going to do that we're going to copy that script and paste it and then after a while it's gonna work so then we're gonna do sudo apt get install y node.js and it's going to install node.js so you can do node Dash V to make sure it worked we got 16. we're going to do sudo apt install npm I don't even know if this is oh npmv so here we already have it so I don't even think you need to do that okay so we have node.js um now we're going to do sudo apt install nginx vim and zip yes so now we want to do the firewall stuff we're going to say sudo ufw allow single quote open SSH okay and then sudo ufw allow and Gen X http so make sure that you added open SSH otherwise it will it could lock you out so pseudo ufw enable yes sudo ufw status so we have open SSH and nginx and now if we go to the browser with our IP so copy your IP and Go to http make sure it's not https otherwise this will fail so it's actually failing for me because it keeps redirecting it so if you open a new terminal and you just type that so curl and then the website you'll see that it has this HTML with nginx so it is working mine's just being really stupid and annoying and redirecting so we have nginx working we have our firewall working so I don't have a domain but I'm going to pretend like I do and I'm going to tell you what to do um so we're gonna just clear out we're gonna do sudo make dur slash VAR www then you're going to put your website's domain here so I'm just going to say my website.com so lsbar www and we have my website.com directory now I'm on the root uh user you really should make your own user and not be using the root and then ask for pseudo privileges using sudo but you're gonna do you're gonna do sudo CH own slash R and uh dollar sign user colon dollar sign user slash VAR www.mywebsite so pseudo them Etc nginx sites available not enabled available and you're going to do your domain so mine is my slash website.com now I'm going to copy something in so this is for nginet configuration so you see here how it says server name my mouse is doing weird things on my keyboard so this here is going to be your domain and then you're going to do www.yourdomain I don't think that I can do www with this so I'm just going to delete this but you're going to do a space between your domain and then the www version location this is what you want so your local host and then you have your Port that your application is running on so mine is running on 5000. here's maybe you set three thousand maybe seven thousand I don't know but copy all of this exactly and make sure that you get it all correct then you're gonna do you're gonna if you're in Vim click Escape colon WQ and that's going to save it so we're gonna do CD slash Etc nginx sites available and we're just going to look at this we're gonna cat default read it out you'll see we have all this stuff already already in our um default now I'm just going to delete this you can make a decision what you want to do but I'm just gonna RM slash RF Etc nginx sites available default I'm also going to do that for sites enabled so now we're going to do sudo Ln slash S uh or dash s slash Etc nginx sites available and then my website.com or your domain and then we're going to do slash space slash Etc nginx sites enabled and then my slash website.com okay so now we're just gonna LS two dots uh Slash sites enabled and you'll see we have our file so we're going to do nginx T which is going to check that our syntax is okay and then nginx slash S reload that's going to reload everything so now nginx is trying when we go to the website or it's not going to work for me when we curl the website it's gonna say bad gateway because it's looking for the website on localhost uh 5000 but our node.js app isn't running so it's not going to find it so it's going to give us an error so we're going to CD change directory to slash VAR www and then mywebsite.com we're gonna LS we're going to move that zip file that we transferred before so tilde slash and then URL mine is called URL session I'm gonna move it to the current directory with the DOT we're going to do unzip so remember we installed zip earlier it's going to be URL session we're going to LS so what I like to do is move the zip file back a directory and then we're gonna move the contents of URL session and then a start end and then a space in the dot right here so LS so what I like to do is move source star space dot so removing the contents of source and we're just going to delete most of this stuff so RM slash RF uh the underscore underscore Mac if you don't have a Mac you won't have that URL session and then source so we have helpers images a bunch of stuff for my node app now we're going to do npm install so we get our node modules back so now that that has installed we're going to do no node and then server or whatever starting point your app has then we're gonna reload our browser and as you can see I have my app but the problem is if we close this obviously we're going to lose our app so what we need to do is install we're going to say sudo npm install pm2 at latest Dash G this is going to install an application called pm2 now pm2 allows us to automatically start our app if it crashes when our server starts up and it allows us to run it without keeping like a window open or whatever so we're going to say pm2 startup system d okay and then we're going to say pm2 pm2 start server.js semicolon and pm2 logs so this is going to start the server and then right after it's calling another function to open up the logs so just to check for errors and as you can see we have started on Port 5000 so we can close this and it's still going to be running so before when you close the thing it wouldn't so what we're going to do is pm2 save so this is whenever pm2 starts up it's going to start up this application so we can say pm2 status and it'll show us what we have and we can run multiple of them but then we're going to want to do sudo system CTL start pm2 slash root and we don't really need the service but we can have it but then systemctl status pm2 root or whatever user you're on so if your user is server admin that's going to be pm2 Dash server admin so as you can see we have active running so now if we want to restart the Box we can say system CTL reboot and the website is going to go down okay so so now we're going to log back into our server and it will automatically start up the app even if the server crashes so it's super helpful so this is the point where I'm not going to be doing anything more because I don't have a domain name and without a domain name you can't do let's encrypt https certificates but so I'm going to show you how to do it so sudo ufw status so we have nginx HTTP so what you're going to want to do is you're going to do sudo ufw allow nginx full so right now we have nginx HTTP we want nginx full so the next thing you're going to do is sudo apt I'm just going to copy this thing because I suck up typing um sudo apt install cert bot space Python 3 cert Bots uh nginx so python3 Dash cert bot Dash nginx then at that point that's going to install and you're going to say sudo insert bot Dash Dash nginx Dash D and then your domain so my domain.com space slash D and then if you're using a www as well you're going to do www dot my domain.com and you're gonna run that then you're gonna run sudo system CTL status start timer and this is going to let you know if you if the timer to renew after three months is working and then you can try it out with pseudocerpot renew dry run but uh the next what you're going to want to do is sudo ufw delete nginx HTTP you'll say sudo ufw status and where I have HTTP you'll have full so by the way with nginx if you're doing uploads with the server you're going to need to update Vim slash Etc nginx slash nginx.com and inside this HTTP block what you're going to do is you're going to click I and you're going to copy or you're going to type in client Max body size 100 megabytes you can change that to whatever you want I'm just going to put a thousand so that's a gigabyte and you're going to do nginx T and nginx I think it's s reload okay so now I can upload as many pictures as I want under one gigabyte and this is obviously going to take a minute because quite a large image okay okay so after a bunch of typing in the wrong keys and all that as you can see success I was able to upload some images so make sure that nginx you need to set the client Max body size to whatever bits your needs so that is all for setting up a node.js app on a Linux VPS I hope you enjoyed um if this video was good consider liking and commenting if you're interested in iOS development videos then you can subscribe that is all thank you
Info
Channel: CodeBrah
Views: 41,600
Rating: undefined out of 5
Keywords: deploy nodejs to linux, deploy nodejs to vps, deploy nodejs to digital ocean, nodejs, node, node.js, ubuntu, ubuntu node, ubuntu nodejs, nginx, vps node, deploy node to vps, nginx ubuntu node, nginx vps tutorial, nodejs deployment tutorial, vps deployment tutorial, digital ocean, vps hosting, hosting, digitalocean, how to upload nodejs to vps, how to upload nodejs to digital ocean, nodejs digital ocean, nginx tutorial, nginx tutorial digitalocean, node nginx tutorial digitalocean
Id: bBA2yCnEf68
Channel Id: undefined
Length: 20min 3sec (1203 seconds)
Published: Mon Oct 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.