[Raspberry pi4] DockerでWordpressを動かす。

Docker
Docker Linux RaspberryPi Ubuntu Wordpress 自宅サーバ

概要

DockerでWordpressを立てたときの備忘録です。ホスト側でNginxが動いていて、SSL証明書も取得済みなので、Wordpressもその仕組に乗っけます。

過去の関連記事
  1. Ubuntu 19.10 でPPPOE接続をしてポート開放を行う。
  2. [RaspberryPi]ドコモ光からNuro光に変えたので、PPPoE接続をやめて普通のポート開放に変更した
  3. [Raspberry pi4] Ubuntu 20.10 のヘッドレスインストール。 キーボード、ディスプレイなしでインストールする方法
  4. [Raspberry pi4] NginxのインストールとLet’s EncryptからSSLサーバー証明書を取得する
  5. [Raspberry pi4] Docker でメールサーバーを建てる。
  6. [Raspberry pi4] Docker で WordPress を動かす。★ ←ここ

試した環境

Raspberry Pi 4 Ubuntu 20.10 (arm64)

$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
$ docker –version
Docker version 20.10.2, build 2291f61
$ docker-compose –version
docker-compose version 1.25.0, build unknown

Wordpress のコンテナは Official Image を使います。

Wordpressを動かすラズパイは、ポート80と443 を解放済み、SSL証明書も取得済みです。

docker-compose.yml

オフィシャルサイトを参考に docker-compose.yml を作ります。ラズパイのアーキテクチャ(arm64)用のMySQL5.7のイメージがなかったので、DBのイメージは、mysql/mysql-server:latest (MySQL 8.0)を使ってみました。

version: '3.1'

services:

wordpress: image: wordpress restart: always ports: - 127.0.0.1:8080:80 # 8080 が外部に公開されないように 127.0.0.1を追記しています environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wp volumes:

# - wordpress:/var/www/html
  - ./html:/var/www/html
# - ./php.ini:/usr/local/etc/php/php.ini
depends_on:
  - db

db: image: mysql/mysql-server:latest # MySQLのImageを変更 # image: mysql:5.7 restart: always environment:

MYSQL_ROOT_PASSWORD: wordpress

  MYSQL_RANDOM_ROOT_PASSWORD: '1'
  MYSQL_USER: wordpress
  MYSQL_PASSWORD: wordpress
  MYSQL_DATABASE: wp
volumes:
  - db:/var/lib/mysql

volumes: db:

wordpress:

プラグインやテーマなどをEmacsで直接編集したかったので、使い勝手のために、html フォルダをホスト側にバインドマウントしていますが、通常はボリュームのままで良いでしょう。そのへんはお好みで。

Wordpressを起動する

docker-compose を実行して、コンテナを起動します。

# コンテナを起動
$ docker-compose up -d

起動すると、ホストの ポート 8080にバインドされます。

Nginxの設定

/etc/nginx/sites-available/default を編集して、ポート80と443でWordpressを外部に公開します。

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

Default server configuration

server {

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name nosubject.io; client_max_body_size 512M;

location / {
	# First attempt to serve request as file, then
	# as directory, then fall back to displaying a 404.

#try_files $uri $uri/ =404; proxy_set_header Host $host; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:8080/; }

# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
#	include snippets/fastcgi-php.conf;
#
#	# With php-fpm (or other unix sockets):
#	fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
#	# With php-cgi (or other tcp sockets):
#	fastcgi_pass 127.0.0.1:9000;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#	deny all;
#}
listen [::]:443 ssl ipv6only=on; # managed by Certbot listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/nosubject.io/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/nosubject.io/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot }

Virtual Host configuration for example.com

You can move that to a different file under sites-available/ and symlink that

to sites-enabled/ to enable it.

#server {

listen 80;

listen [::]:80;

server_name example.com;

root /var/www/example.com;

index index.html;

location / {

try_files $uri $uri/ =404;

}

#}

server { if ($host = nosubject.io) { return 301 https://$host$request_uri; } # managed by Certbot

listen 80 default_server;
listen [::]:80 default_server;

server_name nosubject.io;</span>

return 404; # managed by Certbot }

Nginxを再起動する

設定を反映するため再起動します。

$ sudo systemctl restart nginx

/ で 外からWordpressがみえるようになりました。Certbotがhttpに来た接続を https に飛ばすような記述を追加してくれていますので、http -> httpsの転送がちゃんと動いているかも確認しましょう。

まとめ

Dockerを使って、メールサーバとWordpressを動かすことができました。ラズパイ4の8GB RAM版を使っているので、まだまだ余力がありそうです。

~$ free -h
              total        used        free      shared  buff/cache   available
Mem:          7.6Gi       1.1Gi       5.1Gi        78Mi       1.4Gi       6.4Gi
Swap:            0B          0B          0B
わし
わし

Pi3の4GBモデルが存在したらそれで十分そう。