Tuesday, January 28, 2020

Deploy Node.js App To Digital Ocean Server

 

hey guys in this tutorial I'm going to
show you how to get a no js' app up and
running on a digital ocean server also
called a droplet now one important thing
that I want to stress is that there's a
ton of different ways to deploy an ojs
app and by no means am I saying that
this way is the best in fact I wanted to
create a tutorial using something like
flight plan for better automation but it
was an absolute disaster on Windows so
I'll be making a flight plan tutorial
but most likely on Linux but I did want
to make something for Windows client
users so let's take a look at what we'll
be doing now we're not just doing the
deployment we're also just setting up a
server from scratch I'm going to give
you a referral link that will give you
10 bucks or two months free for
digitalocean and if you want to keep
that going it's only going to run you
five bucks per month for the lowest
package and you can increase that at any
time so following along with this
tutorial will cost you nothing I would
definitely suggest digitalocean for all
of your server needs nodejs the mean
stack PHP applications pretty much
anything you can think of you
essentially get a dedicated server or
VPS that will will do anything you want
okay can do absolutely anything with it
so we're going to set up in a bun two
droplet which is just digital oceans
term for a server you can create these
on the fly and you can create as many as
you want so we're going to be using
Windows so we're going to need a Windows
client so we're going to need a way to
connect to our server so we're going to
be using a tool called putty all right
so we're going to need to setup SSH keys
for our client server as well so we're
going to use a tool called puttygen
we're also going to download a tool
called winscp which will give us a good
program to connect to our server so we
can actually drag and drop files and
folders to and from the server alright
so we'll set up and use the SSH keys
once we do that we're going to disable
the password log in for security
purposes we're going to create a new
user because we don't want to use the
root user in fact we're going to disable
the reviews or altogether
once our servers set up along with our
SSH keys will install nodejs
we're also going to need to install git
because we're going to use
a github repository - to bring our
application on to the server all right
we'll need to set up SSH keys for that
as well all right once we connect to
github we're just going to clone a
simple app and set that up on the server
now normally you would type NPM start or
node app and it runs your application
but it always needs to be running in the
command line and that's a huge problem
if your server resets or something like
that so we're going to be installing and
using something called p.m. - which will
allow our app to run as a process in the
background alright once our app is up
and running I'll show you how to link a
domain name to it using your
digitalocean droplet and then we'll
finally install a package called Lib cap
- bin and this will allow you to run the
app on port 80 so that you don't have to
provide the port number with the domain
name alright so it seems like a lot but
it's really not once you do it you'll be
able to you know deploy as many node
apps as you want from github again it's
not this isn't a complete production
guide you're going to want to look more
into server administration and security
and maybe a better workflow using
something like flightplan
but at this point you know it's going to
head you in the right direction alright
guys so we're going to go ahead and get
started now the first thing you want to
do is create an account at digitalocean
so you'll want to sign up and you'll
have to enter either your PayPal account
or a credit card but like I said the
referral link that I'm going to give you
gives you either two three months of the
lowest package or $10 free if you want
to get something bigger alright so go
ahead and sign up for that once you sign
up and you log in you should see
something like this okay so it's just a
list of where your droplets will be
wiped out all the droplets they had they
were just for testing and that's another
thing even if you're not going to use
this for production just sign up for the
lowest package just so you have a remote
server to do testing on you can you know
try out different packages different
software and it's just it's really
helpful and it's a great learning
experience
in itself alright so I also want to
mention that I do have a blog post on
traversée Mediacom that's going to kind
of go through everything we're doing
here I'm kind of give you some
instructions and give you all the code
all the snippets commands that we're
going to be using and will probably
reference this throughout as well
alright so we're going to go ahead and
click on create droplet and we're going
to have some options to choose from now
the first thing we need to choose is a
Linux distribution so we're going to use
a bun - it's what I would suggest and
you can use different versions as well
if you want but you also have the option
for FreeBSD Fedora Debian's core OS cent
OS and I believe there's others as well
and then just to show you if we click on
one click apps if you wanted to set up
for instance the mean stack or just a
MongoDB database or the lamp stack
Apache PHP you can do all that just one
click even content management systems
like Drupal Magento so that is really
helpful but we're just going to have a
clean blank server to work with okay so
we're not going to use any of those next
thing we want to choose is a size or
package now by default we have the
twenty dollar package highlighted which
is two gigabytes of RAM two CPUs 40
gigabyte SSD and three terabyte transfer
which isn't bad at all but we're going
to choose the lowest package which is
five dollars a month and it's 512
megabytes of RAM one CPU 20 gigabyte SSD
and a thousand gigabyte transfer and
that's another great thing about
digitalocean is that they use SSDs for
all of their servers which is makes
things much faster all right down here
we're not going to add any block storage
data center we're going to choose New
York you know you obviously want to
choose whatever is closest to you
okay you can add additional options like
private networking backups IP 6 user
data monitoring but we're not going to
we're not going to choose any of those
alright so now we're going to set up our
SSH keys now you don't have to do it
here you can do it later on but it's
easier to just get it out of the way so
we're going to go ahead and do this if I
click on new SSA
key it's going to ask us to taste in our
public key so to create that to get that
we're going to use something called
puttygen all right so if we go to putty
org and click on this link here there's
two things we need putty Exe is the SSH
client we're going to use to connect to
our server so you're going to want that
you're also going to want this puttygen
to generate our SSH keys all right now
these are actual programs they're not
installers so what I want to run it so
you want to put these somewhere wherever
you want to keep them so I'm going to
just grab them and I'm going to put them
in my C Drive and a folder called putty
alright and then we're going to open up
puttygen because we need to create our
SSH keys okay so it looks like this and
what we're going to do is click on
generate and it's going to say please
generate some randomness by moving the
mouse around the blank area all right so
now our key is created so what we want
to do is copy this whole thing so from
the very beginning down to the very end
copy that and then back in our signup
page we're going to paste that in and
we're going to name it I'm just going to
call this I'm going to call it public
key just public key one and then we're
going to add SSH key ok so now it's
added now before we move on here I just
want you to save your public and private
key so right here we'll click Save
public key and we're going to save it to
the desktop I'm just going to call it
public key one dot txt all right so that
will save it to the desktop which is
right here and then also save private
key it's going to ask you if you want a
passphrase you can make it a little more
secure if you want to you can put that
in here but we're going to just skip the
passphrase
and let's call this private key one and
that's going to give it a PPK extension
all right so we'll save that and now we
have our public key and private key
files so you want to put these somewhere
safe all right so we're done with
puttygen we can close that up and back
to our page over here if you want to use
the default hostname you can but I'm
just going to name it something else I'm
just going to call it Travis E media
droplet alright so we can go ahead and
click create and that will set up our
server in literally seconds all right so
our server is now set up that's the IP
address right there now if we click on
this it'll take us to this interface
where we can get some information the
CPU and input/output graph isn't showing
yet but that will come up momentarily
let's see we have this access tab where
we can access the console droplet must
be powered on is it not on yet it might
be oh it's still creating that's why but
from here you can access the console but
I would definitely suggest using
something like putty to interact with
your server you can reset the root
password you can power it on and off you
can resize okay so if you find out that
your package isn't enough and you need
some more memory or disk space you can
go ahead and upgrade that very easily
seamlessly all right what else backups
ok so if you want to enable backups you
need to pay an extra dollar a month but
you can take snapshots ok so you can
take a kind of a I guess a disk image or
a snapshot and restore that at any time
and then you can destroy it at any time
- ok and you can also just kind of reset
it if you install a bunch of crap that
you don't want you just want to start
from scratch you can just wipe it out
very easily
all right so our server should now be
set up so now what we want to do is
connect to it using putty and let me
just check to see think that's what we
want to do next
yeah so we want to connect to using
putty so let's go ahead and open up
putty Exe okay so inside putty what
we're going to do is we want to add our
IP address which is right here all right
now instead of connecting right now I
want to create a profile that we can
save and we can use at any time so what
we're going to do is go down to the data
right here connection and then data and
right here is where you want to put the
the username you want to use and by
default that's going to be root we
haven't created another user yet and
then we're going to click on SSH and
then off and then this is where you want
to upload your private key so we're
going to click browse we're going to
upload that private key one PPK file all
right then we're going to go back to the
session tab and we're going to give this
session a name I'm just going to call it
travesty media droplet and save that all
right and then I'll click open now
whenever you connect to a server for the
first time you'll see this message just
go ahead and click yes and there we go
we didn't have to enter the root
password or anything because we have our
SSH keys set up all right so the next
thing we want to do if we go back to the
blog post is create a new user okay
because you don't want to be using the
root user in fact we're going to disable
that altogether just for security
purposes so to create a user we can say
add user and let me just let me just
I'll make this a little bigger so you
guys can see it better see change
settings appearance font
all right so let's do add user and I'm
going to call my user Brad and it's
going to ask for a password we're going
to create a password for the user okay
it's going to ask for full name and then
this other stuff it asked I'm just going
to enter for that is this correct yes
all right now we can say I do Brad to
see if that yep okay so that user was
created but notice that it's he's not in
the pseudo group we want to put him in
pseudo so that we can run things as
administrator so to do that if we look
at the blog post here we want to use
this user mod HG sudo and then the user
name all right so let's go user mod a G
a G sudo Brad all right so now if we do
I see Brad you can see that now that
user is in the pseudo group so now what
we want to do is log in as that user we
can do that with this command right here
so that will be sudo su space - space
and then the user name alright so now
you can see that the prompt here is
changed to Brad at Travis tree media
droplet so we're now logged in as Brad
and if we want to double check we can
say Who am I and we get Brad alright so
now we want to do is authorize the key
for the new user because right now we
won't be able to connect with that the
SSH key pair with the Brad user so we
need to make a directory called dot SSH
in our home directory this little
squiggly line here this tilde that that
represents the home directory all right
so let's say mkdir for make directory
and we want to do the home slash dot ssh
alright next thing we want to do is
chmod we want to change the permission
to 700
so squiggly with Slashdot ssh okay now
what we want to do is create a file
called authorized keys and we want to
paste our public key in there so let's
say nano that's the editor we're going
to use and then we're going to say slash
tilde slash dot FSH slash authorized
underscore keys all right so now we're
in that file and we want to paste in our
key so what we're going to do is open up
the public key dot txt file we created
and it should look like this and what we
want to grab is from here to eat to the
end to here okay copy that and this has
to be perfect
all right and then in here what we're
going to do is we're going to say SSH -
RSA space and then we're going to paste
that in now this won't work because it's
on multiple lines we have to make sure
that everything is on a single line so
we're going to go right here click and
backspace once and we're just going to
do the same on each line okay because it
will not work if there's one flaw in
this file it just it won't work all
right so now everything's on one line
should be good so now what we're going
to do is control X to exit it's going to
ask if we want to save we're going to
say Y for yes and then enter all right
so now that file should be saved now
what we want to do is just change the
permission of that file to 600 we're
going to say chmod 600
[Music]
keys okay
now what we want to do is just restart
the SSH service we can do that with sudo
service that's the stage restart ok it's
going to apps for our password now what
we want to do is we want to test it out
and see if we can connect using Brad
with the ssh case so let's go ahead and
exit actually let's just close this out
and then we're going to go back into
putty alright now we're going to click
on the session that we saved and click
load it's going to load everything in
and then go to data and we're going to
change route to our new user which I
called Brad alright and then I'll save
it and then we'll open that up and now
you can see we logged right in no
password needed and we're logged in as
Brad so that's what we want
alright so now that we can we can
connect using the Brad user with the SSH
keys we want to now remove the password
login see where are we right here we
want to remove the password login and
the root login so we're going to have to
edit a file called SSH SSH SSH D config
all right so let's go ahead and do that
we're going to say sudo nano / e TC / SS
h / SS h d-- underscore config enter our
password okay so this is the file here
and now what we're going to do is a ctrl
W to search and we want to search for
permit login root is that it no permit
root login sorry so ctrl W permit root
logon all right so right here you can
see that it's set to yes we're going to
just go ahead and set that to no okay
now we're also going to do another
search for
password authentication so right here
password authentication we're going to
set that to no okay let's do control X
and yes enter and now we want to just
reload that sshd using this command here
so sudo systemctl reload the sage our
sshd okay so that should work now if we
want to test that out you can close our
session go back into putty and let's try
without the SSH keys let's see if it'll
let us with a password which it
shouldn't so let's grab the IP paste
that in click open let's say login is
Brad and it's not letting us it says no
supported authentication methods
available so that's exactly what should
happen alright so we're done with the
preparation of the security I guess
disabling the login and so on so now we
can start to install nodejs and you
start to deploy our application so again
what's going to putty ok and I'm going
to just load up our session here let's
make sure we're logging is Brad or
whatever you used and open alright
so let me just make this bigger
all right close this up close that so
now what we want to do is install nodejs
now on linux ubuntu we can't just do
apt-get install node j s it won't work
it needs to we need to tell it to use
the node source repository so if we look
at our blog post here we need this
command ok this curl command and this is
for node 6 if you want to use node 7 you
can just replace that with seven all
right so let's paste that in run that
and this is just standard if you went to
the node jf s-- documentation and looked
up how to install it on the bun tour
you'd see that command so now with that
done we should be able to say sudo
apt-get install nodejs
all right so no js' is now installed on
our server we can say node V dash V and
we see we have version 694 alright so
now that that's done we need to install
git on the server so we can do that with
sudo apt-get install kit ok say get V or
get version ok and then what we need to
do is we need to set some SSH keys for
github now let's take a look at the
application we'll be using so I'm just
going to search for sample node JSF
github so right here it's actually the
Heroku sample we're not using Heroku but
we're going to use this app it basically
is just a simple Express app and I think
it just says hello world but if we go
when we try to clone it yes you'll see
we get denied we give this permission
denied public key so we need to set up
SSH keys with github so if we go back to
the blog post we need to run right here
this SSH key gem and that will create
our keys to add to github all right so
let's do SSH - key gen and we want to
add - T RSA and then we need to put -
capital C and then put our github email
okay so enter the file where you want to
save the key just click enter now you
can put a passphrase if you want but I'm
just going to enter for no passphrase
alright so now we put the key in in home
Brad or whatever your username dot SSH
and then in a file called ID RSA dot pop
so we need to copy the contents of that
file now instead of trying to copy
through putty which is an absolute
nightmare we're going to use a tool
called winscp
which you should have anyways and it
gives us a way to connect to our server
with a graphical interface so we're
going to go ahead and download that we
want the installation package okay now
this is an installer so we'll just have
to install it real quick
all right so let's just go through this
so except in typical installation we're
going to choose the default here install
alright now it's asking us says we have
store stored sessions and putty talking
about our traversing media droplet do
you want to import them I'm going to
click yes okay and then we're going to
choose to launch it and notice that we
had now have Travis immediate droplet
and it has all the information and then
we'll click login all right so we're now
in our droplet if I go up one or up two
you'll see this is the entire server we
have in this in this program we can drag
and drop files as we want now notice
that in the home directory in the Brad
directory there's no dot SSH that's
because it's hidden by default but it
has a dot in front of it which makes it
hidden so what we want to do is click in
this little path thing right here and
just go slash dot SSH looks and ok and
then that will bring us into that folder
and you'll notice we have the ID RSA pub
file that's the github SSH key file so
let's bring that over to documents and
then I'm going to go to My Documents
folder and there it is right there let's
open that up I'm going to open it with
notepad and it should look like this all
right so we're going to just copy this
make sure you don't have any spaces or
anything at the end copy that
and then we're going to go to github ok
make sure you're logged in and then go
to settings and right here SSH in GPG
keys and we're going to say new SSH key
and just call it my key and then will
paste it in here all right so add SSH
key it's going to ask for your password
and now it's added so now we should be
able to clone the repository so let's
try it again
all right so now if we say LS to list
the contents you can see we have a file
a folder called nodejs sample alright
that's our application so all you would
have to do now is when you create an
application on your client just go ahead
and create a git repository upload it
there and then you can just pull it into
your server ok it's as simple as that so
now we're going to just CD into our app
so node J s sample alright now we're
going to have to run npm install to
install the dependencies that are in the
package.json file just like you would on
your client machine ok and then what we
can do is we can say npm start and it's
running on port 5000 so let's grab our
IP address and we'll go to our IP
address and then port 5000 and there's
our app ok not very pretty but it is an
Express application and it's running
remotely alright now there's a problem
with just running it like this because
if for some reason the server shuts down
or the stops it's it's going to just our
application is going to be dead so we
want to let's do control C to get out of
this and we're going to use something
called PM 2 so that we can run our
application as a process so let's say
sudo npm install want this globally so
dash G and then PM 2
all right now to run this app with p.m.
- we're just going to say p.m. to start
and then this this application has an
entry file of index J s now it could be
apps AF server J s this particular app
is indexed so we're going to say p.m. -
start index J s alright and now you can
see that our app is online our command
line is still freed up which is good and
if we go and we reload it's still
running ok now the next thing we want to
do is add a domain name because
obviously you don't want to use your IP
address so I'm going to go to Anam
central so I think it's calm and you'll
want to go to wherever your domain is
registered but I have a couple domains
here that I'm not using so I'm going to
go to C domains my domains and I'm going
to use this code learner dotnet domain
now what you're going to want to do is
login to your registrar and you're going
to want to change your name servers okay
so this is how you do it in enon but you
can do this in GoDaddy or Namecheap
wherever you're hosting your domain name
and you just want to add NS one
digitalocean com ns2 and ns3 all
digitalocean comm and then save all
right now it could take a half hour or a
couple hours to propagate so if what
we're doing here doesn't work right away
don't worry about it
it might just need to kick in alright
but go ahead and change those I already
have those set and we're going to go
back to digitalocean and we're going to
go to networking oops I already have it
let me just delete that you guys won't
have that alright so we're going to want
to add the domain
okay and then what we're going to want
to do here is add an a record now if you
wanted this to be at a sub-domain like
app code learner dotnet you would put
that in I want it to be at the actual
root code burner dotnet so I'm just
going to put the @ symbol and then here
you can that you either want to put the
IP address but digitalocean actually
gives you this nice drop-down of all
your droplets and you can choose which
one you want then we're going to click
create record
okay records created successfully next
thing we're going to do is go to cname
and we want to make sure that WW version
goes to it as well so just put in www
and then here we're going to put the @
symbol and create record all right so
now if we go to http code learner dot
net and we're going to go to port 5,000
and there's our application so our
application is now connected to a domain
now the last thing you're probably
wondering is how do we get rid of this
is 5,000 you're not going to want that
that's not very professional so to do
that we're going to change our app to
run on port 80 which is the default HTTP
port now if you just do that alone
you're going to get errors it's not
going to work but there's a package we
can install that'll let us use it so
what we're going to do is stop the app
with p.m. to stop index dot G is okay
that'll stop it and then I'm going to
use winscp actually no we don't need to
do that lets just use nano so we'll say
sudo nano and we want to edit index dot
JSP
what I do sue so Sukho okay and then
what we're going to do is just change
the port okay right here it's five
thousand let's change that to 80 all
right and then we'll just control X and
then Y enter all right so if we try to
run it let's say NPM start it gives us
this error basically saying hey you
can't run it on port 80 so what we're
going to do is install a package so if
we go back to the blog post and read
here this sudo apt-get install Lib cap -
- bin that's what we want so let's go
ahead and do that okay and then we want
this last command here this is the set
cap basically we're going to bind the
service to that port so we're going to
just copy that and to run it okay and
now we should be able to start our
application and it will run on port 80
so let's do p.m. start index dot JSP m2
okay so now moment of truth we're going
to go to HTTP code learner net note port
number and there we go so our nodejs
application has been deployed to a
remote server and we connected a domain
name to it alright so I really hope you
guys learned something from this now
like I said this isn't I'm not going to
say this is the best way to do things in
fact I'm sure there's many of you tech
guides that are criticizing certain
things that I've done throughout this
tutorial but you know this is a free
tutorial and I'm just trying to help
people out trying to give them an idea
of what it takes to deploy an
application I will be making other
deployment videos with nodejs and others
my next video will probably be using
flight plan from a Linux machine a Linux
client
and I'm not really sure what we'll do
after that maybe some mean stack stuff
but again hopefully guys like this if
you did please subscribe please leave a
like and I will see you next time

VPS vs Cloud Hosting | Should You Use Web hosting?

  cloud hosting verse virtual private servers which one should you choose well ...