バーチャルホストで複数のWordPressを運用

複数のサーバーにWordPressをそれぞれ導入する分には通常環境で良いが,同一サーバーに複数のバーチャルホストを設定して,それぞれにインストールして利用する際に,MYSQLのDB設定やApacheの設定,DNS設定等が面倒なので,メモを残す。バーチャルホストを利用しなくても同一ホストでフォルダーを分けて複数のWordPressを利用できるが,ドキュメントルートを分けてアプリテスト環境として運用したいので環境を構築することにした。

1.MYSQL 環境

MYSQL 環境  (専用DB作成) WordPress毎に専用データベースを準備する。
  事前にDB名,ユーザー名,パワード,ホスト名を決める。
今回は「testdb」を作成して「testuser」でアクセスする環境を作成する。
下記の項目「2.」に各値を記す。(内容はwp-config.phpに保存される)

種別名称パスワード
0. MYSQL-DB管理者rootpassword0
1. WordPress DB名(本番blog)MYSQL
1-1. 「MYSQL」 ユーザーuserpassword1
1-2. 「MYSQL」のホスト名localhost
2. 追加DB名(test用DB)testdb
2-1. 「testdb」ユーザーtestuserpassword2
2-2. 「testdb」のホスト名localhost

MYSQLのインストールフォルダーからコマンドプロンプトを起動しデーターベースを追加。
インストールフォルダー「C:\Program Files\MySQL\MySQL Server 8.0\bin」
1. rootユーザーでログイン     「mysql -u root -p」
2. パスワード入力         「password0」
15. データーベース「testdb」作成  「create database testdb;」
18. アクセスユーザー「testuser」作成(localhost,パスワード)
      「create user ‘testuser’@’localhost’ identified by ‘password2’;」
21. 「testdb」に「testuser」のアクセス権を登録
             「grant all on testdb.* to ‘testuser’@’localhost’;」
24. ユーザー一覧を表示  「select user,host,plugin from mysql.user;」
36. データーベース一覧を表示 「show databases;」

C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7341
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database testdb;
Query OK, 1 row affected (0.01 sec)

mysql> create user 'testuser'@'localhost' identified by '**********';
Query OK, 0 rows affected (0.03 sec)

mysql> grant all on testdb.* to 'testuser'@'localhost';
Query OK, 0 rows affected (0.01 sec)

mysql> select user,host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
| testuser         | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
5 rows in set (0.01 sec)

mysql>

2.Apache環境

Apacheの環境変更(バーチャルホスト関連)
  httpd.conf のDirectory文に新規vhosts分追加・修正
10~17 フォルダー「testhost」追加

<Directory "D:/www/public_html">
    #
    Options FollowSymLinks ExecCGI
    Options +Includes
    #
    AllowOverride All
    Require all granted
</Directory>
#
<Directory "D:/www/testhost">
    #
    Options FollowSymLinks ExecCGI
    Options +Includes
    #
    AllowOverride All
    Require all granted
</Directory>

httpd-vhosts.conf   追加・修正
10~17 バーチャルホスト「testhost.hoge.com」追加

<VirtualHost *:80>
    ServerAdmin webmaster@hoge.com
    DocumentRoot "D:/www/public_html"
    ServerName hoge.com
    ServerAlias www.hoge.com
    ErrorLog "${SRVROOT}/logs/error.log"
    CustomLog "${SRVROOT}/logs/access.log" common
</VirtualHost>
#
<VirtualHost *:80>
    ServerAdmin webmaster@hoge.com
    DocumentRoot "D:/www/testhost"
    ServerName testhost.hoge.com
    ServerAlias www.testhost.hoge.com
    ErrorLog "${SRVROOT}/logs/error.log"
    CustomLog "${SRVROOT}/logs/access.log" common
</VirtualHost>

ssl使用時のhttpd-ssl.conf  追加・修正
今回は15~25を追加
「MDomain」ディレクティブ単位でサーバー証明書は1枚となるので,「hoge.com」と「testhost.hoge.com」で1枚,「hoge.jp」で1枚の発行となる。№27~44は参考表記の為,別途「httpd.conf,httpd-vhosts.conf」等の追加・修正必要。トップレベルドメイン(TLDs)毎に1枚の証明書となるようだ。
【参考:サーバーSSL/TLS化検討・導入 メモ

MDomain hoge.com testhost.hoge.com
<VirtualHost _default_:443>
#   General setup for the virtual host
DocumentRoot "D:/www/public_html"
ServerName hoge.com
ServerAlias www.hoge.com
ServerAdmin webmaster@hoge.com
ErrorLog "${SRVROOT}/logs/error.log"
TransferLog "${SRVROOT}/logs/access.log"
#   :
#   : 省略
#   :
</VirtualHost> 
#
<VirtualHost _default_:443>
DocumentRoot "D:/www/testhost"
ServerName testhost.hoge.com
ServerAlias www.testhost.hoge.com
ServerAdmin webmaster@hoge.com
ErrorLog "${SRVROOT}/logs/error.log"
TransferLog "${SRVROOT}/logs/access.log"
#   :
#   : 省略
#   :
</VirtualHost>
#
MDomain hoge.jp
<VirtualHost _default_:443>
DocumentRoot "D:/www/jp"
ServerName hoge.jp
ServerAlias www.hoge.jp
ServerAdmin webmaster@hoge.com
ErrorLog "${SRVROOT}/logs/error.log"
TransferLog "${SRVROOT}/logs/access.log"
 
SSLEngine on

#SSLCertificateFile "${SRVROOT}/conf/server.crt"
#
#SSLCertificateKeyFile "${SRVROOT}/conf/server.key"
#   :
#   : 省略
#   :
</VirtualHost>
#
<IfModule md_module>
MDBaseServer            off
MDCertificateProtocol   ACME
MDCAChallenges          http-01 tls-alpn-01
MDRenewMode             auto 
MDPrivateKeys           RSA 2048
MDRenewWindow           33%
MDStoreDir              md
# 
#MDCertificateAuthority https://acme-staging-v02.api.letsencrypt.org/directory
MDCertificateAuthority  https://acme-v02.api.letsencrypt.org/directory
MDCertificateAgreement  https://letsencrypt.org/documents/LE-SA-v1.2-November-15-7.pdf
</IfModule>                                                                   

3.サーバーのフォルダー

  ドメインルート配下に新規のバーチャルホストのフォルダー作成しWordpress配置
  フォルダー名:testhost の場合URL https://testhost.hoge.com/blog/

4.DNS環境

以前はドメインにCNAMEで「www」を指定していたが,CNAMEを指定しないことで,Aレコードのワイルドカード指定のみで,「hoge.com」と「*.hoge.com」を対象とする。この辺はDNS運用サイト毎に若干の違いがある。「hoge.com」の前の文字列は何でも通して,サーバー側で振り分けする。vhosts指定で,今回追加したtesthostは,「testhost.hoge.com」とエイリアスの「www.testhost.hoge.com」を受け入れ,その他は先頭指定のデフォルト「hoge.com」に振り分ける。

HostnameTypeContent,Delegateid
hoge.comDomain(FQDN)
hoge.com MX 10
 * A(DDNSのIPアドレス)
 www CNAMEhoge.com

5.まとめ

思いつくまま書いたが,その他は通常の手順と同じだったと思う。
 URL 「https://testhost.hoge.com/blog/」でアクセスできる。
 サーバー証明書もバーチャルホスト分が発行された。
  ※上記名称等は公開用で実在しません。

コメントは受け付けていません。