Skip to content

Notes: Setting up swag with xapi proxying in nginx

Brad Smith edited this page Jan 7, 2015 · 9 revisions

(untested!) notes on how to set up django+swag+nginx, until I can get it all automated with Ansible.

References:

django+swag code and dependencies

cd /usr/local/tunapanda/data/
sudo pip install virtualenv
virtualenv swag
cd swag
source bin/activate
git clone --recursive https://github.com/usernamenumber/swagmaps src
pip install -r src/requirements.txt
deactivate

Note: This configuration will still produce javascript errors until this pull request is merged. As a temporary workaround, edit src/swag/settings.py and set XAPI_URL to a full URL like http://your_server/xapi.

DB setup and storage for static files

This assumes you are still in /usr/local/tunapanda/data/swag

mkdir -p ../nginx/static/ 
echo "STATIC_ROOT = '/usr/local/tunapanda/data/nginx/static'" >> src/swag_devsite/settings.py
src/manage.py syncdb --noinput
src/manage.py collectstatic -l --noinput

/usr/local/tunapanda/data/swag/gunicorn_start.sh

#!/bin/bash

# From http://goodcode.io/blog/django-nginx-gunicorn/
set -e
PORT=8003
LOGFILE=/var/log/gunicorn/hello.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=3
# user/group to run as
USER="www-data"
GROUP="www-data"
cd $(dirname $0)/src
source ../bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
exec ../bin/gunicorn swag_devsite.wsgi:application -b 127.0.0.1:$PORT -w $NUM_WORKERS
--user=$USER --group=$GROUP --log-level=debug
--log-file=$LOGFILE 0.0.0.0:${PORT} 2>>$LOGFILE

/etc/nginx/sites-enabled/default:

upstream swag {
    server 127.0.0.1:8003;
}

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;

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://swag ;
    }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }

    location /xapi/ {
        proxy_pass http://staging.tunapanda.org/learninglocker/public/data/xAPI/;
    }

    location /static/ {
        root /usr/local/tunapanda/data/nginx/;
    }
}

Start everything up!

Note that sudo is used, so gunicorn/django run as root, which means authentication works! This is still maybe not best long-term, but for now it's something! Obviously we'll eventually create a proper init script or something for this.

sudo /usr/local/tunapanda/data/swag/gunicorn_start &
sudo service nginx restart
Open site's IP in a browser