WordPress on Docker

WordPress is a content management system for running blogs or websites. Meanwhile, Docker is a software container platform for building, shipping and running applications and/or utilities. Since there are thousands of Docker images already built, installing an application such as WordPress is fairly easy. In this article, I will show you how to install WordPress, MySQL, PHPMyadmin on the Docker environment.

Install Docker

Docker is agnostic, meaning it really doesn’t matter what platform you use, whether you’re on Windows, Linux or the Mac.

Download Docker Docker. Install.

Create A Project Folder

$ mkdir wordpress
$ cd wordpress

Create a Docker Compose file

The docker file for composing an image is called docker-compose.yml. It’s a YAML file that contains instructions on what to do and apps to use.

$ nano docker-compose.yml

Type in the following configuration in the YAML file. In this example, I’m telling docker to install WordPress, MySQL and PHPMyAdmin and using the following TCP ports, credentials, and volumes.

wordpress:
  image: wordpress
  links:
    - wordpress_db:mysql
  ports:
    - 8080:80
  volumes:
    - ~/Docker/wordpress/html:/var/www/html
wordpress_db:
  image: mariadb
  environment:
    MYSQL_ROOT_PASSWORD: password
phpmyadmin:
  image: corbinu/docker-phpmyadmin
  links:
    - wordpress_db:mysql
  ports:
    - 8181:80
  environment:
    MYSQL_USERNAME: root
    MYSQL_ROOT_PASSWORD: password

Run Docker Compose

$ docker-compose up -d

The installation may take anywhere from 5-10 mins or longer. So, grab a cup of coffee.

Access WordPress

Once installed, open up your browser and access WordPress and PHPMyadmin

WordPress at: http://localhost:8080
PHPMyAdmin at: http://localhost:8181

You can access the WordPress files from your project’s “wordpress/html” directory.

wp-docker

That’s it.

Password Protect phpMyAdmin

phpMyAdmin is an open-source MySQL database administration application written in PHP. The software allows database administrators, web administrators, application programmers to perform several database functions via the browser. Functions such as browse and drop databases and tables, views fields and indexes, create, copy, drop, rename and alter databases, tables, fields and indexes, perform queries, update and delete, to name just a few.

Since phpMyAdmin has direct access to your database, it needs to be secure. By default, phpMyAdmin is secured using it’s built-in login feature. If you like to make it even more secure, you can add .htaccess password protection to it by simply adding .htaccess file to the phpmyadmin pages and creating username and password file to go along with it. The following instructions will show you how to add .htaccess protection to phpMyAdmin.

Edit the phpMyAdmin Apache conf file.

sudo nano /etc/phpmyadmin/apache.conf

Add the following in the /usr/share/phpmyadmin directory section.

<Directory /usr/share/phpmyadmin>
  <IfModule mod_authn_file.c>
  AuthType Basic
  AuthName "phpMyAdmin"
  AuthUserFile /etc/phpmyadmin/htpasswd.protect
  </IfModule>
  Require valid-user
</Directory>

Make sure the htpasswd file is not accessible from the web.

Create the htpasswd file. You’ll be asked to enter the password twice.

sudo htpasswd -c /etc/phpmyadmin/htpasswd.protect username

Finally, to make sure your changes are in effect, reboot Apache.

sudo service apache2 restart

Change WordPress Password

One of the worst things that could happen to your blog is losing your login credentials. It’s a rare event, but it does happen occasionally. This is particularly true if you run hundreds of blogs and websites. Keeping track of all usernames and passwords can be a bit of a challenge.

If you have forgotten your WordPress password, you can quickly recover it, by using the “Lost your password” link underneath the login form. In my case, WordPress mail was not working. So, not only I can’t remember my password, but I also can’t send out lost passwords via email.

Thankfully, I had access to PHPMyadmin, a MySQL database administration tool.

Log in to PHPMyadmin. Access the WordPress database and look for the wp_users table. Edit the admin user and change the “user_pass” field. The WordPress password field uses the MD5 encryption instead of being in the clear.

password

Save your changes. Log in to WordPress.

Editing a Blob in PHPMyAdmin

One of my projects this week requires editing a blob. A blob is a binary large object used primarily for storing images, audio and other media types in the database. Unfortunately, the application didn’t allow me to edit a blob. So off to the PHPMyAdmin I went. PHPMyAdmin is a MySQL administration tool.

By default, the blob entries in PHPMyAdmin can’t be edited. Simple texts in blobs are sometimes scrambled. In order to make changes to the blob entries, we need to reconfigure PHPMyAdmin by editing the configuration file.

Edit the /etc/phpmyadmin/config.inc.php file.

$ sudo nano /etc/phpmyadmin/config.inc.php

Add the following entries at the end of the file.

$cfg['ProtectBinary'] = FALSE;
$cfg['DisplayBinaryAsHex'] = FALSE;
$cfg['ShowBlob'] = TRUE;

There is no need to restart any services. Changes are automatically reflected in PHPMyAdmin.

Importing SQL in phpMyAdmin

If you have a problem importing SQL files into phpMyAdmin, the issue could be that the upload limit in PHP is set too low. By default, PHP sets the upload file limit to 2MB, which is too low for most people. You can increase the limit to something more realistic such as 16MB, if you’re working mostly with medium-sized SQL databases.

If you have access to the Terminal and have root access, you can edit your PHP.ini settings. First, you need to find out where your default PHP.ini is located. There might be several PHP.ini files in your system, but there’s only one bound to Apache. To determine to which one is being used, I suggest you create a file that contains the PHP function called phpinfo(). Place this file your web server and run it.

Create a file called phpinfo.php. Enter the code below. Save. Upload to server.

<?php phpinfo(); ?>

Now, open your browser and point the URL where your phpinfo.php is stored on your web server. The phpinfo.php file will run the phpinfo() function and will display the environment variables being used by Apache. Near the top of the page, you’ll see the path of the php.ini. Now that you know where your php.ini file is exactly located, you’ll need to edit that file and look for the Upload Limits.

In Ubuntu, my php.ini is located in /etc/php5/apache2/php.ini.

sudo nano /etc/php5/apache2/php.ini

Look for the File Uploads text as displayed below. Change from 2M to 16M. Save file.

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;
 
upload_max_filesize = 16M

Finally, you need to reboot Apache for your changes take effect.

sudo /etc/init.d/apache restart