こんにちは。hacknoteのJunya.kです。
この記事はAWS初心者向けAWS環境のWordPress基本パターン8つ作ってみたのパターン3の作成方法を紹介する記事です。
そのまま真似をすれば誰でもAWSを使ってWordpressの入ったタイトル通りの構成のサーバーを建てる事ができます。
RDSでMySQLを構築
WordPressが参照するためのデータベースをおくためにRDSにMySQLを構築します。
1.AWSのコンソールへログインし、サービス→データベースからRDSを選択します。
データベースの作成を選択します。
エンジンはMySQLを選択。
ユースケースについてはとりあえずテストにしておきます(次の設定値の初期値が変わるだけです)。 無料枠にチェックをつければ、無料枠対象の条件が直接入ります。各種設定については以下の記事を参照してください。
RDSでMySQLを構築する
基本的にデフォルトで大丈夫です。
作成を選択し、DB インスタンスのステータスが利用可能になったらOKです。
WordPress用のEC2インスタンス作成
WordPressを動かすためにLAMP環境を構築したサーバーを作成します。以下の記事を参考にしてください。
WordPress基本構成パターン2-Webサーバー1台、DBサーバー1台
必要なコマンドのみ列挙しておきます。
$ ssh -i [公開鍵].pem ec2-user@[IPアドレス] [ec2-user@ ~]$ sudo su - [root@ ~]# amazon-linux-extras install php7.2 [root@ ~]# yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm -y [root@ ~]# yum-config-manager --disable mysql80-community [root@ ~]# yum-config-manager --enable mysql57-community [root@ ~]# yum install -y httpd php mysql-community-server [root@ ~]# systemctl start httpd mysqld [root@ ~]# systemctl enable mysqld httpd Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service. Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. [root@ ~]# cd /var/www/html/ [root@ html]# wget https://ja.wordpress.org/latest-ja.tar.gz [root@ html]# tar -xzvf latest-ja.tar.gz [root@ html]# rm latest-ja.tar.gz
ドキュメントルートを変更します。
[root@ html]# vim /etc/httpd/conf/httpd.conf ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # DocumentRoot: The directory out of which you will serve your # documents. By default, all requests are taken from this directory, but # symbolic links and aliases may be used to point to other locations. # DocumentRoot "/var/www/html/wordpress" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EC2インスタンスからRDSへの接続
1.RDSに接続できるようにEC2インスタンスをセキュリティグループに登録します。
作成したRDSのセキュリティグループのInboundを選択します。インバウンド→編集から、先ほど作成したEC2インスタンスのパブリックIPアドレスとプライベートIPアドレスを許可します。固定IPの場合はマスクに/32をつけておきます。
接続の際に使うので、エンドポイントをコピーしておきます。
2.EC2インスタンスからRDSに接続できるかどうかテストしましょう。ssh接続してインスタンスに入ります。
リモートでRDS内部のMySQLに接続します。
[root@ ~]# mysql -h [エンドポイント] -P 3306 -u [USERNAME] -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 44 Server version: 5.6.40 Source distribution Copyright (c) 2000, 2019, 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>
以上のように表示されたら成功です。
3.RDSのMySQLにWordPress用のDBを作成します。
mysql> CREATE DATABASE wpa001; Query OK, 1 row affected (0.01 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | innodb | | mysql | | performance_schema | | sys | | wpa001 | +--------------------+ 6 rows in set (0.00 sec) mysql> CREATE USER wpa_user IDENTIFIED BY 'Townin@0000'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL ON wpa001.* TO wpa_user IDENTIFIED BY 'Towninc@0000'; Query OK, 0 rows affected (0.01 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec)
wpa001というDBを作成し、wpa_userという専用のユーザを設定しました。
作成したユーザでDBにアクセスできたら成功です。
[root@ ~]# mysql -u wpa_user -p -h [エンドポイント] Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 49 Server version: 5.6.40 Source distribution Copyright (c) 2000, 2019, 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> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | wpa001 | +--------------------+ 2 rows in set (0.00 sec)
4.サーバーからWordPressの設定画面に飛んで、DBの情報を打ち込みます。
これでタイトル通りのWordPressサーバー構成が実現できました。