Deploying a Meteor app with Nginx from scratch

Tom Goldenberg
May 26, 2016 · 6 min read

Just like ways of skinning a cat (whatever that is supposed to mean), there are many ways to deploy a Meteor application. One way is to bundle the Meteor application so that it can be run by Node. The problem with this is that it isn’t wise to run an application like Meteor through your public port (which is 80). So it is considered best practice to use a cross-proxy and redirect public requests with Nginx or another service. In this blog post, we’ll look at how to set up the configuration and deploy a Meteor app with Nginx. There are other resources online for this, but many of them are outdated, so I feel that it will be useful.

  1. Log into remote server and create a user

First you will want to ssh into your virtual machine (Digital Ocean, Azure, etc.). If you are new to logging in to remote servers, this could be a handy review. The command looks something like ssh root@IP_ADDRESS.

From here, you will want to add a user, since running commands as root is usually not recommended. To do this, you can type adduser USERNAME. Then to add sudo privileges to the user, you can type visudo and make the following changes:

under the line root ALL=(ALL:ALL) ALL add

Once you establish the username and password, you will be able to sign in as the new user and make changes with root privileges.

2. Download the latest Meteor distribution

This is a simple curl command, as explained on the official Meteor site:

This will download Meteor to your machine. You can test that it works by calling meteor — version and seeing that it gives a response

Image for post
Image for post

3. Install Node

Currently Meteor only supports older versions of Node. They are working on this in the Meteor roadmap. For now, we will use Node v10.04. Here are the commands:

You can then make sure you have global access to npm and node

Image for post
Image for post

4. Install Forever

We use forever to run our Meteor instance as a continuous program in the background.

If you run forever list, you then should see something like this

Image for post
Image for post

5. Install Git and Clone Repository

We need to install git to clone our Meteor repository.

6. Check that the app runs locally

cd into your app directory and run it locally — npm install and then meteor run

Image for post
Image for post

6. Install Nginx

We use Nginx to route incoming requests to port 80 to our Meteor port (for us, 8080). This is the complex part of deployment that can be tricky at first. We’ll try to make this as easy as possible through the instructions.

As per the nginx docs, we need to first create a signing key for nginx. You can download the signing key from the above link to your computer. It should look something like this:

Image for post
Image for post

In the home directory of your remote server, you’ll want to create a file nginx_signing.key and copy the contents to it. To verify the signing key, type sudo apt-key add nginx_signing.key. You should see an OK response.

Image for post
Image for post

Next you’ll need the “codename” for your virtual server. If you are using Ubuntu 14.04, it is “trusty”. A list of different codenames can be found here.

Using the codename, we open up the file /etc/apt/sources.list and append these 2 lines to the bottom with the proper codename

Finally, we can install the nginx package

Image for post
Image for post

7. Configure Nginx to process incoming requests

Next we’ll need to add our nginx file that processes requests on the publicly-accessible port 80. We can go to the directory /etc/nginx where all the work will happen. If you check the available files (ls), you should see something like this:

Image for post
Image for post

We need to create 2 directories: “sites-available” and “sites-enabled”. We will then create a file “sites-available/app” and edit it with our server configuration:

Save this file and then create a symbolic link to a file with the same name in “sites/enabled”.

Check that the symbolic link worked — cat /etc/nginx/sites-enabled/app. You should see the same code from above.

Last, add this to the bottom of the /etc/nginx/nginx.conf file to include your changes:

Great! Now nginx is configured to pass all requests to our server to our Meteor instance on port 8080. Now we just have to boot up the Meteor app and run it in the background.

8. Run the Meteor app on port 8080 through Forever

First start nginx

Later, we can use the command sudo service nginx restart to reload the configuration. If you go to your server IP address in your browser, you should see something like this:

Image for post
Image for post

Next we will want a script to load our environment variables and build the Meteor bundle. Assuming we have our environment variables (MONGO_URL, ROOT_URL, etc.) in a file “env_settings.sh” one directory up, here is one we can use:

If we go to the address of our IP, our app should be running!

I hope this blog post was helpful. There is a lot more you can do with Nginx, but this is a very common use-case. Please press the like button if you enjoyed the article, and follow me @tomgoldenberg on Twitter.

Startup Founder Panel

We interview startup founders and discuss lessons learned

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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