0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

XServer VPSに乗り換えて、Docker上にWordPressを構築する|WordPress構築編

Last updated at Posted at 2025-09-28

前回のVPS構築編の続編です。

いよいよVPSにDocker環境を立ち上げて、WordPressを構築していきます。

記事にするにあたり、実際に行った作業そのままではなく、試行錯誤の過程を一部省略しているので、もしかしたら前後で辻褄が合っていない箇所があるかもしれません。

WordPress on Dockerの構築

上述の通り、今回はすべてDocker上に構築していきたいと思います。構築するのはnginx + WordPress(PHP) + MariaDBです(実はDockerをちゃんと?使うのは初めてです笑)

ざっくり手順は以下の通りです。

1. Dockerをインストール
2. nginxを構築
3. WordPress + DBを構築

4. データ移行【次回】
5. サブドメイン対応【次回】
6. HTTPS対応【次回】

前回同様、コマンドの先頭が%の場合はローカル、$の場合はVPS上の一般ユーザ(sudoが使える)、#の場合はVPS上のrootとします。

Docker環境の構築

なにはともあれ、まずはDocker本体の環境を構築していきます。

システム最新化

$ sudo dnf update -y
// 略
完了しました!

$ sudo dnf install -y yum-utils
// 略
完了しました!

Dockerリポジトリ追加

$ sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
repo の追加: https://download.docker.com/linux/centos/docker-ce.repo

Dockerインストール

$ sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
// 略
完了しました!

Dockerサービス起動

$ sudo systemctl start docker

自動起動設定

頻繁にはないかもしれませんが、サーバの再起動時などに自動でDockerを立ち上げてほしいので設定しておきます。

$ sudo systemctl enable docker
Created symlink '/etc/systemd/system/multi-user.target.wants/docker.service' → '/usr/lib/systemd/system/docker.service'.

$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; preset: disabled)
     Active: active (running) since Tue 2025-09-02 23:10:56 JST; 24s ago
 Invocation: 80659fc54c8f47c1a49515cf458f7b4f
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 43385 (dockerd)
      Tasks: 10
     Memory: 23.7M (peak: 25.2M)
        CPU: 184ms
     CGroup: /system.slice/docker.service
             └─43385 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
// 略
 9月 02 23:10:56 xxxx-xxx-xxx-xx systemd[1]: Started docker.service - Docker Application Container Engine.

root以外のユーザでもDockerを使えるようにする

Docker界隈では、hello-worldのイメージをテストで走らせるのが定番みたいです。

$ sudo usermod -aG docker $USER

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete
Digest: sha256:a0dfb02aac212703bfcb339d77d47ec32c8706ff250850ecc0e19c8737b18567
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
// 略

nginx on Docker

続いて、Docker上でnginxを動かします。まずは単純な静的ページのみを置きますが、最終的にはWordPressへのReverse Proxyを担ってもらう想定です。

ディレクトリ構成

/home/admin/app/
  ├── nginx/
  │    ├── conf.d/
  │    │    └── default.conf   ← nginx設定
  │    ├── html/
  │    │    └── index.html     ← テスト用ページ
  │    └── log/                ← ログ保存用
  └── docker-compose.yml       ← 全体compose

nginx/conf.d/default.conf

