Chapter 4: Modulus
One of the simplest services for deploying a Meteor application to the web is Modulus.io — a service that’s designed to host Node applications and Mongo databases (although we can use an externally hosted database).
Hosting applications with Modulus has a number of advantages:
- The interface is user-friendly.
- There’s minimal setup involved.
- You can scale with ease.
If you’re looking to launch your first Meteor project to the web and don’t want to worry about too much of what’s going on behind the scenes, then Modulus is what I generally recommend.
To begin, register for an account with Modulus. You’ll be provided with a $15 credit, which is enough credit for a month’s worth of free service.
There’s no free plan, but you’re at least not required to enter your credit card details upon sign-up.
Creating a Project
After registration, click the “Create New Project” link:
Here, the word “project” is synonymous with “application”. For every application you want to deploy to the service, create a new project.
You’ll be given a choice of how much RAM the project will need. By selecting the 396mb option, you’ll receive a month of free hosting based on the $15 credit.
Choose a name for the project, then click the “Make It So” button. You’ll be taken to a page with a range of instructions, but you can ignore these.
Creating a Database
At this point, we have a choice:
We can create a database on Modulus, or we can use a database that’s hosted with Compose. Creating a database on Modulus is cheaper (and easier in the short-term), but I prefer hosting databases using an external service because:
- The database management features are slick.
- It’s easier to move the project to a different server.
- Your database will scale automatically.
To cater for all preferences:
- I’ll show you how to create a database on Modulus.
- You can skip this step if you’d like to use Compose.
To continue with this step:
- Return to the dashboard (my.modulus.io).
- Click the “Databases” link in the sidebar.
- Click the “Create Database” button.
Define the following settings:
- Provider and region. (The default value is fine.)
Then click the “Create” button.
You’ll be shown two values:
- Mongo URI
- Mongo Console
(These are the same types of values that are provided after creating a database on an external service such as Compose)
Copy the URI value into a text file, replacing the “user” part with the database username and the “pass” part with the database password.
It should look something like this:
In a few minutes, we’ll see how to use this value.
Command Line Tools
Before we continue, we’ll need to install a set of command line tools that Modulus provides to streamline the deployment process.
To install these tools, enter the following command into the command line:
npm install -g modulus
Then tap the “Return” key. (If the command doesn’t work, try the command again with
sudo at the front of it.)
Once the tools are installed, navigate into the directory of a Meteor project with the
cd command. I’ll be using the “Todos” application as an example, which is inside a “Meteor” folder I created:
Then run the following command:
When prompted, enter the username and password for your Modulus account. (To be clear, this is not the username and password for your Modulus database, but for the account itself.)
You should see a “Signed in as user” message:
We’re about to deploy the project to the web, and this will be similar to how we deploy the application in the future, but since we haven’t deployed this particular application before, there’s a couple of extra steps involved.
For the moment, run the following command:
Then, when prompted:
- Confirm the deployment.
- Wait for the files to upload.
- Take note of the URL that appears.
This is the URL of the Meteor application that’s now live on the web.
But if we copy and paste the URL into a browser, we’ll get an error, and that’s because of those couple of extra steps we still have to take.
- Return to the main dashboard.
- Click on the project we created earlier.
- Select the “Administration” option in the sidebar.
- Scroll to the “Environment Variables” section.
You should see the following:
In this section, we want to define:
- The location and login details of a Mongo database.
- The Root URL of the application itself.
Once these values have been defined, we’ll be able to deploy the application for a second time, and from that point onward, everything will work as expected.
Click on the first empty field — beneath the “NODE_ENV” field — and write:
Then, in the field beside this, paste a Mongo URI value. This could be the value from Compose or Modulus — or any other database host. It doesn’t matter. Just make sure you end up with something that resembles:
In the next empty field — beneath the “MONGO_URL” field — write:
…and in the field beside this, copy and paste the URL of the application that appeared inside the command line (including the “HTTP” part):
Then click the “Save” button.
Most of what we’ve done so far — creating the project, database, etc — only needs to be done once when setting up a new server. So while the process has been a bit fiddly, deployment from this point onward is easy.
We only have to run a single command:
It’s the same command as before, but since we’ve defined the environment variables, the application will now work as expected.
Here, we can see my deployment of the “Todos” example application:
If you were to make a change to the project’s files, you would simply have to run the
deploy command again to push those changes to the world.