AWSでWordPressサイトを構築 第2回〜Linuxセットアップ〜

こんにちは。かっぺいです。
AWSでWordPressサイト構築、第2回。Linuxのセットアップです。

AmazonLinux2

EC2インスタンスに利用するOSイメージは、追加の費用が発生せずAWSがリポジトリ管理してくれている、AmazonLinux2を利用します。
AmazonLinux2には、AWSを利用するための最小限の構成で組まれています。不足するパッケージをインストールしていきます。

パッケージインストール

WordPressを利用するための条件として、以下の内容があります。

  • PHP 7.4以上
  • MySQL5.7 または MariaDB 10.2以上

AmazonLinux2のデフォルトでインストールされるPHPは古いバージョンのため、Extraから最新をインストールするように設定します。

# sudo amazon-linux-extras | grep php
 _  php7.4                   available    [ =stable ]
 43  php8.0=latest           available    [ =stable ]

PHP8.0を選びます

# sudo amazon-linux-extras enable "php8.0"

パッケージインストール

WordPressサイトを構築するために必要なパッケージをインストールします

# sudo yum install httpd php php-mysql php-mbstring php-gd mariadb-server

php関連で忘れがちなのが、php-gdです。
私自身もハマったのですが、不足していてもなんとなく問題なく動くのですが、サイトアイコンをアップロードした際に、何も動作せずに途中で止まってしまいました。
このパッケージが、画像リサイズをするときに必要らしく、動作しなくなります。

MariaDBのパラメータ変更

MariaDBやMySQLは、UTF-8の文字コードを指定しないと日本語が文字化けする問題があります。
また、最近は絵文字を使うことができるようになり、4バイトコードを使うようにしないと絵文字が正しく反映できません。

server.cnf

serverの設定をするファイルです。
/etc/my.cnf.d/server.cnfを編集します。

[mysqld]
character_set_server = utf8mb4
innodb_buffer_pool_size = 1GB
innodb_log_file_size = 512M
innodb_flush_method = O_DIRECT
  • character_set_server
    utf8mb4にすることにより、4バイトコードを使えるようになります
  • innodb_buffer_pool_size,innodb_log_file_size
    インスタンスの環境に応じて適当な値をセット。今回は、実メモリの50%を設定しました。
  • innodb_flush_method
    OSのキャッシュを使わない方が効率が良いという話があったので、この値を採用しました。

client.cnf

clientの設定をするファイルです。
/etc/my.cnf.d/client.cnfを編集します。

[client]
default-character-set=utf8mb4
  • default-character-set
    MySQL系はクライアント側も文字コードを指定しないとうまく反映しないことがあるので設定します。

httpdの設定

WordPressの設定自体は、.htaccessを使うのですが、この.htaccessを有効にするための設定が必要です。
/etc/httpd/conf/httpd.confを編集します。

<Directory “/var/www/html”>を探し、
AllowOverride None をAllに変更します。

<Directory "/var/www/html">
    Options Indexes FollowSymLinks

    AllowOverride All

    Require all granted
</Directory>

サービスの有効化と開始

パッケージを入れたままでは、サービスが起動するようになっていません。
httpdとmariadbを自動起動するようにします。

# systemctl enable httpd
# systemctl enable mariadb
# systemctl start httpd
# systemctl start mariadb

MariaDBの初期設定

mysql_secure_installationという、デモ用のテーブルなどを削除してくれるスクリプトがあるので、これを実行します。

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): ※AmazonLinux2では初期パスワードなし
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password: パスワードを入力
Re-enter new password: パスワードを入力
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

rootユーザーのパスワード設定後は、すべてデフォルトのまま実行すれば、テスト用のデータなどを削除してくれます。

WordPress用データベースの作成

# mysql -u root -p
Enter password: パスワードを入力
MariaDB [(none)]> create database WordPress;
Query OK, 1 row affected (0.00 sec)

データベースの名前は、wp-config.phpに同じ名前をいれれば良いので、なんでもOKです。

WordPressのインストール

WordPress日本語サイトから、WordPressを入手を選び、ダウンロードリンクをコピーします。
Linuxから、wgetコマンドでダウンロードリンクを指定して、ファイルをダウンロードします。

取得したファイルをunzipで解凍します。

# wget https://ja.wordpress.org/latest-ja.zip
# unzip latest-ja.zip

wordpressフォルダが出来上がるので、このフォルダ配下のオーナーとグループをapacheに変更して、httpdのドキュメントルートにコピーします。

# chown apache. -R wordpress
# cd wordpress
# cp -pr * /var/www/html/

WordPressの設定

公式ページの、インストールガイドを参考に、wp-config.phpを編集します。
CloudFrontやELBでhttps化に対応できるように、スクリプトを追加します。/* Add any custom values〜の後に次の内容を記述します。

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
        $_ENV['HTTPS'] = 'on';
        $_SERVER['HTTPS'] = 'on';
} elseif (isset( $_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO']) && $_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'] === 'https') {
        $_ENV['HTTPS'] = 'on';
        $_SERVER['HTTPS'] ='on';
}

ブラウザから、WordPressインストールページを開く

ここまでで、Linux側の設定が完了しているので、ブラウザで「https://ページのURL/wp-admin/wp-install.php」を開くと初期セットアップ画面になります。