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
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:
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:
You can also connect to MySQL as seen below:
Connecting to PostgreSQL
Use the DATABASE_URI environment variable to connect to your PostgreSQL database as seen below:
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.
Step 6. Deploy your app
First you must install the OpsCaptain CLI and then deploy.