server {
    listen 80;
    server_name _;

    root /usr/share/nginx/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

nginx/html/index.html(テストページ)

<!DOCTYPE html>
<html>
<head>
  <title>Nginx Test</title>
</head>
<body>
  <h1>It works! Nginx is running in Docker 🚀</h1>
</body>
</html>

docker-compose.yml(nginx単体版)

この時点では443ポートは使いませんが、後々使うので入れてあります。

services:
  nginx:
    image: nginx:latest
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/html:/usr/share/nginx/html
      - ./nginx/log:/var/log/nginx
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

起動

$ docker compose up -d

WordPress + Maria DB on Docker

続いて、Docker上でWordPressを立ち上げます。これもまずは単一のWordPressをドメイン直下で動かしますが、最終的にはサブドメインで運用する想定です。

ディレクトリ構成(追加部分)

/home/admin/app/
  ├── wordpress/           ← WordPress本体用
  │    └── html/           ← WordPressのコード
  └── docker-compose.yml

docker-compose.yml(nginx + WordPress + DB)

なお、この時点でDocument Rootは/var/www/htmlに変更しています。特に深い理由はなく、/usr/share/nginx/htmlのままでもいいのだと思います。

services:
  nginx:
    image: nginx:latest
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/log:/var/log/nginx
      - ./wordpress/html:/var/www/html
    depends_on:
      - wordpress
    networks:
      - app-network

  wordpress:
    image: wordpress:php8.2-fpm
    container_name: admin_wp
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: admin
      WORDPRESS_DB_PASSWORD: admin9999
      WORDPRESS_DB_NAME: admin_wp
    volumes:
      - ./wordpress/html:/var/www/html
    networks:
      - app-network

  db:
    image: mariadb:10.6
    container_name: mariadb
    environment:
      MYSQL_ROOT_PASSWORD: admin9999
      MYSQL_DATABASE: admin_wp
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin9999
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - app-network

volumes:
  db_data:

networks:
  app-network:
    driver: bridge

nginx/conf.d/default.conf

/配下へのアクセスをすべてWordPressに流します。この時点ではnginx-proxyadmin_wpのpathが揃っているのでわかりやすいです。

server {
    listen 80;
    server_name _;

    root /var/www/html;

    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass wordpress:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

wp-contentの適用と権限変更

WordPressの見た目にあたるテーマや、プラグインなどのファイル群は現行サーバから転送したもので置き換えます。

$ cd ~/app/wordpress/html

// バックアップ
$ sudo mv wp-content wp-content_org

// 置き換え(現行サーバからhomeに転送しておく)
$ sudo mv ~/wp-content .

また、同様にimagesディレクトリも現行サーバから転送したもので置き換え(手順は省略)、wp-contentimagesのパーミッションを変更します。

$ docker exec -it admin_wp bash

# ls -lrth /var/www/html
// 中略
drwxr-xr-x.  7 www-data www-data   99 Sep  3 16:14 wp-content_org
drwxr-xr-x.  9     1001     1001  139 Sep  3 20:56 wp-content
drwxr-xr-x. 22     1001     1001 4.0K Sep  4 13:58 images

# id www-data
uid=33(www-data) gid=33(www-data) groups=33(www-data)

# chown -R www-data:www-data /var/www/html/wp-content
# chown -R www-data:www-data /var/www/html/images

# chmod -R 755 /var/www/html/wp-content
# chmod -R 755 /var/www/html/images

これをしておかないと、管理画面からの画像アップロードやプラグイン等の更新ができないかと思います。(逆にそれらを使わないならやらなくてもいいかも?)

(再)起動

$ docker compose up -d

ここまでで、独自のテーマファイルが適用されたWordPressの構築ができました。

次回はおそらく完結編で、

  1. データ移行
  2. サブドメイン対応
  3. HTTPS対応

このあたりをやっていきたいと思います。というわけで、「データ移行・サブドメイン対応編」でお会いしましょう。

シリーズ三部作


最後までご覧いただき、ありがとうございます。ここでひとつ宣伝をさせてください。

規約上、「広告や販売を主目的とする」でなければよさそうなので!

本規約で定めがない場合又は別途当社が承認していない限り、客観的に宣伝広告や販売を主目的とすると判断される記事の投稿をしないこと

楽天モバイル従業員紹介キャンペーン

楽天モバイルでは、楽天従業員による紹介キャンペーンを引き続き実施中です!

今すぐの乗り換えでなくても、下記からログインしておくと一定期間はキャンペーン対象になるので、検討中の方もぜひー。

JP_1200x675.png

▼ キャンペーン適用はこちらからログインしてお申し込みください!
https://r10.to/hkJoD3

従業員紹介キャンペーンは、通常の紹介キャンペーンと比べても、下記のように大変魅力的な特典となっております。

  • 乗り換え・新規共に、1,000ポイント多くもらえる!

  • 再契約や二回線目以降(最大五回線)の契約も特典の付与対象!

  • データタイプやWi-FiルーターのRakuten Turboの契約も特典の付与対象!

※本記事執筆時点のご案内です

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?