Image for post
Image for post

How To Deploy And Scale Your Meteor Apps

I believe that you already had a good preparation after digesting our previous blog What To Consider Before Deploying A Meteor App, right? Now, it’s time to get your hands dirty. Ready? I’m going to show you how to bring your app into practice.

To begin with, I create 4 servers in AWS (Amazon Web Service) with 4 different IP addresses:

I have chosen the domain which is www.ami.com for the app. I use MongoDB because it is the default database of Meteor. There we’ve got:

  • MONGODB: 54.174.9.184, 54.174.9.185, 54.174.9.186, 54.174.9.187.
  • METEOR APP: 54.174.9.188 (sever-1.ami.com), 54.174.9.189 (server-2.ami.com), 54.174.9.190 (server-3.ami.com), 54.174.9.191 (server-4.ami.com)

Replication sets provide redundancy and increase data availability on different database servers. Hence, I apply replication to increase the performance of reading operations.

Image for post
Image for post

MongoDB Replication

At first, I create an admin user following precise instructions on https://docs.mongodb.com/v2.6/tutorial/add-user-administrator/ and generate a key-file by doing exactly guides on https://docs.mongodb.com/v2.6/tutorial/generate-key-file/

Next, I apply SSH (Secure Shell) to all of 4 servers and run these following commands

>cd ~/
> mkdir mongodb/
> screen -S mongodb
> mongod --dbpath mongodb/ --replSet Ami --auth --keyFile mongo-keyfile

To close terminal session, I use the hotkeys “CTRL + a + d”.

Then I turn 54.174.9.184 into primary MongoDB by running these following commands:

> mongo
> use admin
> db.auth(‘admin’, ‘password’)
> rs.initiate()
> rs.add(‘54.174.9.185:27017’)
> rs.add(‘54.174.9.186:27017’)
> rs.add(‘54.174.9.187:27017’)

In order to set up the app, the first thing I need to do is to install meteor up by using npm. Below is the command line to use:

> npm install -g mup

Setting up is a must. To do this, use the cd path command to go to the folder which contains settings file of the application:

> cd path/to/settings
> mup init

This is how I scale the app. In this file, I edit mup.json file by:

  • Adding servers
{
"host": "server-1.ami.com",
"username": "ubuntu",
"pem": "./ssh.pem",
"env": {
"CLUSTER_ENDPOINT_URL": "https://server-1.ami.com",
"CLUSTER_BALANCER_URL": "https://server-1.ami.com"
}
},
{
"host": "server-2.ami.com",
"username": "ubuntu",
"pem": "./ssh.pem",
"env": {
"CLUSTER_ENDPOINT_URL": "https://server-2.ami.com",
"CLUSTER_BALANCER_URL": "https://server-2.ami.com"
}
},
{
"host": "server-3.ami.com",
"username": "ubuntu",
"pem": "./ssh.pem",
"env": {
"CLUSTER_ENDPOINT_URL": "https://server-3.ami.com",
"CLUSTER_BALANCER_URL": "https://server-3.ami.com"
}
},
{
"host": "server-4.ami.com",
"username": "ubuntu",
"pem": "./ssh.pem",
"env": {
"CLUSTER_ENDPOINT_URL": "https://server-4.ami.com",
"CLUSTER_BALANCER_URL": "https://server-4.ami.com"
}
},
}
  • Adding MONGO_URL

MONGO_URL is one of environment variables in Meteor, which is used for connecting to Mongo in production environment. To add MONGO_URL, I run the following command:

{
“MONGO_URL" : "mongodb://lumin:XXXXXX@54.174.9.184:27017, 54.174.9.185:27017, 54.174.9.186:27017, 54.174.9.187:27017/Ami?replicaSet=Ami"
}
  • Enable OPLOG

Oplog or operation log is a special collection that records all the write operations as they are applied to the database (you can find more helpful information at: https://github.com/meteor/docs/blob/version-NEXT/long-form/oplog-observe-driver.md)

The command used to enable Oplog is as followed:

{ MONGO_OPLOG_URL: ‘mongodb://lumin:XXXXXX@54.174.9.184:27017, 54.174.9.185:27017, 54.174.9.186:27017, 54.174.9.187:27017/ local?authSource=admin
}

Are you done all of these above steps? Let’s move to deploy our app. Firstly, I go to settings folder of the app.

  • Setup servers when using MUP for the first time
> mup setup
  • Bundle your project and deploy to servers
> mup deploy

So far, I have deployed my app successful. MUP also support other useful commands to maintain your production app. After you’ve edited environmental variables of settings.json, you can reconfigure the app without deploying again:

> mup reconfig

Check out other useful commands:

> mup stop # stop all servers
> mup start # start all servers
> mup restart # stop all running servers and start it again

Congratulation! Your Meteor app is now ready to dance to your tune. Keep an eye out for our further blog on Meteor tutorials and you will find out more helpful information. I would love to know whether you deploy your app successfully or not in the comments below.

Written by

Designveloper is leading software development company in Vietnam. We offer Web development, mobile application, UI/UX Design, VOIP. www.designveloper.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store