This is a walkthrough of setting up a NodeJS website on digital ocean droplet by Dokku 0.4.2. If you know the tricks, its an easy way to deploye NodeJS sites to the cheapest, fastest hosting - Digital Ocean.
In my case, Im running a website on a subdomain (not www, not the apex) and I only want to run one site per droplet.
Creating the Image
- Sign Up at Digital Ocean and click to create a droplet
- Enter a host name - I recommend the full domain name (ie mysubdomain.mydomain.com)
- Choose a droplet of at least 1GB
- Under "Image" choose "Applications" and choose "Dokku" (for me its 0.4.2 on 14.04)
- Dont check any "Available Settings" boxes - IPv6 appears to be incompatible currently
- Add your ssh key or there will be pain (run
pbcopy < ~/.ssh/id_rsa.pub from the mac terminal to copy it to your clipboard)
Setting Up DNS
- In this example, I just want to deploye to a specific subdomain like
- Go to your registrar (ie Godaddy) and create an A-Record with the host=mysubdomain and point it to your IP address listed a the top of the droplets page (ie 220.127.116.11)
- It may take a bit of time to propagate, but for me its nearly instant
Setting Up Dokku
- Now you should be able to go to your website in your browser (ie mysubdomain.mydomain.com)
- If it hasnt propagated yet, you can go to the IP address (ie 18.104.22.168)
- You should see your public ssh key and your host name listed
- This is critical: Change your host name to just the domain (ie mydomain.com) and check "Use virtualhost". If you want to map a website to port 80, you basically have to use virtualhost.
- Press "Finish Setup"
Setting Up Your App
- At this point, Dokku docs leave you hanging a bit...
- Now ssh into the server by running this from your terminal:
ssh [email protected]
- It may ask "Are you sure you want to continue connecting...". Type "yes" and press return.
- Now setup your app on your server by running
dokku apps:create mysubdomain
- Now type "exit" to get out of your server
Deploying Your Code
- At this point, you need to have a working NodeJS project stored in a local git repository
- If you need a quick sample project, use
git clone https://github.com/heroku/node-js-sample to load a sample project on your computer
- Move into that directory
- Add the server location to your git repo:
git remote add RemoteNameHere [email protected]:mysubdomain
- upload your code
git push RemoteNameHere master
Viewing Your Site
- Now go to
mysubdomain.mydomain.com in your web browser
- If your app needs some env variables set (like NODE_ENV) then you can run something like
dokku config:set worker1 NODE_ENV=production
- So I created a 1GB RAM droplet expecting to run a 1GB RAM server. Ends up that dokku created my container at 512MB. I think this is because the daemon needs some ram and the zero-downtime deploy needs the same amount (this is a guess).
- So I enabled swap ram by following this guide