Skip to content

Installation instructions

Rostyslav Bryzgunov edited this page Mar 25, 2018 · 1 revision

Let's assume you (me?) have already setup a host with public IP address (e.g. 1.2.3.4) and Ubuntu Server 16.04 on it:

ssh root@1.2.3.4

Install Z Shell

Remember: this is the best shell ever, you must have it!

sudo apt install zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
nano ~/.zshrc

Find plugins and add history-substring-search:

plugins=(
  git
  history-substring-search
)

Install dependencies

Specific to Ubuntu 16.04: install Python 3.6

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt update
sudo apt install python3.6
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
sudo update-alternatives --config python3
python3 -V # should be 3.6.x now

Install required project dependencies:

sudo apt update
sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx

Configure PostgreSQL:

sudo -u postgres psql
CREATE DATABASE myproject;
CREATE USER myprojectuser WITH PASSWORD 'mypassword';
ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myprojectuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;

Create service user developer:

sudo adduser developer
sudo su developer
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.zshrc
echo "export WORKON_HOME=~/var/www/sites/" >> ~/.zshrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.zshrc
mkdir -p ~/.config/pip
nano ~/.config/pip/pip.conf
[list]
format=columns

Then install Z Shell for this user too (see above).

Create sites directory:

sudo mkdir /var/www/sites/
sudo chown developer:developer /var/www/sites/

Create project

Switch to developer user and create the project.

sudo su developer
mkvirtualenv code.kottenator.com
cd /var/www/sites/code.kottenator.com
setvirtualenvproject

Now the next time you'll login and switch to developer user, you can do:

workon code.kottenator.com

This will cd you into /var/www/sites/code.kottenator.com and activate the virtualenv.

Install the project:

echo '-e git+https://github.com/kottenator/code.kottenator.com.git#egg=code.kottenator.com[prod]' > requirements.txt
pip install -r requirements.txt

Later, to update the source code, run:

pip install -Ur requirements.txt

Create a bunch of required project dirs:

mkdir etc
mkdir -p var/data
mkdir var/log
mkdir var/run
mkdir -p src/vault

Configure project

Create Django settings:

nano etc/settings.yaml
DEBUG: False

ALLOWED_HOSTS:
  - code.kottenator.com

STATIC_ROOT: var/data/static/

DATABASES:
  default:
    ENGINE: django.db.backends.postgresql_psycopg2
    NAME: myproject
    USER: myprojectuser
    PASSWORD: mypassword
    HOST: localhost

LOGGING:
  version: 1
  disable_existing_loggers: False
  formatters:
    verbose:
      format: '%(levelname)s %(asctime)s %(module)s %(process)d %(message)s'
  handlers:
    file:
      level: INFO
      class: logging.FileHandler
      filename: var/log/django.log
      formatter: verbose
  loggers:
    django:
      handlers:
        - file
      level: INFO
      propagate: True

Create uWSGI config:

nano etc/uwsgi.ini
[uwsgi]
module = project.wsgi:application
pidfile = var/run/uwsgi.pid
socket = var/run/uwsgi.sock
processes = 4
master = 1
daemonize = 1

Create Nginx config:

nano etc/nginx.conf
server {
  listen 80;
  server_name code.kottenator.com;
  location = /favicon.ico { access_log off; log_not_found off; }

  location /static/ {
    alias /var/www/sites/code.kottenator.com/var/data/static/;
  }

  location /vault/ {
    alias /var/www/sites/code.kottenator.com/src/vault/;
    error_page 404 = /404/;
    internal;
  }

  location / {
    include uwsgi_params;
    uwsgi_pass unix:/var/www/sites/code.kottenator.com/var/run/uwsgi.sock;
  }
}

Launch uWSGI:

uwsgi --ini etc/uwsgi.ini

Create bin/update.sh script:

touch bin/update.sh
chmod +x bin/update.sh
nano bin/update.sh
#!/bin/bash
pip install -Ur requirements.txt
manage.py collectstatic --noinput
manage.py migrate
kill -SIGHUP `cat var/run/uwsgi.pid`

Switch to sudo-user and do:

cd /etc/nginx/sites-available
sudo ln -s /var/www/sites/code.kottenator.com/etc/nginx.conf code.kottenator.com
cd ../sites-enabled
sudo ln -s ../sites-available/code.kottenator.com
sudo rm default
sudo service nginx reload

Configure your domain

Go to your domain config and add an A record for code.kottenator.com pointing to the machine's IP address. Wait some time until the DNS is propagated.

Check if it works

Visit http://code.kottenator.com

Open browser dev console, "Network" tab, and check that there are no 4xx or 5xx responses.

That's it.

Clone this wiki locally