Skip to content

Instantly share code, notes, and snippets.

@guidouil
Last active November 16, 2017 20:52
Show Gist options
  • Save guidouil/ff842e3857569c8fbe9dfd74b811e582 to your computer and use it in GitHub Desktop.
Save guidouil/ff842e3857569c8fbe9dfd74b811e582 to your computer and use it in GitHub Desktop.

Deploy a Meteor app on a dedicated linux server

Requirement

You need, Meteor, MongoDB and Node.js installed on the machine.

And it also requires the forever npm package to be globaly installed.

Deploy script to copy/past in shell ⚠️ NOT AS ROOT

Be sure to replace the CAPITALIZED words with you own and to adapt the meteor npm install ... to your dependencies from your package.json file

cd
rm -rf APPNAME-source
rm -rf builds
git clone https://LOGIN:PASSWORD@bitbucket.org/USERNAME/APPNAME.git APPNAME-source
cd APPNAME-source
meteor npm install --save autoprefixer bcrypt jquery meteor-node-stubs mobile-detect moment twix
meteor build ../builds/. --server-only
cd ../builds/
tar xzf APPNAME-source.tar.gz
cd
forever stop APPNAME
rm -rf APPNAME
cd builds
mv bundle ../APPNAME
cd ../APPNAME/programs/server/
npm install
cd
export MONGO_URL='mongodb://LOGIN:PASSWORD@127.0.0.1:27017/APPNAME'
export PORT=3000
export ROOT_URL='https://APPNAME.com'
export MAIL_URL='smtp://LOGIN:PASSWORD@smtp.mailgun.org:587'
export METEOR_SETTINGS=$(cat ~/APPNAME-source/private/settings_prod.json)
forever start --append --uid "APPNAME" APPNAME/main.js
date

NginX as a proxy to your Meteor App

Next you might want to configure NginX to be the proxy to your meteor app and also handle the public files serving. The following is an available-site-conf file you can inspire yourself of

server {
  listen 80;
  server_name APPNAME.com;
  return 301 https://$host$request_uri;
}

server {
  listen 443 ssl;
  server_name APPNAME.com

  ssl_certificate /etc/letsencrypt/live/APPNAME.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/APPNAME.com/privkey.pem;

  gzip on;

  ## serve static files by nginx instead of Meteor (the public/ folder)
  location ~ \.(jpg|jpeg|png|gif|mp3|ico|pdf) {
    root /home/USERNAME/APPNAME/programs/web.browser/app; # this should point at the content from the public folder
    access_log off;
    expires 30d;
    add_header Pragma public;
    add_header Cache-Control "public";
  }

  location = /favicon.ico {
    root /home/USERNAME/APPNAME/programs/web.browser/app;
    access_log off;
    expires 1w;
  }

  location ~* "^/[a-z0-9]{40}\.(css|js)$" {
    root /home/USERNAME/APPNAME/programs/web.browser;
    access_log off;
    expires max;
  }

  location ~ "^/packages" {
    root /home/USERNAME/APPNAME/programs/web.browser;
    access_log off;
  }

  location ~ /.well-known {
    root /var/www;
    allow all;
  }

  location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
  }
}

Also it enforce secure connection you can get for free with Let's Encrypt

Happy deploying 😎📲

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