Using Digital Ocean with MEANJS
Warning: Development Blog Post.
So I’ve been playing with building a MEAN.js template for awhile but whats the point if I can’t host it anywhere?
So after playing with Heroku earlier I wanted to try another Node server. For this I decided on Digital Ocean, who although they don’t offer ‘free servers’, which was the main aim before. Do offer a surprisingly good offer of $5 a month for a pretty reasonable system.
Anyway onto the setup.
So after I setup a digital ocean instance, I wanted to redirect my domain to this new app. In Google domains this was pretty easy, I changed the name severs to:
I didn’t realise how easy it is to do a whois, on a mac its as simple as writing whois startuptrained.com — for which you’re able to grab my personal email. Something I need to change shortly!
Next in the DNS section of Digital Ocean click ‘Add Domain’, select your droplet, and then write in your domain. Pretty simple, huh? The A here says which subdomains it is for. If you were running lots of subdomains, you might just add in ‘test’ or ‘app’ here instead of ‘@’.
We’ll want to ensure that www redirects here so go to the ‘CNAME’ section after ‘Adding a record’ and select ‘www’ and then ‘@’, and the create.
Now we want to actually upload content from our local machine to it. To connect we use ssh to do this, write in your terminal:
Where ‘YOURIP’ is the IP address stated inside of Digital Ocean (not YOU should Rest in Peace, thats terrible). When you first login you’ll have to add in the password which was set to you by email, and then create a new password (enter the old password again, then the new one twice). It will log you out again so just relogin in.
Now, we want to push our code here. The way to do this is using Git. So I just uploaded my instance to a public GitHub project and then went to clone. But alas, when you do this it won’t let you because its not authenticated. To get around this we create a ssh key, using a passcode. This process is a little long but you can get it by following this guide:
SSH keys are a way to identify trusted computers, without involving passwords. The steps below will walk you through…help.github.com
As you’re on a Linux machine though you wont have pbcopy, so just use the following:
vim ~/.ssh/id_rsa.pub (copy the content then write “:q” to exit).
In essence you want to grab the content from git (after you’ve gone through the SSH key guide):
git clone email@example.com:yourname/yourgit.git
When I’d finally brought by content over I had to update apt-get and then install npm to get everything to work (I’d not selected MEAN which would have quite a few of these by default). To do this run (inside of the directory after the clone):
- apt-get update
- apt-get install npm
- npm install -g
- (at this point my Debian install had an issue, due to node being renamed ‘nodejs’, to solve this just)
sudo apt-get update && sudo apt-get install nodejs-legacy
- npm install bower -g
- sudo bower install — allow-root
- npm install grunt-cli -g
- npm install esprima -g
- npm install
This takes awhile but we’re pretty much there.
Now in order to actually view it you then grab the IP Address from before and use port 3000, i.e. in my instance at the moment its:
Now, of course we don’t want everyone to always put in that port, so to solve this we want to push port 80 requests to 3000. To do this you can do the following:
iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000
And then Tada, you can now see the site running on a domain like the below:
We’ll build ontop this to create a better template as time goes on, but for now we have a working Mean + Material Design template!