Deploying Multiple Meteor Apps on One Ubuntu Server

Meteor makes everything easy. Except for deploying your app on your own server, since they want you to use their solution “Galaxy”. Setting up your own infrastructure can be daunting, especially when you want to host multiple meteor apps on one server. Here I’m going to describe how this can be done using nginx.

Step 1) Configuring nginx

After installing nginx

sudo apt-get update
sudo apt-get install nginx

you can use the following setup in your /etc/ngnix/nginx.conf :

# Change YOUR_TOKEN to your prerender token and uncomment that line if you want to cache urls and view crawl stats
# Change example.com (server_name) to your website url
# Change /path/to/your/root to the correct value

events {
  worker_connections 768;
  # multi_accept on;
}

http {

    upstream project1 {
      server localhost:8081;
    }

    upstream project2 {
      server localhost:8082;
    }

    server {
        listen 80;
        server_name project1.com project1.org;
        client_max_body_size 20M;

        location / {
          proxy_pass http://project1;
          proxy_read_timeout 1000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
        }
    }

    server {
        listen 80;
        server_name project2.com;
        client_max_body_size  20M;

        location / {
          proxy_pass http://project;
          proxy_read_timeout 1000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
        }
    }

}

meteordeploy

Step 2) Setting up a meteor project folder

In order to have only one deploy script (aka code duplication is bad), it’s useful to have all meteor apps in one folder. Therefore your directory structure should be somewhat like:

meteor-projects
--project1
--project2

Step 3) Setting up the server

You need to install a bit of stuff on your server in order for meteor to work:

install.sh

#!/bin/bash
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y build-essential

#install node
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install nodejs -y

#install npm modules
npm install -g forever
npm install -g phantomjs

#install mongo
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo service mongod start

#install meteor
curl https://install.meteor.com/ | sh

#utils
sudo apt-get install graphicsmagick

Put this somewhere on your server, then chmod +x install.sh and run it ./install.sh.

Step 4) Configuring properties

There are variables to your project, you can put them into a properties.sh file inside of each project:

#!/bin/bash
server=root@46.101.140.209
domain=project1.com
name=project1
port=8081

Note that I’ve set it up in a way, that name should be identical to your folder name.

Step 5) Writing a deploy script

Put those files into your meteor-projects folder:

meteor-projects/deploy.sh

#!/bin/bash
source ${1}/properties.sh

cd ${name}
meteor build compiled
ssh ${server} mkdir ${name}
scp -r compiled ${server}:${name}/compiled
rm -rf compiled
scp ../helper.sh ${server}:${name}/helper.sh
scp properties.sh ${server}:${name}/properties.sh
ssh ${server} chmod +x ${name}/helper.sh
ssh ${server} ${name}/helper.sh

meteor-projects/helper.sh

#!/bin/bash
cd $(dirname $0)
source properties.sh

tar -xvzf compiled/*.tar.gz
rm -rf compiled

# rebuild native packages
cd bundle/programs/server && npm install
cd ~

# setup environment variables
export MONGO_URL="mongodb://127.0.0.1:27017/${name}"
export ROOT_URL="http://${domain}"
export PORT=${port}

# start the server
forever stop ${name}/bundle/main.js
forever start ${name}/bundle/main.js

Things should be ready now!

In order to deploy, perform chmod +x deploy.sh and run ./deploy.sh project1 or ./deploy.sh project2. This should upload & run your meteor files.

Leave a Reply

Your email address will not be published.