Skip to content

Instantly share code, notes, and snippets.

@krsyoung
Forked from kimadactyl/README.sh
Last active December 19, 2024 17:45
Show Gist options
  • Save krsyoung/29389d4779d3a5912b0c0c1f25743817 to your computer and use it in GitHub Desktop.
Save krsyoung/29389d4779d3a5912b0c0c1f25743817 to your computer and use it in GitHub Desktop.
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
#-----------------------------
## Log in, update and reboot
ssh root@SERVER_IP
apt-get update && apt-get upgrade -y
reboot
## 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
# Add Postgres Backups
#----------------------
## Create a new DigitalOcean Spaces access key, keep track of the access key and secret
## Make note of your spaces endpoint (something like: https://<space>.nyc3.digitaloceanspaces.com)
## Make an initial Folder (bucket) where you want the backups to be places (like APP_NAME-db-backups)
## Authorize the backup endpoint on the dokku server
dokku postgres:backup-auth APP_NAME-db ACCESS_KEY SECRET "" "" <spaces endpoint>
## Enable encryption of the backups
dokku postgres:backup-set-encryption APP_NAME-db <encryption key>
## Test a backup, where folder name is something like acme-db
dokku postgres:backup APP_NAME-db <folder name>
## Schedule backups to run at 3AM (server time, likely UTC)
dokku postgres:backup-schedule APP_NAME-db "0 3 * * *" <folder name>
## Thanks for the guidance https://tute.io/automatic-backup-postgresql-dokku-digitalocean-spaces and https://github.com/dokku/dokku-postgres
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment