Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phund/7372628980516376dfe5eaf8a559428d to your computer and use it in GitHub Desktop.
Save phund/7372628980516376dfe5eaf8a559428d to your computer and use it in GitHub Desktop.
Deploy meteor app with nginx and passenger

How to deploy meteor app with nginx and passenger (current test with meteor 1.4.1 and ubuntu server 14.04)

Step 1: Setup server enviroment

Install nginx and passenger

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates

--- Add our APT repository

sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update

--- Install Passenger + Nginx

sudo apt-get install -y nginx-extras passenger

--- Check install

sudo /usr/bin/passenger-config validate-install

Install node js

Because meteor 1.4.1 is using node v4.5.0 and npm 3.10.6, server need to installed version exactly to work property

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

sudo npm cache clean -f
sudo npm install -g n
sudo n 4.5.0

sudo npm -g install npm@latest

with meteor 1.2.1 using node 0.10.40 and npm 1.4.28

// node 0.10.40 persisted any more on nodesource
sudo curl -sL https://deb.nodesource.com/setup_0.10 | sudo -E bash -
sudo apt-get install -y nodejs

--check

npm -v
node -v

Install node mongodb

Please see https://docs.mongodb.com/manual/installation/ for more detail

Step 2: Config nginx and passenger

--- Enable config passenger in nginx

sudo nano /etc/nginx/nginx.conf
-> Enable passenger config
include /etc/nginx/passenger.conf;

--- Check and config passenger

sudo nano /etc/nginx/passenger.conf

-- It should be:

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/passenger_free_ruby;
passenger_nodejs /usr/local/bin/node;

Step 3: Configure nginx to run meteor app

sudo nano /etc/nginx/sites-enabled/[app_name].conf

---Edit

  server {
    listen 80;
    server_name <domain_name or public_ip>;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root <path_to_app>/bundle/public;

    # Turn on Passenger
    passenger_enabled on;
    # Tell Passenger that your app is a Meteor app
    passenger_app_type node;
    passenger_startup_file main.js;

    # Tell your app where MongoDB is
    passenger_env_var MONGO_URL mongodb://localhost:27017/<app_name>;
    # Tell your app what its root URL is
    passenger_env_var ROOT_URL http://<domain_name or public_ip>;
    passenger_env_var METEOR_SETTINGS '{"private": {"key": "ABCDF"}}';
    passenger_app_env production;

  }

Check config and restart

sudo nginx -t
sudo service nginx restart

Note: <domain_name or public_ip> is doamin or public ip of your app, app_name is your app name, path_to_app is path to your app (that is in next step)

Step 4: Build app and deploy

In development app folder Create a bundle first and upload it to your production server:

ROOT_URL=http://<domain_name or public_ip> MONGO_URL=mongodb://localhost:27017/<app_name> meteor build ../build_result/tmp --architecture os.linux.x86_64 --server http://<domain_name or public_ip> --mobile-settings settings.json

# This will need to create a directory <path_to_app> ex: /webapps/example
scp <app_name>.tar.gz someserver:<path_to_app>

On your production server, extract the bundle and install NPM modules:

cd <path_to_app>
tar xzvf <app_name>.tar.gz
cd bundle/programs/server && npm install --production
cd <path_to_app>/bundle
mkdir -p tmp public
cd <path_to_app> && bundle/tmp/restart.txt

Restart nginx to sure all thing work

Check log app

tail -f /var/log/nginx/error.log

Step 5: Create a auto deploy script

In development folder create a deploy.sh

touch deploy.sh
chmod +x deploy.sh

Edit content of deploy.sh (example with app_name abc and remote server to amazon

 #!/bin/bash

NAME=abc
USER=ubuntu
TARGZ_NAME=$NAME.tar.gz
DIR=/home/ubuntu/$NAME

if [ -z "$1" ]; then
  echo 'Usage: deploy HOSTNAME'
  exit 0
else
  HOSTNAME=$1
fi

URL=http://$HOSTNAME
MONGO_URL=mongodb://localhost:27017/$NAME
REMOTE=$USER@$HOSTNAME
KEYFILE=~/.ssh/abc.pem

if [[ "$KEYFILE" != "" ]]; then
  KEYARG="-i $KEYFILE"
else
  KEYARG=
fi

ROOT_URL=$URL MONGO_URL=$MONGO_URL meteor build ../build_result/tmp --architecture os.linux.x86_64 --server $URL --mobile-settings settings.json

ssh $KEYARG $REMOTE "mkdir -p $DIR"

scp $KEYARG ../build_result/tmp/$TARGZ_NAME $REMOTE:$DIR

CMD="
cd $DIR && tar xfz $TARGZ_NAME
cd $DIR/bundle/programs/server && npm install --production
mkdir -p $DIR/bundle/tmp $DIR/bundle/public
cd $DIR && touch bundle/tmp/restart.txt
"

ssh $KEYARG $REMOTE "$CMD"

run "./deploy.sh <host_name>" to deploy ex ./deploy.sh 123.23.14.5

Step 6: Deploy with capistrano

COMING SOON....

Good luck!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment