Deploy to production on Heroku
In the last post we talked about deployment with the NPM-Package Meteor Up.
Today we want to take a closer look into the deployment of a Meteor application to Heroku.
What is Heroku?
Heroku is a very nice PaaS provider which makes it dead easy to host your Application (it doesn’t matter if it’s a Node, Ruby, Python or other application) in the cloud.
You can scale your application in a matter of seconds and add addons (such as MongoDB Provider, Mailgun, …) to your application. You can easily deploy your newest version of your application with a simple git push command which is pretty neat!
We won’t go into any details about Heroku here (I’m just a Heroku fan and not related to the company :-D). There are other different competitors such as e.g. Modulus, AppFog or Scalingo. Check them out and find the service provider which fits best to your needs.
1. Install the Heroku Toolbelt
The first thing you need to do is to install the Heroku toolbelt. The toolbelt is a command line software which makes it easy to interact with the Heroku service through your command line.
Go to the toolbelt-Page and follow the instructions to install the software on your machine (I’ve used the standalone version, but it’s up to you what you use).
2. Create a Heroku account
Sign up for Heroku here.
3. Add Git-Support for your Meteor application
Because Heroku uses a Git-Repository where you push your code to when deploying your application you need to add Git version control to your Meteor application (if not already done).
cd into your applications folder and run:
git init<br />
git add .<br />
git commit -am "Initial Commit"<br />
to setup a git repository and commit the current status to your local repository. (Play through this primer if you want to learn more about Git)
4. Create your Heroku app
Open up a terminal and create your Heroku app with the following command:
heroku create your-app-name<br />
The name of your application should be unique. You could leave it blank if you want Heroku to create one for you:
heroku create<br />
You may be prompted to sign in to your Heroku account on the command line. Behind the scenes Heroku created an app-Container for your application and added a remote Git-Repository (with the name “heroku”) to your local Git settings which points to your Heroku application.
5. Setup the Heroku app
It’s important to let Heroku know that the application we want to deploy is a Meteor application. We could configure this by hand but there is a better solution available: A Meteor Heroku Buildpack. A Buildpack is a package with information for Heroku so that it knows how to setup and configure the server-Container (in which your application runs). There are several Meteor specific Buildpacks available. But we’ll use the well known “Horse” buildpack here (which has always suited well). In your Meteor project folder run this command to add the buildpack to the Heroku container you have configured recently:
heroku buildpacks:set https://github.com/AdmitHub/meteor-buildpack-horse.git<br />
After that we need to add the MongoLab Addon to our application (MongoLab is a MongoDB service provider with production ready MongoDB Databases):
heroku addons:create mongolab<br />
The last thing we need to do is to set the ROOT_URL environment variable. This variable is used internally by Meteor and is necessary so that the application can run smoothly. Every Heroku-App has a URL which contains the name of your app (e.g. awesome-meteor-project.herokuapp.com). We use this Domain as the ROOT_URL environment variable with the following command:
heroku config:set ROOT_URL=https://your-app-name.herokuapp.com<br />
You can set other Meteor related environment variables (e.g. the MAIL_URL) the same way.
That’s it. Our Heroku app container is now successfully configured. Let’s deploy our app.
6. Deploying the App on Heroku
This is pretty simple. Just run the following Git push-Command in your Meteor projects directory:
git push heroku master<br />
Heroku will now receive your Git repository, check the Buildpack and build and deploy your application accordingly. Your app should be up and running after the push is completed (which takes a little bit longer than “normal” push to e.g. GitHub). This command can be re-Run after new changes are commited to the Git-Repository to deploy the newest version.
7. Manage your Heroku app container
You could also sign in to your Heroku account to configure your app containers if you don’t want to use the command line.
Just visit the sign in page and enter your credentials. After that you should see all your containers. Feel free to tinker around.
Pro tip (enable sticky session-Support)
One of the downsides of using Heroku with Meteor was always the lack of sticky session support. So basically you could only use one Dyno (App-Container that runs inside a Server in Heroku speak) to serve your requests, because if the router forwards your next request to another Dyno than the one you’ve used before it looses the knowledge of your current state which affects user experience negatively. Heroku recently announced session affinity (sticky session support) so that you can scale up your Dynos without affecting the user experience. As of this writing session affinity is not supported per default. You need to enable the support (which is in Beta status) by hand. Run this command so that your Heroku application can use the sticky session support:
heroku labs:enable http-session-affinity<br />