Running Metabase locally as a service

May 29th, 2017 in  by Michael Cho

Want the power of Periscope or Tableau, without the big monthly bill? Setup Metabase to run locally on your CentOS server.

Metabase is a pretty awesome open source business intelligence / analytics framework that lets non-technical users query your database. It is modelled after similar enterprise software like Tableau and Periscope, except that by virtue of its 'open-sourcedness' (!!) you are free to install and use it on your own servers without the hefty fees.

This article lists the steps I went through to get this installed on my CentOS server, and then running it as a service accessible through the browser at a subdomain like data.mydomain.com.

Install Java

Firstly, you will need to have Java installed on your server. The quickest way to check if it is already installed is to run java -version. The current version of Metabase (v0.23.1 as at the time of writing) runs fine with my v1.8.X of Java, you may need to upgrade if your Java version is older.


$: wget --no-cookies --no-check-certificate --header "Cookie:oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-linux-x64.rpm"

$: yum localinstall jdk-8u121-linux-x64.rpm

Download Metabase

This is straightforward:


$: wget http://downloads.metabase.com/v0.23.1/metabase.jar
$: mv metabase.jar /path/to/my/folder

Run Metabase as a service

I like to keep my applications running as services, so that I can start and stop them with simple commands like service metabase start and service metabase stop.

To do so, save this file as /etc/init.d/metabase:


#!/bin/sh
#
# metabase - this script starts and stops the metabase daemon. It should be kept in /etc/init.d/metabase
#
# chkconfig:   - 85 15
# description:  Metabase is the easy, open source way for everyone in your company to ask questions and learn from data.
# processname: metabase


# Source function library.
. /etc/rc.d/init.d/functions

SERVICE_NAME=Metabase
METABASE_DIR=/var/www/metabase
METABASE_JAR=$METABASE_DIR/metabase.jar
METABASE_PORT=6543
METABASE_DB=$METABASE_DIR/metabase2.db
METABASE_PID=$METABASE_DIR/metabase-pid

JAVA_PATH=/usr/bin/java


start() {
    echo -n $"Starting $SERVICE_NAME ... "
    if [ ! -f $METABASE_PID ]; then
        MB_JETTY_PORT=$METABASE_PORT MB_DB_FILE=$METABASE_DB nohup $JAVA_PATH -jar $METABASE_JAR >> $METABASE_DIR/service.out 2>&1 &
        echo $! > $METABASE_PID
        echo "Started $SERVICE_NAME."
    else
        echo "$SERVICE_NAME already running."
    fi
}

stop() {
    if [ -f $METABASE_PID ]; then
        echo $"Stopping $SERVICE_NAME ... "
        PID=$(cat $METABASE_PID);
        kill $PID;
        echo "Stopped $SERVICE_NAME."
        rm $METABASE_PID
    else
        echo "$SERVICE_NAME is not running."
    fi
}

rh_status() {
    status -p $METABASE_PID $JAVA_PATH
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status}"
        exit 2
esac

You can edit the few variables at the start of this init script. 

What this file does is:

  • runs Metabase (METABASE_JAR) locally on port 6543 (METABASE_PORT)
  • keeps all files at the METABASE_DIR, such as the H2 database file, PID file, and the output of the service (service.out)
  • uses the java executable in JAVA_PATH
  • allows the user run 3 possible commands (service metabase start, service metabase stop, and service metabase status)

Make sure you set the correct permissions on this file, and also add this to chkconfig to run on server boot.

Make this web-accessible

I wanted to access Metabase from my browser at data.mydomain.com, so I added the following Nginx config file to my server. This assumes you already have Nginx setup and configured.


server {
    listen      80;
    server_name data.mydomain.com;
    rewrite     ^   https://$server_name$request_uri? permanent;
}

server {
    listen 443;

    ssl        on;
    ssl_certificate         /var/www/metabase/data.mydomain.com.pem;
    ssl_certificate_key     /var/www/metabase/data.mydomain.com.key;

    server_name data.mydomain.com;
    charset     utf-8;
    client_max_body_size 75M;
    gzip_static on;

    access_log /var/www/metabase/access.log main;
    error_log /var/www/metabase/error.log debug;

    expires epoch;
    add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0";
    add_header Access-Control-Allow-Origin *;

    location / {
        proxy_pass          http://localhost:6543;
        proxy_set_header    Host             $host;
        proxy_set_header    X-Real-IP        $remote_addr;
        proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header    X-Client-Verify  SUCCESS;
        proxy_set_header    X-Client-DN      $ssl_client_s_dn;
        proxy_set_header    X-SSL-Subject    $ssl_client_s_dn;
        proxy_set_header    X-SSL-Issuer     $ssl_client_i_dn;
        proxy_read_timeout 1800;
        proxy_connect_timeout 1800;
    }

}

This configuration assumes that you already have SSL pem/key pairs setup and you prefer to access using https (not http). It also includes access and error logs to your METABASE_DIR.

(Of course you should modify this Nginx config file to match your METABASE_DIR and METABASE_PORT from earlier.)

Don't forget to add an A record to your DNS provider for data.mydomain.com.

That's all that needs to be done, you should now see Metabase at data.mydomain.com and you are ready to configure your database etc.


Other articles you may like

Prioritized Code Review Checklist - what to look for first, second, and last
Sep 21st, 2017
Principles for Code Reviews
Sep 2nd, 2017
Create a custom Alexa Skill with AWS Lambda - Part 3 (Lambda)
Dec 18th, 2016
Create a custom Alexa Skill with AWS Lambda - Part 2 (Alexa Skill)
Nov 10th, 2016
Create a custom Alexa Skill with AWS Lambda - Part 1 (Overview)
Nov 1st, 2016