ServerInstall

= Installation guide =

This guide assumes that you are running recent Ubuntu (12.04).

These instructions describe following setup
 * nginx front end web server
 * Jetty application server
 * PostgreSQL database

Setup
* port 80 * port 81 * port 8000
 * haproxy (loadbalancer)
 * nginx (webserver)
 * aleph/netty (ruuvitracker server)

haproxy is needed because nginx doesn't support yet WebSockets. Haproxy routes requests for WebSocket URL directly to ruuvitracker server. Everything else goes through nginx.

Fedora 17
Brief instructions for Fedora.

Packages
yum install java-1.7.0-openjdk.x86_64 java-1.7.0-openjdk-devel.x86_64 yum install emacs vim git yum install tomcat nginx clojure postgresql postgis postgresql-server yum install system-config-firewall yum install mlocate yum install haproxy

Install leiningen http://leiningen.org/

su - postgres -c "PGDATA=/var/lib/pgsql/data initdb"

service nginx start systemctl enable nginx.service

service tomcat start systemctl enable tomcat.service

service postgresql start systemctl enable postgresql.service

Configure firewall system-config-firewall-tui

= Ubuntu =

Install basic utils
sudo apt-get update sudo apt-get install emacs vim git

Install JDK
sudo apt-get install openjdk-7-jdk openjdk-7-source visualvm

Install clojure
sudo apt-get install clojure1.4 leiningen

Install PostGRES database
sudo apt-get install postgresql-9.1 postgis

Setup user for running RuuviTracker (ruuviserver)
sudo adduser ruuviserver

Setup database
sudo -u postgres createuser -S -R -D -l -P ruuvi
 * 1) set password to ruuvi

sudo -u postgres createdb -O ruuvi ruuvi_server

Install frontend webserver and Java Servlet container server
sudo apt-get install nginx jetty liblogback-java haproxy

Configure loadbalancer
global # to have these messages end up in /var/log/haproxy.log you will # need to: #   # 1) configure syslog to accept network log events.  This is done    #    by adding the '-r' option to the SYSLOGD_OPTIONS in    #    /etc/sysconfig/syslog    #    # 2) configure local2 events to go to the /var/log/haproxy.log #  file. A line like the following can be added to   #   /etc/sysconfig/syslog #   #    local2.*                       /var/log/haproxy.log #   log         127.0.0.1 local2

chroot     /var/lib/haproxy pidfile    /var/run/haproxy.pid maxconn    4000 user       haproxy group      haproxy daemon

# turn on stats unix socket stats socket /var/lib/haproxy/stats defaults mode                   http log                    global option                 httplog option                 dontlognull option http-server-close option forwardfor      except 127.0.0.0/8 option                 redispatch retries                3 timeout http-request   10s timeout queue          1m timeout connect        10s timeout client         1m timeout server         1m timeout http-keep-alive 10s timeout check          10s maxconn                3000 frontend public bind :80 use_backend ruuvi_ws if { path_beg /api/v1-dev/websocket } default_backend nginx
 * 1) see http://www.exratione.com/2012/12/websockets-over-ssl-haproxy-nodejs-nginx/

backend ruuvi_ws balance leastconn server ws1 127.0.0.1:8000

backend nginx balance leastconn server nginx1 127.0.0.1:81

frontend stats bind 127.0.0.1:2000 default_backend stats

backend stats stats enable stats realm Haproxy\ Statistics stats uri / stats auth admin:password

Configure frontend webserver
Create file server { listen  80; ## listen for ipv4; this line is default and implied listen  [::]:80 default ipv6only=on; ## listen for ipv6

root /usr/share/nginx/www; index index.html index.htm;

# Make site accessible from http://localhost/ server_name localhost;

# path / is mapped to html url location / { # First attempt to serve request as file, then # as directory, then fall back to index.html try_files $uri $uri/ /index.html; }

# API is found under /api path location /api { proxy_pass http://127.0.0.1:3000; proxy_redirect http://127.0.0.1:3000 http://dev-server.ruuvitracker.fi/api; } }

Link ruuviserver configuration

cd /etc/nginx/sites-enabled rm default ln -s /etc/nginx/sites-available/ruuvitracker

Restart nginx /etc/init.d/nginx restart

Configure Application server (jetty)
Edit NO_START=0 JETTY_HOST=localhost JETTY_PORT=3000 JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
 * 1) allow connections only from localhost to port 3000
 * 1) Point to your favorite JDK

cd /usr/share/jetty/lib sudo ln -s ../../java/logback-classic.jar sudo ln -s ../../java/logback-core.jar sudo /etc/init.d/jetty restart

Install RuuviTracker server
sudo -i -u ruuviserver bash git clone https://github.com/RuuviTracker/ruuvitracker_server.git

cd ruuvitracker_server lein deps

Check that all tests pass
lein midje

Should say something like this:

> All claimed facts (44) have been confirmed.

Create ruuvi tracker database
Create tables: lein run migrate forward

Create initial test data: lein run load-example-data

Testing
Check that server starts. lein run server

Point browser to
 * http://domain.example.com:8080
 * http://domain.example.com:8080/api/v1-dev/trackers

Build and install Web Application Archive
Building final package that will be installed to application server.

lein ring uberwar

sudo cp ruuvi-server-0.0.1-SNAPSHOT-standalone.war /usr/share/jetty/webapps/ruuvi.war sudo /etc/init.d/jetty restart

Check install
Use browser to go to url http://domain.example.com/ruuvi/api

Setup robots.txt
/usr/share/nginx/www/robots.txt User-agent: * Disallow: /api/

Enable service on boot
chkconfig nginx on chkconfig haproxy on chkconfig postgresql on