How to set up your wordpress site on dokku

July 7, 2020Last Updated: November 15, 2021

This post will cover everything you need to know about getting WordPress deployed to your dokku server from start to finish.

I already have a dokku server setup on digital ocean. If you haven't done so as yet you can create an account and get 100 credits [Free 100 credit (https://m.do.co/c/04ef1769fce8) After that follow the instructions on the dokku documenation to get your server up and running DigitalOcean Droplet Installation Notes

Let us begin, to get the most out of this tutorial follow along and comment below if there are any issues with the steps.

Installing the MariaDB plugin on dokku, creating a database, and linking it to our app

In your dokku terminal run the following command to create your app. I will be using blogger as my app name feel free to use another name.

dokku apps:create blogger

If you run dokku apps:list you should see your newly created application.

Now will install the mariab plugin, create a database and link it to our app so that it has access to it

# Install app
sudo dokku plugin:install https://github.com/dokku/dokku-mariadb.git mariadb

# Create database
dokku mariadb:create bloggerdb

# Link database to app
dokku mariadb:link bloggerdb blogger

Creating a local wordpress instance and pushing it to dokku

On your local machine clone the wordpress github project

# Download and unzip wordpress files
curl -LO https://wordpress.org/latest.zip
unzip latest.zip

# Change directory to downloaded project
cd wordpress

# Download wordpress gitignore file
curl https://raw.githubusercontent.com/github/gitignore/master/WordPress.gitignore > .gitignore

# Initialize repository and make initial commit
git init
git add .
git commit -m "Initial commit of wordpress files"

# Open folder in your code editor for this tutorial i'll be using visual studio code
code .

We are going to update the values of our config file so it knows to look for the dokku environment variables. First copy wp-config-sample.php to wp-config.php. Then update the following sections

// ** MySQL settings - You can get this info from your web host ** //
$url = parse_url(getenv("DATABASE_URL"));

$host = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$database = substr($url["path"], 1);

/** The name of the database for WordPress */
define( 'DB_NAME', $database );

/** MySQL database username */
define( 'DB_USER', $username );

/** MySQL database password */
define( 'DB_PASSWORD', $password );

/** MySQL hostname */
define( 'DB_HOST', $host );

We also want to change authentication keys and salts so go to https://api.wordpress.org/secret-key/1.1/salt/ copy and paste the generated salts in the wp-config.php file

define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
define( 'NONCE_SALT',       'put your unique phrase here' );

We also need to create a custom_php.ini file to improve some settings

upload_max_filesize = 128M
post_max_size = 128M
max_execution_time = 60
memory_limit = 512M

Now we need to tell dokku to use those settings when running the project to do that we will use a Procfile in the root of the project

web: vendor/bin/heroku-php-nginx -i custom_php.ini

Now we will commit and push our changes to our dokku server

# commit changes
git add .
git commit -m "updated config to work with dokku url"

# Add the dokku url to the downloaded project
# If your server is dokkuserver.com and your blog is called blogger this would be:
git remote add dokku dokku@dokkuserver.com:blogger

# push changes
git push dokku master

Setting up application storage on dokku server

# Create the folders (might require sudo)
mkdir -p /var/lib/dokku/data/storage/blogger/plugins
mkdir -p /var/lib/dokku/data/storage/blogger/themes
mkdir -p /var/lib/dokku/data/storage/blogger/uploads

# Change the permission (might require sudo)
chown 32767:32767 /var/lib/dokku/data/storage/blogger/plugins
chown 32767:32767 /var/lib/dokku/data/storage/blogger/themes
chown 32767:32767 /var/lib/dokku/data/storage/blogger/uploads

# Mount the storage to the container
dokku storage:mount blogger /var/lib/dokku/data/storage/blogger/plugins:/app/wp-content/plugins
dokku storage:mount blogger /var/lib/dokku/data/storage/blogger/themes:/app/wp-content/themes
dokku storage:mount blogger /var/lib/dokku/data/storage/blogger/uploads:/app/wp-content/uploads

The default upload limit for nginx is 2mb let us update the limit to 100mb. Sudo may be required to run these commands

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

We are almost finish. To ensure we have a secure site, we will add https using lets encrypt

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

# Set the global email
dokku config:set --global DOKKU_LETSENCRYPT_EMAIL=your@email.tld

# Setup cronjob to auto-renew certificates when they expire
dokku letsencrypt:cron-job --add

# Add https to site
dokku letsencrypt:enable blogger

Go to your url application url in your browser you should see the installation page of your wordpress site. Thank you for following along

Resources

  • https://github.com/dokku/dokku-mariadb
  • https://github.com/dokku/dokku-letsencrypt
  • http://dokku.viewdocs.io/dokku~v0.6.3/installation/
  • https://wordpress.org/download/