Development to Production in less than 5 minutes

Introduction

In this tutorial, we will cover setting up a production-ready Node.js environment. Our one main server is composed of two servers : App Server and Web Server.

Courtesy from DigitalOcean

One server will run Node.js application, while the other will provide users with access to the application through an Nginx reverse proxy to the application server.

To put our application on server, we will use dokku.

Dokku is a mini-Heroku powered by Docker written in less than 100 lines of Bash. Once it’s set up on a host, you can push Heroku-compatible applications to it via Git. They’ll build using Heroku buildpacks and then run in isolated containers. The end result is your own, single-host version of Heroku.

Dokku makes it easy to deploy and manage web applications on your own server in a way that’s very similar to Heroku. Except on DigitalOcean, it’s faster, cheaper, and you have more control. Now DigitalOcean has a one-click application for creating a Dokku Droplet, making it even easier to have your own private application platform in a matter of minutes.

Prerequisites

  • You must have a DigitalOcean account where we will create our dokku droplet. If you don’t have yet, click here and make one.
  • You must have an application which we will soon deploy on server. If you don’t have a NodeJS application yet, you can create one by using express-generator.
⚠️ Just follow these steps and soon you will have a NodeJS application running and ready to deploy.

Spinning up our server

Before doing anything, first we will setup our server. We will do some modifications in the creating process but other than that, all things are pretty much same as it is. Like the size and locations of the droplet, SSH keys, etc. you can choose whatever you want.

Note : If you haven’t create droplet before, you can follow this basic guide. But its recommended to have a experience with droplets. If you do not have that right now, first go and have one. If you face any kind of problem while creating your first droplet, feel free to ask me;
You can have $10 credits in your account on first registration. Click here to get one.
  • Login into your DigitalOcean account and Click on Create.
  • Now click on One-Click Apps and select Dokku.

This is the only major thing which you have to make sure about. After this you can choose whatever setting you want i.e you can choose size, location and SSH keys as per your choice.

Its recommended to use 5$/mo if you want to learn how to deploy application and if you really want to use this server as production, Then please go for 10$/mo.

Then wait for some time until DigitalOcean will make your server ready.

Instead of XX.XXX you will see your IP address here

You will receive a notification soon.

Remember, it will take little time to spin up your server after this notification also. So sit back and please wait, it will take approximately 2–3 more minutes because during this time our server is preparing dokku configurations.

Configuring Dokku

Copy the IP address and visit it in chrome. You will see a dokku configuration box. If it isn’t showing, then wait for some more minutes and try again later. 
( ⌛️ There is nothing you can do in it. )

If you used a DigitalOcean SSH key with your Droplet, it should already be filled in for your admin SSH public key. Anybody set up with this key can deploy and manage apps on your Dokku. You can add more later, but this one is for you. If there’s no key in that first box, you’ll have to create a keypair and paste in the public key. Remember, here’s a great article if you forgot how.

⚠️ Change Hostname to your domain name if you have any. You also have to first configure this domain with your DigitalOcean account.
  • Click Finish Setup and you will redirected to dokku documentation. Thats it! Now the only thing we have to do is push our code to this server.
Congratulations!, If you reached here, you are doing very well.👍

🔱 Final Touch

Now we will push our code to our dokku server. Before this we have to prepare our application. Your app must have following things :

  • A package.json file in the project root.
     — This is the file which tell dokku that you have a NodeJS application.
  • Your package.json must contains a start script.
     — Dokku will execute npm start in order to run the app after deployment. Thats why it is must. Click here if you don’t know how to make a npm script.
  • Your application must have at least one commit.
     — If you are not familiar with git. Click here to learn how to make your first commit.
📕 Read this guide to know more about how to make your NodeJS application ready for deployment ?

If your application has all the things listed above, you are good to proceed.

Now, you have to add dokku remote with your repository. The remote will be something look like as shown below :

⚠️ If you don’t know what Git Remote is ? Click here.

Now fasten your seat belt and get ready to deploy 🚀. Execute the following command :

git push dokku master
⚠️ If you want to push another branch, you have to specify that like this :
git push dokku <your_branch>:master
You must have to push your code to master branch because dokku has hooks on master branch. If you push your code to any other branch, your code will not be live on the server but it will get pushed to dokku.

Below is the output which you will see while dokku is pushing and deploying your application on server.

-----> Running post-deploy
=====> renaming container (56afae26699c) upbeat_jackson to myapp.web.1
=====> no global VHOST set. disabling vhost support
-----> Unsetting myapp
-----> Unsetting DOKKU_NGINX_PORT
-----> Unsetting DOKKU_NGINX_SSL_PORT
-----> Unsetting DOKKU_PROXY_PORT_MAP
-----> Setting config vars
NO_VHOST: 1
-----> no nginx port set. setting to random open high port
-----> Setting config vars
DOKKU_NGINX_PORT: 17518
-----> Setting config vars
DOKKU_PROXY_PORT_MAP: http:17518:5000
-----> Creating http nginx.conf
-----> Running nginx-pre-reload
Reloading nginx
-----> Setting config vars
DOKKU_APP_RESTORE: 1
-----> Attempting to run scripts.dokku.postdeploy from app.json (if defined)
=====> Application deployed:
http://139.59.XX.XXX:XXXXX
To 139.59.XX.XXX:myapp
* [new branch] master -> master

🎉 Congratulations! You have your application live now.

Just click on the link( http://139.59.XX.XXX:XXXXX) provided by dokku or copy and paste this into chrome and Enjoy!.

🎥 In Case, Here is the video

If you have any kind of issue while executing the command above. Click on the video given below and watch the sequence and command i executed.

Conclusion

In this tutorial, we learned how to make a production ready NodeJS Server. Now you have to just commit, push to dokku and your application will live within a fraction of minute.

Next week we will learn how to make our dokku application SSL Secure / HTTPS. Stay Tuned.

Happy Coding !


If you like this article, please ❤/👏 or share it! For more like this, follow me on Medium or Twitter. To ask a Question visit this link. More about me on mywebsite.

Don’t forget to subscribe our weekly-newsletter service for web hackers. All latest articles and stuff like trending repositories, straight to your mailbox.

Like what you read? Give Gaurav Mehla a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.