Deploying WordPress with dokku

I have been spending a few days trying to figure out how to deploy wordpress with dokku. Here is what I found.

Database (MariaDB)

I use tutum/mariadb

docker run -d --name db -p 3306:3306 tutum/mariadb

To view username and password after creation

docker logs <CONTAINER ID>

WordPress

Folder Structure

This will be our basic structure, I will explain each item later

|-- node_modules/
|-- vendor/
|-- wordpress/
|-- wp-content/
|-- .buildpacks
|-- composer.json
|-- composer.lock
|-- index.php
|-- nginx.conf
|-- package.json
|-- wp-config.php

Buildpack

For buildpack to correctly identify your php app, you need to have either “composer.lock” or “index.php” under your root directory. The buildpack also support installing modules using npm if you include “package.json” inside your project (Useful when you need bower). Unfortunately, the buildstep will detect your app as node.js as it sees “package.json” first. To correct this, put a .env file under root directory with the following line. Put a .buildpacks file under the root directory to specify what buildpacks you want to use.

.buildpacks

https://github.com/heroku/heroku-buildpack-php.git
https://github.com/heroku/heroku-buildpack-nodejs.git

Dependency

We will be using WordPress Packagist for our themes and plugins. Note that WordPress is also a dependency! Here I recommended johnpbloch/wordpress‘s package for installing wordpress.

composer.json

{
    "repositories": [{
        "type": "composer",
        "url": "http://wpackagist.org"
    }],
     "require": {
        "johnpbloch/wordpress": "3.9.2",
        "wpackagist/advanced-custom-fields": "4.3.8"
    }
}

URL Rewrite (Pretty Permalinks)

To enable url rewrite, do the following

nginx.conf

location / {
    try_files $uri $uri/ /index.php?$args; 
}
location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}

composer.json

{
    "extra": {
       "heroku": {
            "nginx-includes": ["nginx.conf"]
        }
    }
}

Config

Copy and use the following config inside “wp-config.php”, WordPress is able to look for the config file in its own folder and one-level up.

define('DB_NAME', getenv('DB_NAME'));
define('DB_USER', getenv('DB_USER'));
define('DB_PASSWORD', getenv('DB_PASSWORD'));
define('DB_HOST', getenv('DB_HOST'));

define('WP_CONTENT_DIR', dirname(__FILE__) . '/wp-content' );
define('WP_CONTENT_URL', 'http://' . $_SERVER['HTTP_HOST']. '/wp-content' );

Index.php

Copy “index.php” and replace the require function with the following line.

require( dirname( __FILE__ ) . '/wordpress/wp-blog-header.php' );

Persistence

To persist  “uploads” directory, we will use Persistent Storage plugin.
Inside the app folder (/home/dokku/<app>),  create the following file

PERSISTENT_STORAGE

path/to/data:/app/wp-contents/uploads

Linking Database with WordPress

Link Containers plugin

Issue the following command

dokku link:create wordpress db db
dokku config:set wordpress DB_HOST=db DB_USER=super DB_PASSWORD=<password> DB_NAME=wordpress

Note: A re-deploy is required after linking 2 containers

Custom Domain

Custom Domains

dokku domains:set wordpress www.mywordpress.com

Post Deployment Config

If you are migrating your wordpress to a new server, you may want to change the “siteurl” and “home” value to the new url. Note that “siteurl” should be pointing to your wordpress directory.

UPDATE wp_options SET option_value = 'www.mywordpress.com/wordpress' WHERE option_name = 'siteurl';
UPDATE wp_options SET option_value = 'www.mywordpress.com' WHERE option_name = 'home';

Also you may want to update the guid of the post.

UPDATE wp_posts SET guid = REPLACE(guid, 'www.mywordpress.com/wordpress/wp-content/uploads', 'www.mywordpress.com/wp-content/uploads');

Reference:

Advertisements

One thought on “Deploying WordPress with dokku

  1. Pingback: Hosting wordpress with dokku on digital ocean - Train of Thoughts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s