ローカル環境でwordpressをセットアップする-DigitalOceanのkubernetes環境にwordpressを構築する02

2024年1月14日

DOKSの手順のwordpressではなくwordpressのdockerイメージから作ります。

ローカルで作業します

download & run

docker pull wordpress:latest
docker run -p 80:80 -d -i docker.io/library/wordpress:latest

http://localhost/
に繋いでwordpressをセットアップします。

DBはGCEを指定しています。

ingress-nginx利用時にHTTPSで管理画面に接続できなくなるので(loopする)下記のプラグインを入れて有効化しておきます。

ssl-insecure-content-fixer

設定

  • 有効
    • HTTP_X_FORWARDED_PROTO (ロードバランサー、リバースプロキシ、NginX など)

イメージ化の準備

勝手に/var/www/htmlが永続ボリュームとしてマウントされているので、特定します

ocarina@ab350-pro4:~/work/wordpress.docker$ docker ps -a --format "{{.Names}}" | xargs docker inspect | jq -r '["Name","Type","Src","Dst"], (.[] | { Name:.Name, Mounts:.Mounts[] } | [ .Name, .Mounts.Type, .Mounts.Source, .Mounts.Destination ]) | @csv' 
"Name","Type","Src","Dst"
"/suspicious_wu","volume","/var/lib/docker/volumes/929d3a1a7386474b2e4103a37bf8e9d10cda628c05c2e4686c18c0104c9cdbc7/_data","/var/www/html"

カスタムイメージの準備

mkdir -p ~/work/wordpress.docker
cd ~/work/wordpress.docker
mkdir files
docker exec -it f0cc416da81a cat /etc/apache2/sites-enabled/000-default.conf > files/000-default.conf
sudo cp -rip /var/lib/docker/volumes/929d3a1a7386474b2e4103a37bf8e9d10cda628c05c2e4686c18c0104c9cdbc7/_data files/html

vi files/html/wp-config.php

