Skip to content

Instantly share code, notes, and snippets.

@mrbongiolo
Last active March 12, 2018 22:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrbongiolo/f556dd0eb27c8fcc9059b63fbb8fd8d0 to your computer and use it in GitHub Desktop.
Save mrbongiolo/f556dd0eb27c8fcc9059b63fbb8fd8d0 to your computer and use it in GitHub Desktop.
From Heroku to Digital Ocean (backed by Dokku 0.6.5)

dokku 0.6.5

DO Dashboard

Create a droplet (can be a basic $5 one) and add a SSH key.

On the Browser

Access the droplet ip and finish the basic Dokku setup, basically it's used to setup a domain and add the SSH key to the dokku user.

The Droplet

ssh root@droplet-ip

Disable password login for SSH

vim /etc/ssh/sshd_config

PermitRootLogin without-password
PasswordAuthentication no

# save the file then restart SSH

sudo service ssh restart

Add a swapfile if the droplet doesn't have enough memory

dd if=/dev/zero of=/swapfile bs=1024 count=1024000
mkswap /swapfile
swapon /swapfile

Create the App

dokku apps:create app-name

Database

# Install the Postgres plugin
dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres

# create the DB
dokku postgres:create db_name

# link DB to the App (this will setup a DATABASE_URL env variable)
dokku postgres:link db_name app-name

Import data from the Heroku Postgres DB

Automatic backups to AWS:S3

# install pip
sudo apt-get install python-pip

# install the aws-cli
pip install awscli

# configure the aws-cli, just run the command and follow the screen instructions
aws configure

# copy the AWS CLI configs to /home/dokku
cp -r /root/.aws/ /home/dokku/.aws

Create a /home/dokku/db_backup.sh shell script. Set the owner as dokku and with executable permission +x. Create a folder /home/dokku/tmp_db_backup that will be used as a temporary folder for the backups, note that this folder must have read and write permission for dokku user, this can be done via chown or by creating the file with su - dokku -c "mkdir ~/tmp_db_backup".

#!/bin/bash
# /home/dokku/db_backup.sh
NOW=$(date +"%Y%m%d_%H%M%S")
cd /home/dokku/tmp_db_backup
dokku postgres:export db_name backup_name | gzip > backup_name.$NOW.dump.gz

aws s3 mv backup_name.$NOW.dump.gz s3://bucket-name/db_backups/$1/

The $1 in s3://bucket-name/db_backup/$1/ will be used to send the backup file to correct "hourly", "daily" or "weekly" folder.

Configure the crontab: crontab -u dokku -e

# Add those lines to the beginning of the file
MAILTO="mail@tld.com"
PATH=/usr/local/bin:/usr/bin:/bin
SHELL=/bin/bash

# Add those lines to the end of the file
# the "2>&1 | /usr/bin/logger -t db_backup_hourly" will make the cron jobs log to the default syslog with the specified tag
0 * * * * /home/dokku/db_backup.sh hourly 2>&1 | /usr/bin/logger -t cron_db_backup_hourly
1 0 * * * /home/dokku/db_backup.sh daily 2>&1 | /usr/bin/logger -t cron_db_backup_daily
2 0 * * 6 /home/dokku/db_backup.sh weekly 2>&1 | /usr/bin/logger -t cron_db_backup_weekly

With those settings the DB will be backed up hourly, daily and weekly to the specified S3. It might the good to setup some lifecycle rules in the bucket folder, ex: delete hourly after 30 days, delete daily after 180 days (or move them to AWS Glacier).

Environment

# copy from Heroku if needed
heroku config -s -a heroku-app-name

dokku config:set app-name ENV_VAR=value ENV_VAR2=value \
VERY_OTHER_ENV_VAR=other_value

SSL

# install the dokku-letsencrypt plugin
sudo dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git

# add email config
dokku config:set --no-restart myapp DOKKU_LETSENCRYPT_EMAIL=your@email.tld

# enable lets encrypt for the app
dokku letsencrypt app-name

# Add lets encrypt cron job to auto renew the certs
dokku letsencrypt:cron-job --add

Edit /home/dokku/app-name/nginx.conf

# add this line to the SSL server
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

Test the SSL configs with: https://www.ssllabs.com/ssltest/index.html

Logs

Using Papertrail for this example. Create an account there and get the log destination URL.

# install logspout plugin
dokku plugin:install https://github.com/michaelshobbs/dokku-logspout.git

# set the logspout server URL (change it accordingly)
dokku logspout:server syslog+tls://logs.papertrailapp.com:12345

# start logspout
dokku logspout:start

The Local Project

# Add a remote pointing to the droplet
git remote add dokku dokku@droplet-ip:app-name

# Push the project to the server
git push dokku master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment