How I set up my wordpress sites on digitalocean with dokku

I recently discovered two great joys in my IT life: one is digitalocean where I can set up a droplet and have my own server in no time, the other one is docker/dokku. Docker alone is great, but dokku makes deployments a breeze.. it is so close to the heroku experience that it’s almost poignant. I took the easy road and created a dokku droplet.

I’m skipping a lot of steps here such as actually creating a DigitalOcean droplet for dokku and so on but that info can be found here. Also I’d like to say that this specific recipe works for me but your mileage may vary. There’s a lot of resources about setting up wp on dokku out there.

 1. Get wordpress

git clone blog
cd blog

2. Modify wordpress according to your needs

Your wp-config should have the following:

$table_prefix  = 'myblog_';

This is especially useful as you have more than one blog on the same mysql instance. I usually just create one mysql docker instance for all blogs and let each of them use a different table prefix. Works like a charm and limits resources.

And the database settings as follows:

define('DB_NAME', getenv('BLOGS_DB_NAME'));
define('DB_USER', getenv('BLOGS_DB_USER'));
define('DB_PASSWORD', getenv('BLOGS_DB_PASSWORD'));
define('DB_HOST', getenv('BLOGS_DB_HOST'));

3. Set the dokku remote

git remote add dokku dokku@:

If your server is and your blog is called myblog this would be:

4. Creating the database

Now with the mysql plugin for dokku installed we can do:

dokku mysql:create blogs_database

And the configuration vars we have set in the wp_config. I’m using one for all so I’m setting these configurations with the –global flag:

dokku config:set --global BLOGS_DB_NAME='blogs_database'
dokku config:set --global BLOGS_DB_USER='mysql'
dokku config:set --global BLOGS_DB_PASSWORD='8920734jfdsakljfFDAFD7897432987'
dokku config:set --global BLOGS_DB_HOST='dokku-mysql-blogs-database:3306'

Finally we’ll link it to the app we’ll deploy (can be done for multiple apps):

dokku mysqldb:link blogs_database myblog

5. Push to deploy

This ideally should be as simple as doing:

git push dokku master

If nothing happens after pushing probably the ssh key on dokku is incorrect. It will simply silently fail and do nothing. Check the authorized_keys file (~/.ssh/authorized_keys). If your authorized_keys file on the Dokku droplet starts with “ssh-rsa” instead of “command=” remove the ssh-rsa line.

6. Choose your domain

If you have set one or more domains on your droplet you can use them setting them on your app like this:

dokku domains:add myblog

Otherwise you’ll get

7. Storage!

Use docker-options for this. It works fine. If you omit this step you will NOT have persistent plugins/themes/uploads and everything will be wiped clean each time you restart the server or redeploy your apps!

Create directories for your blogs. In case of myblog I like keeping them in the dokku home under a directory I call “volumes”. So:

su - dokku # So we don't need to change owner/permissions later
cd /home/dokku
mkdir volumes
cd volumes
mkdir myblog
cd myblog
mkdir plugins; mkdir themes; mkdir uploads;
#chmod them all to have public read/write access

Now back to root:

su - root

And now the real storage part:

dokku docker-options:add myblog run -v /home/dokku/volumes/homecoffeecode/themes:/app/wp-content/themes
dokku docker-options:add myblog run -v /home/dokku/volumes/homecoffeecode/plugins:/app/wp-content/plugins
dokku docker-options:add myblog run -v /home/dokku/volumes/homecoffeecode/uploads:/app/wp-content/uploads

This is very important! Failing to set this will result in losing all assets/themes/plugins upon restarting any of your wordpress instances.

Also very important is the file upload limit (by default 2mb) that must be set in the nginx configuration as follows:

mkdir /home/dokku/myblog/nginx.conf.d/
echo 'client_max_body_size 50M;' > /home/dokku/myblog/nginx.conf.d/upload.conf
chown dokku:dokku /home/dokku/myblog/nginx.conf.d/upload.conf
service nginx reload

8. Memory

The dokku droplet needs memory… so the 1GB is the minimum viable option. Also the best thing is to just add some swap to make things easier during deployments (especially wordpress or rails). To add swap space do the following:

as root:

dd if=/dev/zero of=/extraswap bs=1M count=512
mkswap /extraswap