A WordPress Setup With Docker, PHP 7.4, Apache 2.4 and MySQL 8 on Ubuntu 20.04 LTS

Image for post
Image for post

Are you a web developer working on another WordPress site? If so, you may want to run a site locally and safely on your laptop to play around with a theme or plugin first, or you want to experiment with this or that cool feature with no fears on breaking the live site.

Today’s post is especially helpful in 2020 and 2021 with so many radical, disruptive changes taking place in the WordPress community mainly introduced by the development of Gutenberg, the new content editor for WordPress 5.0.

Gutenberg aims to be a game-changer in the way WP sites are built. Some argue it resembles Medium or Wix in the way content is created, but judging by the debate users have had so far, the future of the new editor’s adoption might not seem too clear yet.

Be that as it may, in today’s post let me guide you through the process of running a WordPress site locally with the help of a specific, custom Docker set up.

Prepare the Docker Environment

$ git clone git@github.com:programarivm/wpdocker.git

Unzip your ZIP file into the wpdocker directory:

$ unzip /home/standard/Downloads/programarivm.zip -d /home/standard/projects/wpdocker

Create an .env file:

$ cp .env.example .env

Make sure to comment out or remove the CREATE DATABASE statement, if any, from your SQL script and then copy it into docker/mysql/backup.sql

$ cp /home/standard/Downloads/backup.sql /home/standard/projects/wpdocker/docker/mysql/backup.sql

Build the Docker containers:

$ docker-compose up --build
Image for post
Image for post

Import the Database Backup

$ docker exec -it wp_mysql /bin/bash
root@7ed4c31aa253:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use wpdocker;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> source /home/backup.sql

Add a New Entry to Your Hosts File

$ echo $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' wp_httpd)

With the IP at hand, add the following entry to /etc/hosts:

172.18.0.3      wpdocker.local

As you can see my IP is 172.18.0.3 this time, but of course don’t forget to replace that one with your actual IP instead.

Edit Your WordPress Configuration File

$ echo $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' wp_mysql)

And update your wp-config.php file accordingly as follows:

define('DB_NAME', 'wpdocker');
define('DB_USER', 'bob');
define('DB_PASSWORD', 'password');
define('DB_HOST', '172.18.0.4');

Once again, as you can see my container’s IP is 172.18.0.4 this time, but don’t forget to replace that one with your actual IP instead.

Update the URLs

UPDATE wp_options SET option_value='http://wpdocker.local' WHERE option_name='siteurl';UPDATE wp_options SET option_value='http://wpdocker.local' WHERE option_name='home';

Set up File Permissions

sudo chown standard:www-data -R wp-content
sudo chmod 775 -R wp-content

The step-by-step process described above worked for me on Ubuntu 20.04 LTS, and hopefully you’ll find something useful that can be applied to your particular set up and situation too! If you’re already familiar with Docker your site will be up and running in minutes.

In my opinion the present how to guide may definitely be a good alternative to installing WordPress 5 with other popular stacks like XAMPP, WAMP and MAMP, particularly if working as a part of a team.

Thanks so much for reading! For now that’s all I have to say about moving a live Gutenberg WordPress site from live to localhost. I hope you’ll stay tuned for the next episode about installing a self-signed SSL certificate to enable HTTPS.

You may also be interested in…

Written by

Hi there! How are you today? I blog about technology, the Internet, SEO, programming tips, PHP and more. programarivm.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store