Deploying node.js applications #2 - provision server & setup flightplan
Key Takeaways
Deploying Node.js applications using Flightplan and provisioning a server with Digital Ocean
Full Transcript
So, we are deploying this Noode.js app that I've spun up into production. We're going to be doing an automated deployment on it. So, we can run one command and deploy to as many servers as we have. If our application is huge and has 10 servers, we can run one command and it'll send them off to all 10. So, uh I've used Digital Ocean to spin up a server called Test Node App. If you did not if you do not know about Digital Ocean and you didn't catch my first video, go ahead and catch that now. It's in the description. And if you don't know about SSH, you'll want to watch my SSH video. That is also in the description, and that will get you up to speed with what we're about to be doing. So, we've got an email here that's created our computer for us. There's the IP address, and there's our password. So, let's go ahead and SSH root at IP address. Let me grab that password. I already logged in, just so you know. So, it will ask you to change your password the first time you log in. Um, and I've already changed my password. So, let's say I've logged in. And I've changed my password. So now what we are going to do is we are going to install node. That's already installed because I spun up a digital ocean image that has node installed. So let's go ahead and install forever. Forever is going to be our node package that actually runs our app. mpm install g forever. There we go. Forever is installing. And then once that's done, the next thing we're going to do is add a user uh a deploy user. There you go. forever is installed. Let's create a deploy user. So, if you look at this gist I have for you, it's got all the commands that I'm going to be running. I've got this gist right here in the description. And I've got a link uh I've got a link to how to create a deploy user. So, let's go ahead and just copy and paste this. You can look at those commands later on and see what they actually are. It's just going to ask for that user's new password. There we go. My user is updated. There's now a deploy user who has pseudo powers. So we can kind of do some pseudo things. You don't really want to do things as root. It's a good idea to have a user that at least has you can apply restrictions to. So now we can SSH in as this user. So let me go exit. We're going to SSH deploy at that IP address. So we're going to log in as that user. And we are going to now set up passwordless SSH. Uh so that way that user can log in with an ID with an SSH key instead. So the steps for doing that as you can see here. We're going to make a directory for SSH and get out. MKDIR.SH. Okay, let's exit. There we go. I'm out. Um, and now all we have to do is copy our RSA key into the authorized keys. So, I'm assuming you already have an ID RSA key. And now we are just going to copy that. If you have it by a different name, you'll want to change that. I'm just going to copy that to Oops, I kept url.com. It's not at url.com because I have an IP address here. Deploy at my IP address. It will ask for my password. I've copied the key over and now I can SSH in and it will not ask for a password. There we go. I'm in. Awesome. So now almost done. All we need to do is automate our deployment. We already have a user set up who has uh permissions and so let's go ahead and auto made our deployment. The tool we're going to be using for this is called Flight Plan. It's a Noode.js library. So it really kind of will feel very familiar if you're familiar with Noode.js. JS Capestrono is also a great tool. Uh it's a little more fullfeatured which means it's a little more complicated to set up but you get more features out of it. Uh but flight plan is great for a lot of scenarios. So we're going to install flight plan. So we are on our machine right now. We're just going to go npm install g flightlight plan. That gives us the fly command everywhere on the machine. There we go. And then we are in our application right now. We are going to install fly locally in mpm install and we're going to do savedev fly. Just so you guys know too, I'm popping in and out of my terminal because I'm using iTerm 2. So I can hotkey in and out, which is very nice. So now you'll notice the reason I did savedev, that savedev flag, uh, for those of you guys who don't know, is it adds it to dev dependencies instead of dependencies. So when I push my repository to production, I can just run npm install production and it will only install these guys. Whereas if I run npm install, it will install the dev dependencies as well. Really all that does is it just speeds up my deployment a little bit. If you have 10 dev dependencies like gulp and stuff, then your deployment on production doesn't install any of those. It makes it just a little bit faster. You can install it here. It doesn't really matter. It's not going to blow anything up. So, flight plan is installed. So, now I can hit fly and it's going to say there's no flight plan.js. So, all I need to do now is add a flight plan.js file. Let's touch that. There we go. So, now it's created and let's copy in. So, now I'm going to copy and paste this flight plan.js code that I have for you in my gist. It's a slight tweak on what they provide for you by default. uh that's set up for forever. So there we go. That's my flight plan.js. Here's what you're going to do. You're going to give it an app name, which you can make whatever you want. This determines the folder that it deploys your app to. The username, which we made a user called deploy, and then whatever your start file is. So we run bin www to start our app. Some people call it server j server.js. Then the only other configuration you're going to do is you're going to set up your IP addresses for your staging and production servers. So, let me grab that IP address. There's my IP address. And that's my production server. If you had a staging server, you could also set that up. So, we could deploy to staging first and then deploy to production once that's successful. And you can set this up as an object or you can set it up as an array. And you can add multiple servers. So, when you do fly production, it will deploy to, I don't know, 10 or 20 of these servers if you want. I just have one right now, so that's all I have. And that's it. So now I can just run fly production and it will deploy to my production server. Fry fly production. There we go. It's sending it. Sending it over running my app with forever. And we are good. So now I can go to my IP address and I can go to port 3000. And check it out. There's my node application. And all I need to do at this point is buy a domain name and point it to this IP address and I'm good. So that's basically how you deploy. Um, and if I need to redeploy, I just run fly production again or fly staging to run to the staging servers and it's done. So let me show you kind of under the hood what this is doing for us, what what fly is actually doing, what flight plane is doing. So when you run it, the first thing it does is it gets all the files to copy. It runs this command on your machine. It runs all the local ones first. So if you have like a gulp build or a grump bit, a grunt build, you can uncomment that and it will run that first and then it will do run get ls files. And what that does is that gets a list of all the files that would be checked into your git repository. So it will ignore node modules. It will ignore anything that's in your git ignore file. I can even show you what this command does. Uh get ls files. It shows you right there. There's all my files and their full URLs. So that's kind of nice. Um, and then it copies all those files with one arsync command. You can see what that rsync command is here. It's a big old messy command that it does for you. So, it rsyncs them all in and then it runs npm install production. Um, it moves them to a temp directory, runs npm install production, and then it makes a sim link uh to the application name folder, which I'll show you what that is. That doesn't probably doesn't make sense. And then it stops your application with forever. And then it restarts your application with forever. Let's go ahead and log into our server SSH deploy. There we go. And let me show you what I got here. So we see that I have node app. That's a folder. And then I have two different folders for my two deployments. So I have history of all my deployments. And this is actually a sim link. So it deploys your deployment to a folder given gives it the time stamp. That's a time stamp right there. And then it says it makes a sim link. Node app now points to this folder. So if I were to make a new deployment, it would create my new folder, put all my new code in there, and then it would change the sim link. So now node app points to here. So then if I go cdspace node app, it thinks I'm in node app, but I'm really in my deployment folder because it's a shortcut. It's a sim link. And then it runs my my command which is forever start um and then whatever I've set, you know, bin www. And that's what flight plan's doing for you behind the scenes. Uh that's pretty much how you do an enjoyable deployment to any kind of environment you want. We call these environments, staging environment, production environment. And now we'll go into the next video where I'll show you how to enhance this uh by running our node app as a service. And that will make it an even stronger deployment strategy.
Original Description
In this video, we'll setup an automated deployment strategy for our node.js application.
Prerequisite videos!
- Setting up a server with Digital Ocean: http://youtu.be/W3rnQbsMb4Q
- SSH Tutorial: https://www.youtube.com/watch?v=DbPDraCYju8
Here's the code I'm using in this video:
https://gist.github.com/learncodeacademy/35045e64d2bbe6eb14f9
What we're doing in these videos is called automated-deployment. It's a part of devOps. DevOps is a set of tools/practices that focus on having a streamlined, heavily automated and reliable strategy development, deployment & monitoring of web applications.
We're going to setup a way to deploy our node app with a single command.
-~-~~-~~~-~~-~-
Also watch: "Responsive Design Tutorial - Tips for making web sites look great on any device"
https://www.youtube.com/watch?v=fgOO9YUFlGI
-~-~~-~~~-~~-~-
Playlist
Uploads from LearnCode.academy · LearnCode.academy · 55 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
▶
56
57
58
59
60
Web Development Tutorial for Beginners (#1) - How to build webpages with HTML, CSS, Javascript
LearnCode.academy
Web Development Tutorial for Beginners (#2) - Basic CSS - How to build a website with HTML & CSS
LearnCode.academy
How to create CSS Layouts - Web Development Tutorial for Beginners (#3) - with HTML & CSS
LearnCode.academy
Bootstrap Tutorial For Beginners - Responsive Design with Bootstrap 3 - Responsive HTML, CSS
LearnCode.academy
Angularjs Tutorial for Beginners - learn Angular.js using UI-Router
LearnCode.academy
CSS Tutorial - Web Development Tutorial for Beginners (#5)
LearnCode.academy
Node.js tutorial for beginners - an introduction to Node.js with Express.js
LearnCode.academy
Github Tutorial For Beginners - Github Basics for Mac or Windows & Source Control Basics
LearnCode.academy
Javascript Tutorial - Programming Tutorial for Beginners Pt 1
LearnCode.academy
Javascript Tutorial - jQuery Tutorial for Beginners Pt 2
LearnCode.academy
AngularJS Directives Tutorial - Part 1 - Demystifying Angular Directives
LearnCode.academy
WATCH THIS IF YOU WANT TO BECOME A WEB DEVELOPER! - Web Development Career advice
LearnCode.academy
YEOMAN TUTORIAL - Master Front-End Workflow with Yeoman, Grunt and Bower
LearnCode.academy
BOWER! - Streamline Web Workflow with Bower Package Manager
LearnCode.academy
Chrome DevTools for CSS - Better CSS Coding & CSS Debugging with Developer Tools
LearnCode.academy
GITHUB ATOM - Why Atom.io will be your favorite Text Editor!
LearnCode.academy
GITHUB PULL REQUEST, Branching, Merging & Team Workflow
LearnCode.academy
Pimp that Terminal - Add shortcuts and functions to your .bash_profile to simplify routine tasks
LearnCode.academy
jQuery Tutorial #3 - Writing Smarter, Better Code - jQuery Tutorial for Beginners
LearnCode.academy
jQuery Tutorial #2 - Event Binding - jQuery Tutorial for Beginners
LearnCode.academy
jQuery Tutorial #1 - jQuery Tutorial for Beginners
LearnCode.academy
Node.js MongoDB Tutorial using Mongoose
LearnCode.academy
Node.js tutorial for beginners 2014 - an introduction to Node.js with Express.js
LearnCode.academy
WEB DEVELOPMENT - SECRETS TO STARTING A CAREER in the Web Development Industry
LearnCode.academy
jQuery Tutorial #4 - DOM Traversal with jQuery
LearnCode.academy
jQuery Tutorial #5 - Building a jQuery Tab Panel Widget
LearnCode.academy
jQuery Tutorial #6 - Building a jQuery Image Slider
LearnCode.academy
jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
LearnCode.academy
jQuery Ajax Tutorial #2 - Posting data to backend (jQuery tutorial #8)
LearnCode.academy
jQuery Ajax Tutorial #3 - Delegating Events & Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
jQuery Ajax Tutorial #4 - "Edit" modes & Better Mustache.js Templating (jQuery tutorial #9)
LearnCode.academy
How to put your website online - how to FTP to a domain & upload files to a webhost
LearnCode.academy
Basic Terminal Usage - Cheat Sheet to make the command line EASY
LearnCode.academy
SSH Tutorial - Basic server administration with SSH
LearnCode.academy
Vagrant Tutorial - Running a VM For Your Local Development Environment
LearnCode.academy
Sublime Text Favorite Packages and Workflow
LearnCode.academy
What Makes Javascript Weird...and AWESOME - Pt 1
LearnCode.academy
Javascript is Event-Driven - What makes Javascript Weird...and Awesome Pt 2
LearnCode.academy
Javascript Closures Tutorial - What makes Javascript Weird...and Awesome Pt 3
LearnCode.academy
FREE REST API - Practice Developing Javascript AJAX Apps with this API
LearnCode.academy
Javascript Scope Tutorial - What Makes Javascript Weird...and Awesome Pt 4
LearnCode.academy
Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5
LearnCode.academy
Nginx Tutorial - Proxy to Express Application, Load Balancer, Static Cache Files
LearnCode.academy
Live Reload Sublime, Chrome, Anything - Fast and easy with Live-Server
LearnCode.academy
Are you bad, good, better or best with Async JS? JS Tutorial: Callbacks, Promises, Generators
LearnCode.academy
Javascript Generators - THEY CHANGE EVERYTHING - ES6 Generators Harmony Generators
LearnCode.academy
Web Development Advice - Interview with Dev Tips
LearnCode.academy
How the Internet Works for Developers - Pt 2 - Servers & Scaling
LearnCode.academy
How the Internet Works for Developers - Pt 1 - Overview & Frontend
LearnCode.academy
HAPROXY vs NGINX - 10,000 requests while killing servers
LearnCode.academy
Node.js Cluster - Boost Node App Performance & Stability with Clustering
LearnCode.academy
Web Dev Training with Treehouse
LearnCode.academy
What is Node.js Exactly? - a beginners introduction to Nodejs
LearnCode.academy
How to deploy node.js applications #1 - spin up a server
LearnCode.academy
Deploying node.js applications #2 - provision server & setup flightplan
LearnCode.academy
Deploying Node.js Applications - Deploy Node the right way - as an Upstart Service
LearnCode.academy
Mobile Web Design - Coding Workflow For Mobile Websites
LearnCode.academy
WHY YOU NEED A BUILD SYSTEM LIKE GRUNT, GULP, BRUNCH FOR YOUR WEBSITE
LearnCode.academy
GRUNT TUTORIAL - Grunt makes your web development better!
LearnCode.academy
STOP USING FTP! - How to Deploy with Flightplan over SSH
LearnCode.academy
More on: CI/CD Pipelines
View skill →Related Reads
📰
📰
📰
📰
I Built a One-Click Azure CI/CD Pipeline with Terraform & GitHub Actions From git push to a Live Website
Dev.to · David Cletus
Part 1.5: Optimizing Dockerfiles with Multi-Stage Builds
Dev.to · spmahapatra
The MCP server that changes its mind after you approve it
Dev.to · Abdul Manan
Ingress-nginx Is Retired: A Step-by-Step API Gateway Migration Guide
Medium · DevOps
🎓
Tutor Explanation
DeepCamp AI