Using a VPS
You don't want to serve a website from your personal computer, because any time your computer is off, the website will be down. You don't want that kind of responsibility with your computer, so much of the time people choose to essentially rent server space from companies that's sole purpose is to get you space/bandwidth on a simple computer that is always on. These are often called VPS's (virtual private servers).So the first step I'd recommend is to grab a VPS for yourself. Digital Ocean is a great service that you can get a solid server from for $5/month, I would recommend starting there. There are bunches of other companies you can get VPS's from though if you prefer, probably the most popular alterntive being linode.
Once you've got yourself a VPS, log in to it using ssh. Usually it will look something like this:
ssh root@000.000.0000
...with the number at the end being the IP address of your server.
Most VPS's are some flavor of linux, so being familiar with the linux
command line interface is important. Once you're all set in your server,
you'll want to do a few things. This is what I usually do, in order:Install vim
For me, vim is the easiest way to edit files through the command line. This certainly might not be the case for everyone - some people prefer emacs, and some nano, which is a lot simpler. If you are interested in learning about vim, there are loads of tutorials around the 'net. If getting into vim isn't your thing, I'd recommend using nano instead wherever I mention it from here on.To get it installed, we can use
apt
, which is aptitude,
the package manager on ubuntu, the flavor of linux I'll use in this
answer since it's a popular one for servers, and is the default for
digital ocean. Just run apt-get update
to make sure packages are up to date, then apt-get install vim
to put in vim.Add your ssh key
Add your ssh key to~/.ssh/authorized_keys
so that you don't need a password to log in. If you are unfamiliar with ssh keys,
they are basically a pair of cryptographic keys that you can use to
avoid needing to authorize with a password every time. By adding your
public key to the ~/.ssh/authorized_keys
file, you are
essentially telling the server "this is my computer, so you don't need
to ask me for a password to log in". Github has a great guide
on how to generate keys. Once this is done, you can open the file with
vim, get into insert mode, and paste the public key in from your local
machine. Save and quit and you're set.Install node.js
If you are trying to run a node app, you will of course need to have node! Installing node on linux is a bit different because the node installer I'm sure you used locally is graphical, where here you only have the command line. Luckily, it's not much more difficult with this set of instructions, which you can follow exactly. Make sure you do not just do the defaultapt-get install nodejs
, as this will install an old version. Take the couple steps after the second paragraph to add ppa and get a newer version.Deploy your app
Ok, so you have a machine that has node and theoretically could run your app. This is good news. Now we need to actually get the app onto the machine. There are a few ways you can do this. If you have ruby installed locally, you can use capistrano, a popular deployment solution. A lighter weight approach that I often prefer is deploy, although I don't think that will work on windows. You can also just use github or bitbucket - push your app to a remote repo then clone it down from your VPS (make sure toapt-get install git
and set up
your username first - if it's a private repo you'll probably have
generate and add a key to get access to pull it down). However you
manage to do it, get the files transferred.Test your app
On your VPS,cd
into wherever your app was put and run it. Make sure everything is working ok, and hit http://YOUR_IP:PORT
,
just your ip address followed by a port number that your app is running
on after the colon. You should be able to see your app. If not check
back to the terminal, it may have crashed. Sometimes you can find flukes
when you are setting it up on a different system. If your app uses a
database, you might need to get this configured too. You can google
"ubuntu setup database name" and find some tutorials -- digital ocean
has a pretty solid library of these types of tutorials themselves.Install nginx
Nginx is a great way to serve multiple apps on one machine, and to handle domain names and such. I wrote an article on how to set up nginx that you can check out to learn the basics and get it installed. Once this is done, you can link up your app with aproxy_pass
. Rather than try_files
, which is what the article does to server static files, just drop in a proxy_pass
statement to the port your app is running on instead, and nginx will
direct traffic right through to your app. Here's an example, if you had
your app running on port 1234
and your domain name was example.com
server {
server_name example.com;
location / {
proxy_pass http://localhost:1234;
}
}
This will just take traffic coming into the box from example.com
and pass it to your app, which is awesome.Get your domain in order
I have to assume you don't want to require people to use an IP address to access your app, and you want a domain name. Go grab one from wherever, and once you have this you need to edit the DNS records. I've found that it's easiest to use dnsimple for this, as not every domain registrar has solid dns record handling, and you can keep all your dns management in one place. Now, just put an A record on the root of your domain, pointing it to your VPS's IP address. After giving it a couple minutes for the records to propigate, a hit to that domain should go directly to your server - fantastic.Now is the time to check through and make sure that your app is running properly and that your nginx configuration is correct (and that you have reloaded nginx). Make sure that in your configuration, the
server_name
mirrors the domain you set to point at your VPS. Make sure the port in the proxy_pass
is the same as your app is running on. Once this has been confirmed, go
to the domain, and if you did it right, your app will come up. Whoo!Run it on a production server
Great, so we got our app running and it's online on the internet for the public to enjoy. Just about time to sit back and let everyone throw money at you, a common occurance whenever you get a site shipped. But don't recline too quickly, because the last thing we need is to make sure this app stays up and continues running even if something goes wrong, or you log out of your VPS, so you don't always have to keep a terminal window open running the app. For this, we can use what some call production servers -- servers made specifically to ensure that your app runs in the background and stays running all the time. Luckily, node has a few of these open source, my favorite beingpm2
. Check out this page, read the getting started instructions, install pm2
on your machine, and run your app. The process might look something like this:npm install pm2 -g
cd path_to_my_app
pm2 start app.js
Since you ran it on the same port, your nginx configuration should
remain the same, and your app should still be up if you visit the
domain.Phew, that was a lengthy process. Probably more than you expected - makes sense why something like heroku exists. So is this really worth it, running and maintaining the site yourself? I'd argue yes, and I host every one of the sites and apps I run like this. Here's why:
- learning: I learn tons about how things work this way, and get much better at sysops.
- cost: You can host like 20 sites on a single $5 digital ocean box. hosting is pennies.
- control: Heroku sometimes goes down and it sucks because all you can do is wait for them to get it back up. If my site goes down, it's my fault and I can find out why and fix it.
Finally, I want to note that this is without a doubt an opinionated guide. There are tons of other tools and other ways to do these things -- the workflow I have here is just the way I prefer to do things. By all means feel free to tinker and suit the workflow to your needs once you have it under your belt! There are also lots of other details that could be added in here about setting up different databases, improving your deploy/restart flow, and securing your box a little more throughly. Would love to hear any feedback and add any of these pieces in if you or others are interested.
from http://stackoverflow.com/questions/19625025/install-a-server-for-a-node-js-web-service-application/19625953#19625953