/* Sets up WordPress vars and included files. /
の上に書きを挿入

define('FS_METHOD', 'direct');

define('MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL);

// 複数ドメイン対応
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// https強制の場合は下記アンコメント
//$protocol = "https://";

define('WP_HOME', $protocol . $_SERVER['HTTP_HOST']  );
define('WP_SITEURL', WP_HOME );

define( 'WP_AUTO_UPDATE_CORE', 'minor' );
ocarina@ab350-pro4:~/work/wordpress.docker/files/html$ sudo tar zcfp wproot.tgz .htaccess *
ocarina@ab350-pro4:~/work/wordpress.docker/files/html$ tar tfv wproot.tgz | head
-rw-r--r-- www-data/www-data 598 2023-12-30 08:09 .htaccess
-rw-r--r-- www-data/www-data 405 2020-02-06 15:33 index.php
-rw-r--r-- www-data/www-data 19915 2023-01-01 09:06 license.txt
-rw-r--r-- www-data/www-data  7399 2023-07-06 02:41 readme.html
-rw-r--r-- www-data/www-data  7211 2023-05-13 06:35 wp-activate.php
drwxr-xr-x www-data/www-data     0 2023-12-07 01:25 wp-admin/
drwxr-xr-x www-data/www-data     0 2023-12-07 01:25 wp-admin/js/
-rw-r--r-- www-data/www-data  3465 2021-01-22 21:32 wp-admin/js/media-upload.js
ocarina@ab350-pro4:~/work/wordpress.docker/files/html$ cd ../
ocarina@ab350-pro4:~/work/wordpress.docker/files$ mv -i html/wproot.tgz .
mv: 'html/wproot.tgz' から './wproot.tgz' へ移動できません: 許可がありません
ocarina@ab350-pro4:~/work/wordpress.docker/files$ sudo mv -i html/wproot.tgz .
ocarina@ab350-pro4:~/work/wordpress.docker/files$ 
ocarina@ab350-pro4:~/work/wordpress.docker/files$ cd ../
ocarina@ab350-pro4:~/work/wordpress.docker$ vi Dockerfile 
ocarina@ab350-pro4:~/work/wordpress.docker$ cat Dockerfile 
FROM wordpress:latest

ADD files/wproot.tgz /var/www/html/
ADD files/000-default.conf /etc/apache2/site-enabled/

ocarina@ab350-pro4:~/work/wordpress.docker$ docker build -t myimages/wordpress:0.0.0 ./
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  100.9MB
Step 1/3 : FROM wordpress:latest
 ---> 9071407ed1c0
Step 2/3 : ADD files/wproot.tgz /var/www/html/
 ---> d49e9bb0e23b
Step 3/3 : ADD files/000-default.conf /etc/apache2/site-enabled/
 ---> 2b5a81b49076
Successfully built 2b5a81b49076
Successfully tagged myimages/wordpress:0.0.0

ocarina@ab350-pro4:~/work/wordpress.docker$ docker run -p 80:80 -d -i myimages/wordpress:0.0.0  /bin/bash
db8eba9fa0badcc111f0c3c66f11066b61a046f32e1139017071bd3005417b6c
ocarina@ab350-pro4:~/work/wordpress.docker$ docker ps
CONTAINER ID   IMAGE                      COMMAND                   CREATED         STATUS         PORTS                               NAMES
db8eba9fa0ba   myimages/wordpress:0.0.0   "docker-entrypoint.s…"   3 seconds ago   Up 2 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   friendly_burnell
ocarina@ab350-pro4:~/work/wordpress.docker$ docker exec -it db8eba9fa0ba /bin/bash
root@db8eba9fa0ba:/var/www/html# ls -la
total 252
drwxrwxrwt  5 www-data www-data  4096 Dec 30 00:59 .
drwxr-xr-x  1 root     root      4096 Dec 19 12:35 ..
-rw-r--r--  1 www-data www-data   598 Dec 29 23:09 .htaccess
-rw-r--r--  1 www-data www-data   405 Feb  6  2020 index.php
-rw-r--r--  1 www-data www-data 19915 Jan  1  2023 license.txt
-rw-r--r--  1 www-data www-data  7399 Jul  5 17:41 readme.html
-rw-r--r--  1 www-data www-data  7211 May 12  2023 wp-activate.php
drwxr-xr-x  9 www-data www-data  4096 Dec 30 00:59 wp-admin
-rw-r--r--  1 www-data www-data   351 Feb  6  2020 wp-blog-header.php
-rw-r--r--  1 www-data www-data  2323 Jun 14  2023 wp-comments-post.php
-rw-r--r--  1 www-data www-data  5491 Dec 28 02:49 wp-config-docker.php
-rw-r--r--  1 www-data www-data  3013 Nov 15 17:47 wp-config-sample.php
-rw-rw-rw-  1 www-data www-data  3762 Dec 30 00:43 wp-config.php
drwxr-xr-x  8 www-data www-data  4096 Dec 30 00:59 wp-content
-rw-r--r--  1 www-data www-data  5638 May 30  2023 wp-cron.php
drwxr-xr-x 27 www-data www-data 12288 Dec 30 00:59 wp-includes
-rw-r--r--  1 www-data www-data  2502 Nov 26  2022 wp-links-opml.php
-rw-r--r--  1 www-data www-data  3927 Jul 16 12:16 wp-load.php
-rw-r--r--  1 www-data www-data 50924 Sep 29 22:01 wp-login.php
-rw-r--r--  1 www-data www-data  8525 Sep 16 06:50 wp-mail.php
-rw-r--r--  1 www-data www-data 26409 Oct 10 14:05 wp-settings.php
-rw-r--r--  1 www-data www-data 34385 Jun 19  2023 wp-signup.php
-rw-r--r--  1 www-data www-data  4885 Jun 22  2023 wp-trackback.php
-rw-r--r--  1 www-data www-data  3154 Sep 30 07:39 xmlrpc.php
root@db8eba9fa0ba:/var/www/html# tail wp-config.php
// https強制の場合は下記アンコメント
//$protocol = "https://";

define('WP_HOME', $protocol . $_SERVER['HTTP_HOST']  );
define('WP_SITEURL', WP_HOME );

define( 'WP_AUTO_UPDATE_CORE', 'minor' );

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
root@db8eba9fa0ba:/var/www/html# 

Dockerfile作成に当り公式wordpressのCMDを確認します

docker image inspect 9071407ed1c0 | jq ".[0]" | less

    "Env": [
      "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
      "PHPIZE_DEPS=autoconf \t\tdpkg-dev \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \t\tmake \t\tpkg-config \t\tre2c",
      "PHP_INI_DIR=/usr/local/etc/php",
      "APACHE_CONFDIR=/etc/apache2",
      "APACHE_ENVVARS=/etc/apache2/envvars",
      "PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
      "PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
      "PHP_LDFLAGS=-Wl,-O1 -pie",
      "GPG_KEYS=39B641343D8C104B2B146DC3F9C39DC0B9698544 E60913E4DF209907D8E30D96659A97C9CF2A795A 1198C0117593497A5EC5C199286AF1F9897469DC",
      "PHP_VERSION=8.2.14",
      "PHP_URL=https://www.php.net/distributions/php-8.2.14.tar.xz",
      "PHP_ASC_URL=https://www.php.net/distributions/php-8.2.14.tar.xz.asc",
      "PHP_SHA256=763ecd39fcf51c3815af6ef6e43fa9aa0d0bd8e5a615009e5f4780c92705f583"
    ],
    "Cmd": [
      "apache2-foreground"
    ],

とりあえずCMDだけ利用します。

 cat Dockerfile 
FROM wordpress:latest

ADD files/wproot.tgz /var/www/html/
ADD files/000-default.conf /etc/apache2/sites-enabled/

#ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#ENV PHP_INI_DIR=/usr/local/etc/php
#ENV APACHE_CONFDIR=/etc/apache2
#ENV APACHE_ENVVARS=/etc/apache2/envvars
#ENV PHP_VERSION=8.2.14
CMD /usr/local/bin/apache2-foreground
ocarina@ab350-pro4:~/work/wordpress.docker$ docker build -t myimages/wordpress:0.0.0 ./
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  100.9MB
Step 1/4 : FROM wordpress:latest
 ---> 9071407ed1c0
Step 2/4 : ADD files/wproot.tgz /var/www/html/
 ---> Using cache
 ---> d49e9bb0e23b
Step 3/4 : ADD files/000-default.conf /etc/apache2/sites-enabled/
 ---> Using cache
 ---> 2b5a81b49076
Step 4/4 : CMD /usr/local/bin/apache2-foreground
 ---> Using cache
 ---> ebffc76b62ac
Successfully built ebffc76b62ac
Successfully tagged myimages/wordpress:0.0.0
ocarina@ab350-pro4:~/work/wordpress.docker$ docker images
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
myimages/wordpress   0.0.0     ebffc76b62ac   45 seconds ago   813MB
wordpress            latest    9071407ed1c0   3 weeks ago      740MB
ocarina@ab350-pro4:~/work/wordpress.docker$ docker run -p 80:80 -d -i myimages/wordpress:0.0.0
3381b35914b64dde0e1455ac5516f03bb4ac1d9e6aff06def0b69abd506a6a3d
ocarina@ab350-pro4:~/work/wordpress.docker$ docker ps
CONTAINER ID   IMAGE                      COMMAND                   CREATED         STATUS         PORTS                               NAMES
3381b35914b6   myimages/wordpress:0.0.0   "docker-entrypoint.s…"   3 seconds ago   Up 2 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   reverent_keldysh

カスタマイズされたファイルが反映されていることを確認します

cker ps
CONTAINER ID   IMAGE                      COMMAND                   CREATED         STATUS         PORTS                               NAMES
3381b35914b6   myimages/wordpress:0.0.0   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   reverent_keldysh
ocarina@ab350-pro4:~/work/wordpress.docker$ docker exec -it 3381b35914b6 /bin/bash
root@3381b35914b6:/var/www/html# ps awwwxu|grep apache
root           1  0.0  0.0   2576  1664 ?        Ss   01:54   0:00 /bin/sh -c /usr/local/bin/apache2-foreground
root           7  0.0  0.0 239864 38664 ?        S    01:54   0:00 apache2 -DFOREGROUND
www-data      18  0.3  0.0 245676 51024 ?        S    01:54   0:00 apache2 -DFOREGROUND
www-data      19  0.0  0.0 243056 42980 ?        S    01:54   0:00 apache2 -DFOREGROUND
www-data      20  0.0  0.0 239904 10168 ?        S    01:54   0:00 apache2 -DFOREGROUND
www-data      21  0.0  0.0 239904 10168 ?        S    01:54   0:00 apache2 -DFOREGROUND
www-data      22  0.0  0.0 239904 10168 ?        S    01:54   0:00 apache2 -DFOREGROUND
www-data      23  0.0  0.0 239904 10168 ?        S    01:55   0:00 apache2 -DFOREGROUND
www-data      24  0.0  0.0 239904 10168 ?        S    01:55   0:00 apache2 -DFOREGROUND
root          32  0.0  0.0   3324  1664 pts/0    S+   01:58   0:00 grep apache
root@3381b35914b6:/var/www/html# tail wp-config.php
// https強制の場合は下記アンコメント
//$protocol = "https://";

define('WP_HOME', $protocol . $_SERVER['HTTP_HOST']  );
define('WP_SITEURL', WP_HOME );

define( 'WP_AUTO_UPDATE_CORE', 'minor' );

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
root@3381b35914b6:/var/www/html# tail /etc/apache2/site-enabled/000-default.conf
   Order Deny,Allow
   Deny From All
   Allow from x.x.x.x
   Allow from y.y.y.y
  </IfModule>

 RewriteEngine On
 RewriteRule /xmlrpc.php - [F,L]
</VirtualHost>

imageとDockerfile,コンテンツをバックアップしておきます。

ocarina@ab350-pro4:~/work/wordpress.docker$ time docker save myimages/wordpress:0.0.0 | gzip > ~/GoogleDrive/itinfra.tmp/degitalocean/images/myimages/wordpress-0.0.0.gz

real    1m19.768s
user    0m38.813s
sys 0m2.161s
ocarina@ab350-pro4:~/work/wordpress.docker$ cd ../
ocarina@ab350-pro4:~/work$ cp -rip wordpress.docker ~/GoogleDrive/itinfra.tmp/degitalocean/.

コンテナレジストリにpush

ocarina@ab350-pro4:~/work/doks-wp$ docker tag myimages/wordpress:0.0.0 registry.digitalocean.com/ocarina/doks-wp:0.0.0
ocarina@ab350-pro4:~/work/doks-wp$ docker push registry.digitalocean.com/ocarina/doks-wp:0.0.0
The push refers to repository [registry.digitalocean.com/ocarina/doks-wp]
6571cf3c46c3: Pushed 
190a5c5ad4ad: Pushed 
343629feca26: Pushed 
8c7526a65d8c: Pushed 
77fea2592787: Pushed 
ad0e9cdf7f59: Pushed 
dae03278265f: Pushed 
5c2913c39384: Pushed 
da53f630613f: Pushed 
8d7003052d10: Pushed 
55bc58001d55: Pushed 
c08c3a334b26: Pushed 
32e314cd52b8: Pushed 
92b404f132fb: Pushed 
ef0d6787d529: Pushed 
015538319b33: Pushed 
a74fbcdc7b18: Pushed 
5dd2e9a27d29: Pushed 
20b4537eb782: Pushed 
38a6e0f337b9: Pushed 
a84f293bd7d4: Pushed 
e51e598b299b: Pushed 
7292cf786aa8: Pushed 
0.0.0: digest: sha256:ab964d7970755c83221239d1df46bec01aada72ab6028b40b02ed3ce9a4e69de size: 5131

参考
https://qiita.com/gounx2/items/444a341acd2fd18f2c58

Posted by ocarina