How Can We Help?
This is mostly cribbed from Jasmine Tracey’s excellent post at dev.to. There were a couple things that needed to be added and some local tweaks for me, so I put this here for my own reference. The most critical thing is the need for SSL settings in wp-config.php. I have enough sites that I should move to this model, that I may script it to save steps (I tried, but I don’t think I have enough sites to make it worth solving all the problems that scripting this generates). To speed re-use, I have put the commands/steps needed for re-deployments of existing sites in red.
Creating or migrating a WordPress site to Dokku
DNS prep
We need to prep for the ssl cert and final move by ensuring the DNS has a short TTL to allow for a quick dns change at the end.
Create the app and database
In your dokku terminal run the following command to create your app. I will be using testapp as my app name feel free to use another name. If you will be creating an app at a specific FQDN (app.kapn.net), use that as the app name. Otherwise the web address will be a subdomain of the server address.
Additionally, add other domains that might be used to access the website.
dokku apps:create testapp.tld
dokku domains:add testapp.tld domain.tld
dokku domains:add testapp.tld add.domain.tld
If you run dokku apps:list
you should see your newly created application.
Now will install the mariadb plugin, create a database and link it to our app so that it has access to it. I found that the default Mariadb settings would hang forever on ‘waiting for container to be ready
‘ and so I used the instructions here to add a specific, known working, mariadb version. Probably worth trying the mariadb:create command without the -i flag and everything following to see what happens.
# Install app
sudo dokku plugin:install https://github.com/dokku/dokku-mariadb.git mariadb
# Create database
dokku mariadb:create testappdb -i mariadb -I 10.4.17
# Link database to app
dokku mariadb:link testappdb testapp.tld
Setting up application storage on dokku server
I trimmed this down from the three directories that Jasmine used (themes, plugins, uploads). Maybe that’s a bad call, but I think it works.
# Create the folders (might require sudo)
sudo mkdir -p /var/lib/dokku/data/storage/testapp.tld/wp-content
# Change the permission (might require sudo)
# it's 32767 because that's what dokku needs. Not sure why
sudo chown 32767:32767 /var/lib/dokku/data/storage/testapp.tld/wp-content
# Mount the storage to the container
dokku storage:mount testapp.kapn.net /var/lib/dokku/data/storage/testapp.tld/wp-content:/app/wp-content
# if the container has already been deployed, then it'll have to be restarted
dokku ps:restart testapp.kapn.net
If we are migrating a site, we’ll need to inject the sql dump file into the new db:
dokku mariadb:import testappdb < testapp.dump.sql
Creating a local wordpress instance (or moving an existing one)
and pushing it to dokku
On your local machine clone the wordpress github project. Or fetch your archive of an existing site.
# Download and unzip wordpress files
curl -LO https://wordpress.org/latest.zip
unzip latest.zip
OR
rsync sitearchive_MWP_12345678.zip user@servername.tld:
ssh user@servername.tld
mkdir working_directory
cd working_directory
unzip sitearchive.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"
We are going to update the values of our config file so it knows to look for the dokku environment variables. Edit wp-config.php
. Then update the following sections. This is slick, thanks, Jasmine.
// ** 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 replacing the following:
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' );
One more change to wp-config.php
and we’ll save it. This ensures that when we go SSL, the site doesn’t just immediately break. More details of why at supportmyidea.com.
/** Ensure ssl is detected and responded to appropriately */
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$_SERVER['HTTPS'] = 'on';
}
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 create a Procfile
in the root of the project. Jasmine used Nginx, but I’m an old Apache guy.
web: vendor/bin/heroku-php-apache2 -i custom_php.ini
Commit and Push
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 app is going to be
# reached at testapp.kapn.net this would be:
git remote add dokku dokku@servername.tld:testapp.tld
# push changes
git push dokku master
Copy local files
This is why we created that mount earlier, so we can have persistent storage and a place to do updates as needed.
sudo rsync -avz wp-content/ /var/lib/dokku/data/storage/testapp.tld/wp-content/
sudo chown -R 32767:32767 /var/lib/dokku/data/storage/testapp.tld/wp-content
DNS switch
Change your website’s DNS A record to point at your dokku server.
Enable SSL
We are almost finished. 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 testapp.tld
Testing
Go to your url application url in your browser you should see the installation page of your wordpress site. OR better still your migrated site.