Skip to content

Instantly share code, notes, and snippets.

@mauritslamers
Last active January 5, 2017 11:32
Show Gist options
  • Save mauritslamers/bb1e88ac692487a727ba39c22254b8a0 to your computer and use it in GitHub Desktop.
Save mauritslamers/bb1e88ac692487a727ba39c22254b8a0 to your computer and use it in GitHub Desktop.
install telescope guide on lxd

Deploying a Telescope Nova Application in LXD / Ubuntu 16.04

This is a guide how to install the Telescope Nova Application as a deployed app without using MUP. My own motivation is that I want to fit the deployed app into an existing server infrastructure. While LXD is compatible with Docker, I just wanted to use plain LXD.

As Telescope is not considered a full app, but rather as a framework you can use to build your own app, you need to first develop your own app locally. It is assumed you have a deployable application. It is also assumed you already installed Nginx as the main web server.

You can also use this guide to install the app without using LXD. In that case, just skip the LXD parts.

This guide is loosely based on https://www.digitalocean.com/community/tutorials/how-to-deploy-a-meteor-js-application-on-ubuntu-14-04-with-nginx

Create a build of your application

On your development machine, run the following:

meteor build ..

This will cause a [yourprojectname].tar.gz file to be created in the folder containing your project folder. There are many ways how you can transfer this file to your server, but this guide assumes that you are user admin and you managed to upload the file as /home/admin/telescope.tar.gz

Setting up LXD

First you need to install and setup LXD.

sudo apt install lxd lxd-tools lxd-client

During the installation you are asked to install a network bridge. You can avoid that, but installing it is going to make many things easier. Choose NAT as bridge type. After lxd is done installing, create a new machine:

sudo lxc launch ubuntu:16.04 telescope

Then login into the machine:

sudo lxc exec telescope -- /bin/bash

Installing dependencies

We need to install the dependencies, such as NodeJS and MongoDB.

NodeJS

There are quite a few ways of installing a recent NodeJS, so it is best to go to https://nodejs.org/en/download/ and decide whether you want to install through the package manager (apt) or manually. The easiest is through the package manager, which requires you to do the following:

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

MongoDB

Now install mongodb:

sudo apt install mongodb

Make sure MongoDB only runs on localhost:

netstat -ln | grep -E '27017|28017'

The output should show something like:

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:28017         0.0.0.0:*               LISTEN
unix  2      [ ACC ]     STREAM     LISTENING     6091441  /tmp/mongodb-27017.sock

To be safe, we also install the daily backup script by creating the file /etc/cron.daily/mongodb-backup with contents:

@daily root mkdir -p /var/backups/mongodb; mongodump --db telescope --out /var/backups/mongodb/$(date +'\%Y-\%m-\%d')

Forever and Forever-service

It is useful to be able to use a service in order to start the application as soon as the server (or LXD machine) needs to be restarted. This guide suggests forever and forever-service.

npm install -g forever forever-service

Create a dedicated user

It is good practice to run applications as a normal user if possible, so first create a user:

adduser --disabled-login telescope

The output should show something like:

Adding user `telescope' ...
Adding new group `telescope' (1001) ...
Adding new user `telescope' (1001) with group `telescope' ...
Creating home directory `/home/telescope' ...
Copying files from `/etc/skel' ...
Changing the user information for telescope
Enter the new value, or press ENTER for the default
        Full Name []: 
        Room Number []: 
        Work Phone []: 
        Home Phone []: 
        Other []: 
Is the information correct? [Y/n]

Nginx setup

We need to create a nginx configuration file in order to proxy the external requests for the Telescope app to the Telescope server. Before creating this configuration file you need to decide on what internal port the Telescope server is going to run. Port number 3000 is a safe bet, but is also frequently used by other application servers. You can check whether port 3000 is still available by running:

lsof | grep "TCP" | grep 3000

If this does not return any lines, 3000 is available for use. By changing the last number you can see whether a different port you have in mind is available. Note that you cannot choose ports lower than 1000 as they are reserved for the system.

This guide assumes you want to host the app at https://telescope.mydomain.ext and that you want the http traffic to be redirected to https.

LXD

You are going to need the ip address of your lxd host in order to forward the traffic correctly. You can achieve this inside the host by running sudo ifconfig and take the address from eth0. From the main machine you can do lxc list, and it will show a table of hosts with the ip addresses.

Creating the Nginx configuration file

cd /etc/nginx/sites-available
nano -w telescope

Copy the contents below into the editor:

map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

server {
  listen 80;
  server_name telescope.mydomain.ext;
  return 301 https://telescope.mydomain.ext$request_uri;
}

server {
  listen 443 ssl;
  server_name telescope.mydomain.net;
  index index.html index.htm;
  
  ssl_certificate /path/to/certificate;
  ssl_certificate_key /path/to/certificate.key;
  
  location / {
    ## For LXD replace ip.address with the ip of the host, otherwise 127.0.0.1
    proxy_pass http://ip.address:port;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header Host $host;
  }
}

Change the line with proxy_pass to contain your hosts ip address and the port you have chosen.

Save the file (Ctrl+O, Return) and run nginx -t to make sure there are no syntax errors. As soon as nginx reports no problems, you can load the new configuration through sudo service nginx reload.

Installing the app

Go into the new users home directory and unpack the meteor build.

cp /home/admin/telescope.tar.gz /home/telescope
cd /home/telescope
sudo su - telescope
tar -xvzf telescope.tar.gz

This will unpack the bundle into a directory called bundle. As the README file from the bundle tells us, we first need to do a little setup:

cd bundle/programs/server
npm install

We now go back to the bundle folder and create the service:

cd /home/telescope/bundle
sudo forever-service install -r telescope -s main.js -e "MONGO_URL=mongodb://localhost:27017/telescope ROOT_URL=https://my.host.ext PORT=port" telescope

in which ROOT_URL is the base URL of the Telescope project, and PORT is the port you have chosen earlier. The last name telescope is the name of the service. If you need to remove the service for some reason, use:

sudo forever delete telescope.

For more information about the use of forever-service, please check https://github.com/zapty/forever-service.

We can now start the app:

service telescope start

It can take some time (10-30 seconds) before the app will become available. You can follow the progress by using

tail -f /var/log/telescope.log

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