Using docker swarm to build WordPress

Time:2020-11-25

cause

I used to build WordPress inVultrBut for well-known reasons, the visit to this place is getting slower and slower. Later, I chose Sina cloud. Sina cloud is really cheap and very good, but it needs to be put on record. There is also a comment function. It is estimated that it needs to be emasculated. Think about it or forget it. Find a Hong Kong host to build WordPress.

Purchase mainframe

What I choose here is alicloud’s lightweight application server. The advantage of this host is that it is cheap and enough.
For example, the host I chose in Hong Kong only needs 24 yuan a month, one core has one g of memory, the network speed is 30mbps, the disk space is 25gb, and the monthly traffic is 1TB.

The configuration is shown in the figure below
Using docker swarm to build WordPress

Initialize docker environment

Although there is an installation on itwordpressBut I don’t recommend it because the configuration is too old

In the end, what I chose wasubuntu18.04System, directly use the following command to complete the installation and initialization of dockerdocker swarm

curl -o- -L https://gist.githubusercontent.com/hangox/e679464f35dc2a78920e6249a21d7958/raw/c5541e38979dca1e3e1e9704ad171ed2f0556fa1/ubunut-install-docker.sh | bash

<!– more –>

Write docker compose

Configuration list

version: '3.7'

services:
  caddy:
    image: abiosoft/caddy
    ports:
      - 80:80
      - 443:443
    environment:
      - ACME_AGREE=true
      - TZ=Asia/Shanghai
    volumes:
      - caddy:/root/.caddy
      - wp-src:/usr/src/wordpress
    configs:
      - source: wp_caddy
        target: /etc/Caddyfile
  app:
    image: wordpress:5.4.1-php7.2-fpm
    environment:
      TZ: Asia/Shanghai
      WORDPRESS_DB_HOST: wp_db:3306
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: yourpassword
      WORDPRESS_DB_NAME: wordpress
    depends_on:
      - db
    volumes:
      - wordpress:/var/www/html
      - wp-src:/usr/src/wordpress
  db:
    image: mysql:8
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: yourpassword
      MYSQL_DATABASE: wordpress
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - db:/var/lib/mysql

volumes:
  wordpress:
  db:
  caddy:
  wp-src:
configs:
  wp_caddy:
    external: true

Configuration resolution

caddy

It can be used as a reverse proxy, and at the same time, it can be used to apply for the HTTPS certificate. The configuration is as follows

https://47log.com  https://www.47log.com  {
    root /usr/src/wordpress
    gzip
    fastcgi / wp_app:9000 php
    rewrite {
        if {path} not_match ^\/wp-admin
        to {path} {path}/ /index.php?_url={uri}
    }
    log stdout
    errors stderr
}

Here, I use the config function of docker swarm to directly write the configuration towp_caddyIn this configuration.

db

Mysql8 is used here, WordPress is supported, and the performance should be better
We need to pay attention to it,commmandMust be addedcommand: --default-authentication-plugin=mysql_native_passwordOtherwise, there is no way to carry out password authentication, I just forgot to add this egg, which hurt for a while.

app

Pay attention to the connection mode
If you deploy with docker stack, the name of the deployment should be prefixed with the name of the database connection. For example, here I amdocker stack deploy -c docker-compose.yml wpThe host of the database in the docker network iswp_db。 If your stack name is WordPress, change it to WordPresswordpress_db

Pay attention to the configuration of volume
- wordpress:/var/www/htmlThis thing must be configured. Last time I didn’t configure this thing. After deleting the container, the theme will be gone.

Deployment using docker stack

A line of commanddocker stack deploy -c docker-compose.yml wpYou can get into WordPress after a while

Why use docker swarm. Because of the portainer, docker swarm can be configured with full functions after it is connected to the portal.