Hosting Node.js, MongoDB, MySQL and PostgreSQL apps on Azure, AWS EC2, Vultr, DigitalOcean or any server

OpsCaptain Team — Mar, 08, 2018

All serious devs require some form of automation to push their Node.js applications into production as they develop and maintain their app. This automation however comes at a cost, without OpsCaptain, you either decide between developing your own automation tools or pay a platform as a service provider like Heroku to handle the process on your behalf on their servers. OpsCaptain provides the most cost effective automation tools to deploy your Node.js apps on the infrastructure provider of your own choosing at the price point most favorable to you. For example, you could purchase a $4GB server for less than $50 from most providers and use the OpsCaptain $10 tier to automate deployments of applications on your server, handle SSL certificate renewals and backing up your database. You can start using OpsCaptain for 14 days for free without providing your credit card details.

OpsCaptain uses the official Heroku Node.js buildpack to build your applications into a ready to run state which is then executed in Docker containers on your server. All the bleeding edge technology abstracted into a single "opscaptain deploy" command for the developer.

Step 1. Create your OpsCaptain account

Create a new account from the OpsCaptain sign up page. You will be required to verify your email via an email sent to the provided address. Once you receive the email, simply click on the account activation link to activate your account.

Step 2. Provision a server with your cloud provider and provide OpsCaptain root access

Provision a server running Ubuntu 16 from your cloud provider. After this server is provisioned and you are able to log in to the server you just provisioned, navigate to the OpsCaptain Add a New Ubuntu 16 host page and provide the Ip address, and password of your server. It is important you log in to your server first as some cloud providers like DigitalOcean require you first change your password before using the server.

Step 3. Create your app from the dashboard

Navigate to the Create a new app page and then enter the name of your app, select the database required for your app, select the server you added in step 2 and then click on the "Go Captain" button to create your first app. You can create and host as many apps you want on your server.

Step 4. Prepare your Node.js app to run in production

The runtime exports a variable with name PORT. You must listen for incomming connections using this variable instead of hardcoding any random port in your code which will only work in your local environment but not anywhere else.

Check out the below examples and how to properly configure the listen port for your Node.js app

var express = require('express'); var app = express(); // Use process.env.PORT instead of hard coded ports app.listen(process.env.PORT);

Connecting to MongoDB

For your app to connect to your database, the runtime exports the variables MONGO_URL, DATABASE_URI, DATABASE_URL, and MONGO_OPLOG_URL. Typically as you develop your app locally, you connect to the database bound on your localhost, this will not work in production and you must properly set your database connection string as shown in the example below:

var MongoClient = require('mongodb') // Connect to the db using the DATABASE_URI or MONGO_URL variables MongoClient.connect(process.env.DATABASE_URI, function(err, db) { if(!err) { console.log("We are connected"); } });

Connecting to MySQL

To connec to the MySQL database, you can use the runtime environment variables DATABASE_URI, DATABASE_URL, DB_USER, DB_PASSWORD, DB_HOST and DB_NAME as seen below:

var mysql = require('mysql'); //Create the connection using the DATABASE_URI variable var connection = mysql.createConnection(process.env.DATABASE_URI);

You can also connect to MySQL as seen below:

var mysql = require('mysql'); var connection = mysql.createConnection({ host : process.env.DB_HOST, user : process.env.DB_USER, password : process.env.DB_PASSWORD, database : process.env.DB_NAME });

Connecting to PostgreSQL

Use the DATABASE_URI environment variable to connect to your PostgreSQL database as seen below:

var pg = require('pg'); //Create the connection using the DATABASE_URI variable var client = new pg.Client(process.env.DATABASE_URL); client.connect();

Step 5. Create the ocmanifest.json file

In your project's root folder, create the ocmanifest.json file and then using the below example JSON, enter the name of your app and any environment variables you may have.

{ // The name you provided when creating the app in step 1 name: "my-nodejs-app", // Completely optional, configure environment variables if any env: { "STRIPE_API_KEY": "", "S3_ACCESS_KEY": "", "S3_SECRET_KEY": "" } }

Step 6. Deploy your app

First you must install the OpsCaptain CLI and then deploy.

// Install the CLI using the below command npm install -g opscaptain-cli // CD to your project folder and then deploy opscaptain deploy