So… you’ve built a gorgeous Meteor application! You’ve artistically structured a gorgeous codebase that comes together to present a performant, delicious application that your consumers will love. What’s the last piece of the puzzle? Deployment.
Where to deploy?
Heroku? Rackspace? Linode? DigitalOcean? There are plenty of options when it comes to hosting your application. I’m a pretty big Heroku fan, and there’s a really nice Heroku build pack for Meteor, courtesy of jordansissel. I’m not going to talk about that in this blog post, as it’s very straightforward. Instead, I’m going to talk about how to deploy your Meteor application to a Linux distro.
Running Meteor on a Linux distro
The first thing you should know about deploying Meteor is, it’s not the same as running
meteor within your app root to develop your application. Once you’re ready to deploy your application, you can use Meteor’s build system to generate a bundle that can be sent to a server, unpacked, and run.
Installing the tools
You need to install the following things to run a Meteor app bundle on a Linux distro:
- A box running Linux 🙂
- g++ make
- A web server (like Apache or Nginx)
For debian-based Linux systems, you can install MonboDB by running:
apt-get install mongodb-server
For other linux distros that are not debian-based, see the MongoDB installation documentation..
Now, you don’t want this MongoDB instance to be available to external hosts. You can use
netstat to make sure MongoDB is only serving the local host, look for ports 27017 and 28017.
Run the following commands to install NodeJS on debian-based Linux systems:
add-apt-repository ppa:chris-lea/node.js apt-get update apt-get install nodejs
It’s generally safe to assume that Meteor is going to require a more recent version of NodeJS than what is available in aptitude’s repositories. This is why I suggest using the
Meteor depends upon some npm modules that require g++ make tools to run correctly, so you need to install it on your server:
apt-get install g++ make
In the past I have used Nginx to serve my Meteor applications. Apache will also work well. All you really need to do is set up a proxy pass to the port that Meteor is running on. There is a really good document with an example Nginx available on Meteorpedia.com. I’d encourage you to read through that and use the example code if you’re not familiar with Nginx.
Bundling and running Meteor
Step 1: Create the bundle
Start off by navigating to the root of your application in a terminal window, and run this command:
meteor build /path/to/output/directory
This will generate a
tar.gz file (bundle) in the directory that you specified.
Step 2: Unpack the bundle
Next, copy the bundle up to your newly-configured Linux server.
scp /path/to/output/appname.tar.gz firstname.lastname@example.org:/path/to/upload/location
Once this has been uploaded to your server, ssh into your server and unpack the bundle:
ssh email@example.com cd /path/to/upload/location tar -zxf appname.tar.gz
Step 3: Install Meteor’s dependencies
You’ll need to tell npm to install all the modules Meteor needs to run properly:
cd /path/to/unpacked/bundle/programs/server npm install
Step 4: Run Meteor
While you are still inside of the
server directory in the bundle you unpacked, you can crank up your Meteor applications:
PORT=3000 MONGO_URL=mongodb://localhost:27017/myappname node main.js
Of course, some packages you use may require that you pass in more environment variables (like
MAIL_URL), so make sure you enclude all the environment variables your application needs.
Once you have started Meteor, you should be able to reach it by pointing a browser at your server. Party time!
Once you have your Meteor application running, you may want to take some extra steps to make sure that your application keeps running.
Here’s some things you may want to set up:
- Upstart: You can set this up so that your Meteor application is automatically run on system boot. You can even configure it to restart Meteor if something goes wrong.
- forever.js: A (simpler) alternative to upstart.
- MongoDB Client Access Control: This will require all users connecting to MongoDB to identify themselves.
I’ve been working on some toolsets that will make the process of building and deploying a Meteor application easier. This will most likely be in the form of a Vagrant playbook. So, stay tuned 🙂
This post was originally published on Patrick’s personal blog.
Making the web a better place to teach, learn, and advocate starts here...
When you subscribe to our newsletter!