Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dokku / Digital Ocean / Rails / Postgres / Let's Encrypt / persistent storage
# Creating a Digital Ocean droplet running Rails + Postgres with persistant storage and https
#--------------------------------------------------------------------------------------------
# For your ctrl-D pleasure...
# SERVER_IP
# APP_NAME
# RAILS_SECRET (generate with `rails secret`)
# ADMIN_EMAIL
# Create and configure droplet
#-----------------------------
## Sign in and update
ssh root@SERVER_IP
apt-get update && apt-get upgrade
## Optionally add your locale
locale-gen en_GB en_GB.UTF-8
dpkg-reconfigure locales
## Optionally add Imagemagick if we know we are going to be using it
apt install imagemagick libmagickwand-dev
## Create a swap if you're using a cheapo box ($5 tier)
fallocate -l 2048m /mnt/swap_file.swap
chmod 600 /mnt/swap_file.swap
mkswap /mnt/swap_file.swap
swapon /mnt/swap_file.swap
echo "/mnt/swap_file.swap none swap sw 0 0" >> /etc/fstab
# Create a Dokku app and addons
#------------------------------
## Go to http://SERVER_IP and add a domain name (if you don't it seems to go weird: use a junk one if needed)
dokku apps:create APP_NAME
## Add postgresql
dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres
dokku postgres:create APP_NAME-db
dokku postgres:link APP_NAME-db APP_NAME
## Increase the timeout as initial setup can take a while
dokku config:set APP_NAME CURL_CONNECT_TIMEOUT=30 CURL_TIMEOUT=300
dokku config APP_NAME
## Add persistent storage
### http://dokku.viewdocs.io/dokku~v0.10.3/advanced-usage/persistent-storage/
### Paperclip defaults to: /public/system/uploads
mkdir /var/lib/dokku/data/storage/APP_NAME
chown dokku.dokku /var/lib/dokku/data/storage/APP_NAME
### /app comes from the Dockerfile location for the container (see below)
dokku storage:mount APP_NAME /var/lib/dokku/data/storage/APP_NAME/public/system:/app/public/system
dokku ps:rebuild APP_NAME
### To have a look at the file structure if you get lost: dokku enter APP_NAME
# Local config
#-------------
## Create app.json in Rails root
{
"name": "APP_NAME",
"description": "App Description",
"keywords": [
"dokku",
"rails"
],
"scripts": {
"dokku": {
"postdeploy": "bundle exec rails db:migrate"
}
}
}
# Add git remote and deploy
#--------------------------
## Remember to checkin the new files above!
git remote add dokku dokku@SERVER_IP:APP_NAME
git push dokku master
## Set production environment variables
### Can generate a key with `rails secret`
dokku config:set APP_NAME RAILS_ENV=production SECRET_KEY_BASE=RAILS_SECRET RAILS_SERVE_STATIC_FILES=true
# Add Let's Encrypt
#------------------
## When site is accessible and DNS set up, we can set up https
dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git
dokku config:set --no-restart APP_NAME DOKKU_LETSENCRYPT_EMAIL=ADMIN_EMAIL
dokku letsencrypt APP_NAME
## Make it auto-renew
dokku letsencrypt:cron-job --add
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment