Wordpress Sites with Docker-Scripts

Reading time ~3 minutes

Wordpress Sites with Docker-Scripts

Wordpress Sites with Docker-Scripts

Note: This tutorial can be tried interactively on: https://katacoda.com/dashohoxha/courses/docker-scripts/wordpress

1. Introduction

With docker-scripts we can have one or more Wordpress containers, and each container can serve one or more sites, as shown in the diagram:

wordpress-ds.png

Figure 1: Wordpress with docker-scripts

Since all these sites use the ports 80 and 443, we need a reverse HTTP proxy in order to forward each request to the corresponding container that serves that site/domain. The docker-script container wsproxy plays this role, using Apache virtual domains. Not only that, but wsproxy also gets automatically a free LetsEncrypt SSL certificate for each domain/site that it manages, and also redirects automatically all HTTP requests to HTTPS.

All the containers of docker-scripts are placed on the same local network, and wsproxy knows how to access the container of each domain that it manages. So, only the wsproxy container needs to get the ports 80/443 forwarded from the hosts. The Wordpress containers get their requests from wsproxy through the docker-scripts local network, so they don't need to expose the ports 80/443 to the host or to the outside world.

Each Wordpress container in turn uses Apache virtual domains to be able to serve several (more than one) sites/domains.

Each Wordpress site needs also a database, and all of them use the docker-scripts container mariadb, which they access through the local network of docker-scripts.

2. Install dependencies

  1. Install docker:

    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh
    
  2. Install docker-scripts:

    apt install m4 make
    git clone https://gitlab.com/docker-scripts/ds /opt/docker-scripts/ds
    cd /opt/docker-scripts/ds/
    make install
    
  3. Install Webserver Proxy:

    ds pull wsproxy
    ds init wsproxy @wsproxy
    cd /var/ds/wsproxy/
    vim settings.sh
    ds make
    
  4. Install MariaDB:

    ds pull mariadb
    ds init mariadb @mariadb
    cd /var/ds/mariadb/
    vim settings.sh
    ds make
    

3. Install Wordpress sites

  1. Install a wordpress container:

    ds pull wordpress
    ds init wordpress @wordpress1
    cd /var/ds/wordpress1/
    vim settings.sh
    ds make
    
  2. initialize and Install a Wordpress site:

    cd /var/ds/wordpress1/
    ds site
    ds site init site1.example.org
    ls
    ls site1.example.org
    ls apache2/sites-enabled/
    cat apache2/sites-enabled/site1.example.org.conf
    ls ../wsproxy/sites-enabled/
    cat ../wsproxy/sites-enabled/site1.example.org.conf
    

    Initializing a site with ds site init <domain> not only creates a directory for this site, but also creates the necessary VirtualHost configurations for it on the containers wsproxy and wordpress1. It also gets a free SSL certificate from LetsEncrypt for this site/domain.

    After initializing the site, we can customize its settings.sh and then install wordpress in it:

    vim site1.example.org/settings.sh
    ds site install site1.example.org
    ls site1.example.org/
    

    Now you can open in browser https://site1.example.org and login as admin.

  3. Install another site on the same container:

    cd /var/ds/wordpress1/
    ds site init site2.example.org
    ls
    ls site2.example.org
    ls apache2/sites-enabled/
    ls ../wsproxy/sites-enabled/
    
    vim site2.example.org/settings.sh
    ds site install site2.example.org
    ls site2.example.org/
    
  4. Install a second container:

    ds init wordpress @wordpress2
    cd /var/ds/wordpress2/
    vim settings.sh
    ds make
    
  5. Install a site on the second container:

    ds site init site3.example.org
    vim site3.example.org/settings.sh
    ds site install site3.example.org
    ls site3.example.org/
    

4. Manage a Wordpress site

  1. Backup site3:

    ds backup
    ds backup site3.example.org
    ls -lh backup/
    
  2. Let's delete site3 and then try to restore it from the backup:

    ds site del site3.example.org
    ls
    ls apache2/sites-enabled/
    

    Before restoring, we have to initialize it:

    ds site init site3.example.org
    ls site3.example.org/
    ds restore
    ds restore site3.example.org \
        backup/wordpress-site3.example.org-*.tgz
    ls site3.example.org/
    
  3. Clone a site with ds clone:

    ds site clone
    ds site clone site3.example.org site4.example.org
    

    Cloning a site to another one actually initializes the new site, then makes a backup on the source site and restores it on the target site, and finally fixes the options siteurl and home of the new site.

    Cloning a site might be useful when you want to try something new on the site, for example a new plugin or some configuration. Instead of applying it directly to the site, you may want to test it first on a clone.

  4. Using wp-cli:

    ds wp
    ds wp site3.example.org
    ds wp site3.example.org option
    ds wp site3.example.org option list
    ds wp site3.example.org option get siteurl
    ds wp site3.example.org option get home
    
  5. Working inside the container:

    ds shell
    ls
    cd site3.example.org
    wp
    wp option get siteurl
    wp option get home
    

5. Multiple sites vs. multiple containers

As we have seen, we can install multiple sites in a single Wordpress container, but we can also install a single site for each Wordpress container. Which one is better?

It depends.

When we consolidate multiple sites in a single Wordpress container we use less resources (RAM and disk space) then installing each site on its own container.

However if one of these sites is compromised (for example you install a plugin with a security hole) it can potentially compromise the other sites that are on the same container.

If each site is on its own container, they are better encapsulated from each-other.

Date: 2020-08-15

Author: Dashamir Hoxha

Created: 2020-08-16 Sun 08:05

Validate

OpenPGP Web Key Directory

OpenPGP Web Key DirectoryOpenPGP Web Key DirectoryTable of Contents1. Introduction2. How WKD works3. Building a WKD3.1. Create the direct...… Continue reading

SMTP Server with LDAP Authentication

Published on April 17, 2021

Using WireGuard VPN

Published on November 09, 2020