Hosting a Meteor App on Nodejitsu
As development of Proper moves forward, I’ve started to toy with a good way to handle hosting. After doing a bit of research, I came across a short list of PaaS providers, finally landing on Nodejitsu as the most attractive for my needs. If you’ve investigated using Nodejitsu, too, you’ve probably come across this Stack Overflow post that mentions a problem with getting the Node Fibers package up and running.
Fortunately, the Demeteorizer tool created by the Modulus team (another PaaS provider) helps us get around this. In this tutorial, we’ll go over the process of using Demeteroizer, getting a Nodejitsu account running, and deploying a working app.
The first step is to “demeteorize" your app and prep it for deploying to Nodejitsu. The instructions in this post are what you’ll need to follow, but it’s important to note that you’ll only need to do two things: install Demeteorizer and then run it in your project directory. This can be a little confusing, so let’s step through it.
First, run the command to install demeteorizer via the NPM (Node Package Manager) CLI:
npm install -g demeteorizer
Once that’s installed, switch into your project directory and demeteorize it:
cd myproject && demeteorizer
This will do some magical fun stuff and then output a raw Node version of your app in the .demeteorized directory (this will be in your project root unless you specified otherwise). After you’ve done this, pop open the .demeteorized directory and then open up package.json in a text editor. Under the “engines" value in the object, set the value for the “node" key equal to “0.8.x". It’s important to note, this is what worked for me. I’m by no means a Node pro, but changing the version number from 0.8.18 to 0.8.x made the Nodejitsu deploy script quit spitting out errors. Once this is set, save and close this file. Next, we’ll setup a Nodejitsu account and get a deploy going.
First things first, head over to Nodejitsu and signup for an account. For this demo, I just setup an account under the “free" plan. If you’re hosting an open source project, Nodejitsu will give you hosting for free. Check out this link if that’s you. If not, setup a free account and then we’ll keep moving forward.
Got an account? Great. All we need to do now is install the Nodejitsu command line utility:
Cool. Now, let’s login to our Nodejitsu account and deploy our first app:
You’ll be prompted for your Nodejitsu account info. Follow the prompts until you see:
info: Authenticated as <username> info: Nodejitsu ok
Nice work! Now we’re logged into Nodejitsu and can get a deploy going. Let’s make sure we’re in the .demeteorized directory we created early (if you’re not already), and the run a deploy:
cd .demeteorized && jitsu deploy
So, the next thing that happens is you’ll be prompted for some info from Nodejitsu. The first thing you’ll be asked for is a subdomain prefix where your app will live on Nodejitsu. This is just a plain string, something like “exampleapp" (no quotes). Make this whatever you’d like your URL to be (the final URL will be http://<subdomain>.jit.su).
Next is setting up the “start script." This is a Node thing. We need to tell Node a bunch of stuff to get started up and this script is what does the work for us. What this prompt is asking for is a path to the directory for the script. Just type server/server.js (after the prompt) like so:
prompt: scripts.start: (node server) server/server.js
Blammo! Everything is setup and Nodejitsu we’ll start to deploy. Almost there.
Now this will run some dohickery and then we’ll get an error. Crap! This is an easy fix, though. The bug is that the app is looking for a MONGO_URL (our database URL) and can’t find one. If you’ve ever deployed your app to meteor.com before, this may seem strange because they automatically setup a db for us. This time, we need to do it on our own.
While a detailed walkthrough is outside of the scope of this tutorial, I recommend trying out MongoHQ. They offer a free sandbox database that you can use to your heart’s content, and set up is really simple. Head over to MongoHQ quick and setup an account. Once you’ve done that, create your first database (hint: you’ll pick out the “Sandbox" option under the Elastic Hosting (AWS) subheading). With a database in hand, you’ll want to add a user to it so we can access it easily.
To add a user, tap on the “Admin" tab on the left-hand side of the screen and then the Users tab. Add in a new user and remember the information (we’ll need this when we wire up the MONGO_URL next). Got a user? Write down the info? Nice, let’s get this working.
Setting up Environment Variables on Nodejitsu
Let’s head over to the Nodejitsu website and login. At this point, we’ll see the app we just tried to deploy with the name we provided in the prompt above. Here, you’ll see three tabs. Click on the “Environment variables" tab. Here, we need to add some URLs. Make sure to leave the ones that Nodejitsu added by default intact.
Click on the “Add variable" button (you’ll repeat this for each key/value pair below) and add each environment variable listed below:
NOTE: These will be different for you, so make sure to customize with your own information.
Got them added? Great. Now we can try our deploy again and get a working copy of our app running.
Deploying to Nodejitsu…Again
Now that we’ve got these set, we can actually just try starting our app from the Nodejitsu web interface. On the same screen, look for the group of buttons to the right of your app name and click on the one labeled “start". Once you’ve done this, give Nodejitsu a few seconds to boot everything up. Once you see the badge to the right of your name turn green and display the text “started," go to your Nodejitsu URL. If everything went as planned, you should see a working copy of your app! Awesome!
We covered a lot, so let’s review what we accomplished:
Installed Demeteorizer and demeteorized our app to get it Node-proof for Nodejitsu.
Created a Nodejitsu account, logged in, and attempted a deploy.
Ran into an error with our database, created a database on MongoHQ.
Setup Environment variables on Nodejitsu to tell our app which resources to use.
Turned our server on and got a running app.
Pretty cool stuff. Now, we can run our app on Nodejitsu which means we get the benefit of using web sockets and sticky sessions. If you’re curious about scaling and how this works, I recommend checking out this tutorial over on Meteor Hacks.
Did this work for you? Shoot me an email with your app URL after you’ve got it working and I’ll add it to this post as an example.
If this was helpful, make sure to follow this blog on Tumblr and follow @properapp on Twitter for more Meteor resources.