Move WordPress Site to Dokku Server

If you have a dedicated server and you have done all your testing with docker and dokku, you are ready to move all your sites on your new server.
Most common CMS is of course wordpress, so here I’ll explain how you can safely move a hosted wordpress site to a docker container with dokku.
(PS: This tutorial supposes that you have a base knowledge about docker and dokku).

N.B: I wrote this tutorial with specific idea of how wordpress should be installed in a container.
If you search with google something like “docker wordpress” you will found lots of resources and “dockerfile ready” container with all in one (wordpress + mysql).

For me it’s very important that wordpress and mysql stay on different containers (and dokku helps us with this).
Also important data of wordpress should stay in a safe place, because keep wp-content directory inside the container expose it to the risk of loss important files if you delete the container or something goes wrong with your dokku/docker installation.

Let’s start!

Prerequisites:
– You have to be sure that your wordpress site has LAST stable version of wordpress
– You must have a dump of wordpress database (made with phpmyadmin for example)
– You must have a copy of your wordpress install directory

Step 1: We need wordpress!

WordPress is open source, so we have no problems to download source code from github, but we need to know what is the last STABLE release available, at now it’s 4.4.1.

So first of all, we need to clone wordpress repo at this release:

root@myserver:~/wordpress_git# git clone --branch 4.4.1 --single-branch --depth 1 https://github.com/WordPress/WordPress.git
 Cloning into 'WordPress'...
 remote: Counting objects: 230568, done.
 remote: Compressing objects: 100% (102/102), done.
 remote: Total 230568 (delta 43), reused 0 (delta 0), pack-reused 230466
 Receiving objects: 100% (230568/230568), 134.60 MiB | 8.57 MiB/s, done.
 Resolving deltas: 100% (183750/183750), done.
 Checking connectivity... done.

Now we have a local copy of wordpress source code, but we are in a tag branch, so no edits is allowed.

For add new files in the next step, we need to create our branch:

root@myserver:~/wordpress_git# cd WordPress/
root@myserver:~/wordpress_git/WordPress# git checkout -b wp-dokku
Switched to a new branch 'wp-dokku'

Step 2: copy configuration and other files

Copy wp-config.php from your wordpress directory to the git clone and edit mysql connection part as follow:

/** Name of MySQL Database */
define( 'DB_NAME', getenv('DB_NAME') );

/** Username of MySQL Database */
define( 'DB_USER', getenv('DB_USER') );

/** Password of MySQL Database */
define( 'DB_PASSWORD', getenv('DB_PASS') );

/** Hostname MySQL  */
define( 'DB_HOST', getenv('DB_HOST').':'.getenv('DB_PORT') );

This is very important because in next steps we need to set these variables in dokku with DB credentials.

If you have other files in root directory of your wordpress backup (like .htaccess, favicon.ico, google*.html etc) copy them to git directory. (keep attention to not copy wordpress core files like wp-*, index.php etc).

Now commit all:

root@myserver:~/wordpress_git/Wordpress# git add .
root@myserver:~/wordpress_git/Wordpress# git commit -m 'added wp-config and other files'

Step 3: Deploy to dokku

Now we can add remote dokku origin (keep attention on host name and app name) and push all for build the new container.

root@myserver:~/wordpress_git/Wordpress# git remote add dokku dokku@domain.it:blog

root@myserver:~/wordpress_git/Wordpress# git push dokku refs/heads/wp-dokku:master

remote: -----> Injecting git revision
remote: -----> Releasing blog ...
remote: -----> Deploying blog ...
remote: -----> Shutting down old containers
remote: =====> Application deployed:
remote:        http://blog.domain.it
remote: -----> Cleaning up ...
To dokku@domain.it:blog
 * [new branch]      wp-dokku -> master

It’s online, but if you try to connect to given url you should receive a message “Error establishing a database connection”.
It’s ok, deploy is done but we now need a database.

Step 4:  Create a Database

Connect to host machine where there is dokku and let’s start.

check that our blog app is installed:

root@myserver:~# dokku list
blog
mysql
test
www

Create a mysql (mariaDB) database called “blog” and link to the app “blog”:

root@myserver:~# dokku mariadb:create blog
-----> MariaDB database created: blog
root@myserver:~# dokku mariadb:link blog blog
-----> Releasing blog ...
-----> Deploying blog ...
-----> Shutting down old containers
=====> Application deployed:
       http://blog.domain.it

Check that dokku injected ENV vars for DB:

root@myserver:~# dokku config blog
=== blog config vars ===
DATABASE_URL:  mysql2://blog:***password***@mariadb:3306/blog

If you only see DATABASE_URL env, we need to create missing ENVs for DB connection:

root@myserver:~# dokku mariadb:info blog blog

echo "       Host: mariadb"
echo "       Port: 3306"
echo "       User: blog"
echo "       Password: ***password***"
echo "       Database: blog"
echo
echo "       MARIADB_URL=mysql2://blog:op9deZLGwb5W8f5d@mariadb:3306/blog"

root@myserver:~# dokku config:set blog DB_NAME=blog DB_USER=blog DB_PASS=***password*** DB_HOST=mariadb DB_PORT=3306

Ok now if you navigate to the blog link you should see wordpress install wizard.

Step 5: Create a persistant storage for wp-content

Wp-content folder is very important in wordpress, because it contains all personal files of wordpress installation (including images, plugins and themes).
For this reason I decided to keep this folder in a persistant storage in host machine, so a refresh of container doesn’t delete our important data.

Let’s create a new volume called “blog_content” locate in “/home/data_volumes” folder of host machine and link that to the blog app:

root@myserver:~# dokku volume:create blog_content /home/data_volumes/blog_content:/app/wp-content
-----> Volume created: volume_data_blog_content
root@myserver:~# dokku volume:list
blog_content
video_tutorial
root@myserver:~# ls /home/data_volumes/
blog_content  test_volume
root@myserver:~# dokku volume:link blog blog_content
-----> Volume blog_content linked to application: blog
-----> Releasing blog ...
-----> Deploying blog ...
-----> Shutting down old containers
=====> Application deployed:
       http://blog.domain.it

Now you can copy all wp-content data in /home/data_volumes/blog_content and this will be immediately available inside the container.

Step 6: Restore WordPress DB

Last step, we need to restore our wordpress db.

In my server I have a phpmyadmin container named “mysql”, so we can link our new database to this one:

root@myserver:~# dokku mariadb:link mysql blog

Now we can import our db content with phpmyadmin and all should work.

 

I hope that this tutorial can help somebody that want to migrate a wordpress site to the fantastic world of containers!

 

Like This Post? Share It

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *