
AWS経験5年以上の僕が、EC2にWordPressをインストールする手順を分かりやすく紹介します。
なお、今回はWordPressのインストール完了までを目的にしています。
インストール後、EC2でWordPressを運用する場合は他にいくつかの設定が必要となりますが、それについては本記事では取り扱いません。
本記事の内容
- EC2へのNginx / PHP / MariaDBインストール手順(WordPressに必要なパッケージ)
- EC2へのWordPressインストール手順
もくじ
EC2のWordPress構築イメージ:はじめに
本記事ではAWSマネジメントコンソールとSSHクライアントを使用して、EC2(Amazon Linux 2)にWordPressをインストールする手順をご紹介します。
ミドルウェアはNginx / PHP / MariaDBを使用します。
Nginxについて
Apacheより少しだけWordPressインストールにおける手順が増えますが、Nginxのほうが「処理が軽く同時アクセスに強い」といった利点があります。
ちなみに、有名なレンタルサーバー会社であるエックスサーバーも、WordPressのWebサーバーにNginxを採用しています。
EC2のWordPress構築における準備
WordPressをインストールするための準備を行います。
まずはEC2とセキュリティグループを作成します。
手順の説明のため、上図のみセキュリティグループを描いています。
EC2作成
EC2はAmazon Linux 2をt2.microで作成、パブリックサブネットに配置しパブリックIPアドレスを付与します。
未作成の場合は以下記事を参考に構築します。
-
-
【AWS】EC2でAmazon Linux 2を構築しSSH接続してみよう
続きを見る
セキュリティグループの設定
EC2のセキュリティグループで、インバウンドルールに以下の2つを設定します。
- タイプ:SSH、ソース:マイIP
- タイプ:HTTP、ソース:マイIP
上記を設定後はSSHクライアントを使用して、EC2にSSH接続します。
ミドルウェアをインストール
以下の順番でWordPressに必要なミドルウェアをインストールします。
- NginxとPHPインストール
- MariaDBインストール
- セキュアなパスワードを作成
- MariaDB設定
- Nginx設定
- PHP設定
- NginxとPHP-FPMのサービス起動
- PHP接続確認
- ミドルウェアのサービス自動起動設定
順番に解説しますね。
1. NginxとPHPインストール
amazon-linux-extras
でNginxとPHP(ver7.4)をインストールします。
$ sudo amazon-linux-extras install -y nginx1 php7.4
インストール後の確認。"enabled"となっていればインストール完了です。
$ amazon-linux-extras | egrep 'nginx|php7.4'
38 nginx1=latest enabled [ =stable ]
42 php7.4=latest enabled [ =stable ]
amazon-linux-extrasとは
- Amazon Linux 2で使用できる専用のリポジトリを操作するコマンド
- Amazonが公式サポートしたパッケージをインストールできる
- PHP、Pythonなど特定のパッケージは、ディストリビューションの標準パッケージより新しいバージョンで使用可能
【公式】Extras library (Amazon Linux 2)
続いて、PHP関連のパッケージをインストールします。
$ sudo yum install -y php-gd php-mbstring php-xml
PHP関連パッケージの用途
- php-gd : WordPressにおける画像処理
- php-mbstring : WordPressでマルチバイト文字(日本語)を処理
- php-xml : WordPressのバックアップデータ(.xml)を扱う際に使用
インストールされたことを確認します。
$ yum list installed | grep ^php- | sort
php-cli.x86_64 7.4.21-1.amzn2 @amzn2extra-php7.4
php-common.x86_64 7.4.21-1.amzn2 @amzn2extra-php7.4
php-fpm.x86_64 7.4.21-1.amzn2 @amzn2extra-php7.4
php-gd.x86_64 7.4.21-1.amzn2 @amzn2extra-php7.4
php-json.x86_64 7.4.21-1.amzn2 @amzn2extra-php7.4
php-mbstring.x86_64 7.4.21-1.amzn2 @amzn2extra-php7.4
php-mysqlnd.x86_64 7.4.21-1.amzn2 @amzn2extra-php7.4
php-pdo.x86_64 7.4.21-1.amzn2 @amzn2extra-php7.4
php-xml.x86_64 7.4.21-1.amzn2 @amzn2extra-php7.4
2. MariaDBインストール
yum
でMariaDBをインストールします。
$ sudo yum install -y mariadb-server
インストールされたことを確認します。
$ yum list installed | grep mariadb-server
mariadb-server.x86_64 1:5.5.68-1.amzn2 @amzn2-core
3. セキュアなパスワードを作成
以下の用途で使用するパスワードを3つ作成します。
このパスワードは、この後の手順で使用します。
①MariaDB root
②WordPress DBユーザー
③WordPress 管理画面ログイン用ユーザー
ノートンのパスワード作成サイトを使うと、簡単にパスワードを準備できます。
Norton Password Managerにアクセスし、「パスワードの長さ」を64文字にセット、「パスワードのコピー」をクリックしテキストエディタなどに貼り付けます。続いて「更新ボタン」をクリックし、合計3つの異なるパスワードをメモします。
- 「ランダムパスワードを64文字」とする理由は、セキュリティをより強固にするため。
- もし文字列に ' (シングルクォーテーション)が含まれていた場合は別の文字に置き換えること。MariaDBでパスワードを入力する際、パスワードをシングルクォーテーションで囲む必要がある。しかし、パスワード自体にシングルクォーテーションが含まれると、その時点でパスワードの囲みが終了したと判定され、コマンドが正常に実行できないため。
4. MariaDB設定
次にMariaDBを起動します。
$ sudo systemctl start mariadb
$ ps -ef | grep -v grep | grep mariadb
mysql 1112 945 0 23:54 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
mysql_secure_installation
を実行して、セキュリティを強化しながらMatiaDBをインストールします。
$ sudo 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.
以降は対話形式で入力していきます。
# 初期状態ではMariaDBのrootはパスワードを設定していないので、何も入力せずEnterキー押下
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
# MariaDBのrootにおけるパスワードを設定するので、何も入力せずEnterキー押下
# 末尾に[Y/n]と表示されている対話は、何も入力せずEnterキー押下でYが選択されたことになる
Set root password? [Y/n]
# ノートンのパスワード作成サイトでメモした「MariaDB root」のパスワードをコピペ
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.
# MariaDBの匿名ユーザーアカウントは削除するので、何も入力せずEnterキー押下
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.
# MariaDBのrootによるリモート接続は無効にするので、何も入力せずEnterキー押下
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.
# テスト用のDBは削除するので、何も入力せずEnterキー押下
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.
# 権限テーブルを再読み込みするので、何も入力せずEnterキー押下
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!
以下を実行してWorePress用のDBを作成します。
# MariaDBにrootでログイン
$ mysql -u root -p
# ノートンのパスワード作成サイトでメモした「MariaDB root」のパスワードをコピペ
Enter password:
# 今回はDB名を「wordpress-db」としてWordPress用のDBを作成
# もしDB名にハイフンが含まれる場合は` (バッククォート)で囲む必要がある
MariaDB [(none)]> CREATE DATABASE `wordpress-db`;
# 作成したDBを確認する
MariaDB [(none)]> show databases;
# 今回はDB用のユーザー名を「wordpress-user」として作成し、パスワードを設定
# ノートンのパスワード作成サイトでメモした「WordPress DBユーザー」のパスワードを使用
# 「パスワード」は必ず ' (シングルクォーテーション) で囲む
MariaDB [(none)]> CREATE USER 'wordpress-user'@'localhost' IDENTIFIED BY 'パスワード';
# 上記で作成したユーザーにWorePress用のDBに対する権限を付与する
MariaDB [(none)]> GRANT ALL PRIVILEGES ON `wordpress-db`.* TO "wordpress-user"@"localhost";
# 上記で付与した権限を反映する
MariaDB [(none)]> FLUSH PRIVILEGES;
# 作成したDB用ユーザーの確認
MariaDB [(none)]> SELECT Host, User FROM mysql.user;
# DBから切断する
MariaDB [(none)]> exit
5. Nginx設定
続いてNginxを設定します。
最初にNginxのconfファイルを修正します。
# Nginxのconfファイルをバックアップ
$ sudo cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf_org
# Nginxのconfファイルをvimで編集
$ sudo vim /etc/nginx/nginx.conf
# PHPの実行設定
# 以下の26行目から33行目までのハイライト部分を、nginx.confのserverディレクティブ末尾に追記
*==========================*
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
*==========================*
# vimdiffで修正箇所を確認
$ vimdiff /etc/nginx/nginx.conf_org /etc/nginx/nginx.conf
次にNginxのPHP-FPM用confファイルを修正します。
# NginxのPHP-FPM用confファイルをバックアップ
$ sudo cp -p /etc/nginx/conf.d/php-fpm.conf /etc/nginx/conf.d/php-fpm.conf_org
# NginxのPHP-FPM用confファイルを修正
$ sudo sed -i "s/server unix.*/server unix:\/var\/run\/php-fpm\/php-fpm.sock;/" /etc/nginx/conf.d/php-fpm.conf
# vimdiffで修正箇所を確認
$ vimdiff /etc/nginx/conf.d/php-fpm.conf_org /etc/nginx/conf.d/php-fpm.conf
最後にNginxのPHP用confファイルを修正します。
# NginxのPHP用confファイルをバックアップ
$ sudo cp -p /etc/nginx/default.d/php.conf /etc/nginx/default.d/php.conf_org
# NginxのPHP用confファイルを修正
$ sudo sed -i "s/fastcgi_param SCRIPT_FILENAME.*/fastcgi_param SCRIPT_FILENAME \/usr\/share\/nginx\/html\$fastcgi_script_name;/" /etc/nginx/default.d/php.conf
# vimdiffで修正箇所を確認
$ vimdiff /etc/nginx/default.d/php.conf_org /etc/nginx/default.d/php.conf
PHP-FPMとは
- PHP-FastCGI Process Managerの略。
- クライアントPCがサイトにアクセスすると、サーバーが動的にページを生成する。その動的ページをクライアントPCに返し、ユーザーはサイトを閲覧する。
- NginxとPHP-FPMが連携し、上記の動的ページ生成を行う。
- つまり、WordPressのWebサーバーにNginxを使用する場合は、PHP-FPMの設定が必要となる。
6. PHP設定
次にPHPを設定します。
今回はWordPressのインストール完了までを目的にするので、PHP-FPMのみ設定を変更します。
# PHP-FPMのconfファイルをバックアップ
$ sudo cp -p /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf_org
# PHP-FPMのconfファイルを修正
# "user"と"group"の設定をapacheからnginxに置換
$ sudo sed -i -e "s/^user = apache/user = nginx/" -e "s/^group = apache/group = nginx/" /etc/php-fpm.d/www.conf
# PHP-FPMのPATHを変更
$ sudo sed -i "s/^listen = \/run.*/listen = \/var\/run\/php-fpm\/php-fpm.sock/" /etc/php-fpm.d/www.conf
# UNIXソケットのLISTEN設定を変更
$ sudo sed -i -e "s/^;listen.owner.*/listen.owner = nginx/" -e "s/^;listen.group.*/listen.group = nginx/" -e "s/^;listen.mode/listen.mode/" /etc/php-fpm.d/www.conf
# "listen.acl_users"をコメントアウト
# sedで";&"と指定すると、左で指定した文字列を変更せずに先頭に";"を付与する
$ sudo sed -i "s/^listen.acl_users.*/;&/" /etc/php-fpm.d/www.conf
# vimdiffで修正箇所を確認
$ vimdiff /etc/php-fpm.d/www.conf_org /etc/php-fpm.d/www.conf
7. NginxとPHP-FPMのサービス起動
NginxとPHP-FPMのサービスを起動します。
なお、PHP自体はサービスを起動するという概念はありません。
# Nginxを起動
$ sudo systemctl start nginx
# PHP-FPMを起動
$ sudo systemctl start php-fpm
# サービス起動後の確認("--sort cmd"オプションでプロセス名でソート)
$ ps -ef --sort cmd | grep -v grep | egrep 'nginx|php'
root 7347 1 0 00:24 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 7349 7347 0 00:24 ? 00:00:00 nginx: worker process
root 7334 1 0 00:24 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
nginx 7342 7334 0 00:24 ? 00:00:00 php-fpm: pool www
nginx 7343 7334 0 00:24 ? 00:00:00 php-fpm: pool www
nginx 7344 7334 0 00:24 ? 00:00:00 php-fpm: pool www
nginx 7345 7334 0 00:24 ? 00:00:00 php-fpm: pool www
nginx 7346 7334 0 00:24 ? 00:00:00 php-fpm: pool www
8. PHP接続確認
NginxとPHPの連携が問題ないかを確認します。
index2.phpを公開ディレクトリに作成し、ファイルのオーナーをnginxユーザーに変更します。
$ sudo sh -c "echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index2.php"
$ sudo chown nginx: /usr/share/nginx/html/index2.php
Webブラウザから以下にアクセスします。
http://EC2のパブリックDNS/index2.php
EC2のパブリックDNSは、EC2のメタデータから取得します。(AWSマネジメントコンソールの対象EC2における「詳細」タブからも確認できる)
せっかくなのでコピペしやすいように出力します。
$ curl -s http://169.254.169.254/latest/meta-data/public-hostname | sed -e "1s/^/http:\/\//" -e "1s/$/\/index2.php/" | awk 1
http://ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com/index2.php
- curlの-sオプションで出力の進捗を非表示にする
- 「sed '1s/^/http:\/\//'」で先頭に「http://」を付与
- 「awk 1」で末尾に改行を付与
PHP情報ページが表示されれば、NginxとPHPの連携は成功です。
index2.phpを削除します。
$ sudo rm /usr/share/nginx/html/index2.php
9. ミドルウェアのサービス自動起動設定
EC2を再起動したあとも、Nginx, PHP(php-fpm), MariaDBのサービスが自動起動するように設定します。
# 自動起動をenableに設定
$ sudo systemctl enable nginx php-fpm mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
# 3つのサービスが"enabled"になったことを確認
$ systemctl is-enabled nginx php-fpm mariadb
enabled
enabled
enabled
EC2にWordPressをインストール
最後に以下の順番でWordPressをインストールします。
- WordPressインストールパッケージをEC2に配置
- 認証用ユニークキーをメモ
- wp-config.php修正
- WordPressインストール
- WordPress管理画面にログイン
- サイトにアクセス
順番に解説しますね。
1. WordPressインストールパッケージをEC2に配置
# WordPressのインストールパッケージを取得
$ cd /usr/local/src
$ sudo wget https://wordpress.org/latest.tar.gz
# WordPressのインストールパッケージを展開
$ sudo tar zxvf latest.tar.gz
# WordPressのインストールパッケージを削除
$ sudo rm latest.tar.gz
# インストールファイルをNginxの公開ディレクトリにコピー
$ sudo cp -pr wordpress/* /usr/share/nginx/html/
# コピーしたファイルのオーナーをnginxユーザーに変更
$ sudo chown -R nginx: /usr/share/nginx/html/
2. 認証用ユニークキーをメモ
WordPress Salts Generatorにアクセスし、認証用ユニークキー8行をそのまま全行テキストエディタにコピペします。
認証用ユニークキーとは
- WordPress ユーザーがローカルPCに保存される「ブラウザのクッキー情報」を暗号する際に使用される
- 文字数が多くランダムな文字列を設定することで、WordPressサイトへの不正アクセスや侵入・改竄を防ぐことができる
3. wp-config.php修正
# "wp-config-sample.php"を"wp-config.php"というファイル名でコピー
$ cd /usr/share/nginx/html/
$ sudo cp -p wp-config-sample.php wp-config.php
# DB_NAMEの値を"wordpress-db"に置換
$ sudo sed -i "s/database_name_here/wordpress-db/" wp-config.php
# DB_USERの値を"wordpress-user"に置換
$ sudo sed -i "s/username_here/wordpress-user/" wp-config.php
# 変数"DBP"にノートンのパスワード作成サイトでメモした「WordPress DBユーザー」のパスワードを代入する
# 「パスワード」は必ず ' (シングルクォーテーション) で囲む
$ DBP='WordPress DBユーザーのパスワード'
# 変数"DBP"に代入したパスワードに & (アンパサンド) が 含まれていると、この後実行するsedが意図した結果にならない
# この対処として & にエスケープ文字を追加する(パスワードに & が含まれていない場合も、以下を実行して良い)
$ DBP=$(echo ${DBP} | sed "s/&/\\\&/g")
# DB_PASSWORDの値を変数"DBP"に置換
$ sudo sed -i "s/password_here/${DBP}/" wp-config.php
# 以下の8行を行ごと削除し、認証用ユニークキーの8行をコピペする
$ sudo vim wp-config.php
*==========================*
define( 'AUTH_KEY', 'put your unique phrase here' );
define( 'SECURE_AUTH_KEY', 'put your unique phrase here' );
define( 'LOGGED_IN_KEY', 'put your unique phrase here' );
define( 'NONCE_KEY', 'put your unique phrase here' );
define( 'AUTH_SALT', 'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT', 'put your unique phrase here' );
define( 'NONCE_SALT', 'put your unique phrase here' );
*==========================*
# vimdiffで修正箇所を確認
$ vimdiff wp-config-sample.php wp-config.php
4. WordPressインストール
Webブラウザから以下にアクセスします。
http://EC2のパブリックDNS/wp-config.php
上記URLはEC2のメタデータから取得できます。
$ curl -s http://169.254.169.254/latest/meta-data/public-hostname | sed -e "1s/^/http:\/\//" -e "1s/$/\/wp-config.php/" | awk 1
http://ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com/wp-config.php
はじめに言語を設定します。
真ん中くらいまでスクロールし「日本語」を選択、「続ける」をクリックします。
続けて以下の情報を入力し、「WordPressをインストール」をクリックします。
- サイトのタイトル:WordPressのサイト名
- ユーザー名:WordPress 管理画面ログイン用ユーザー名
- パスワード:ノートンのパスワード作成サイトでメモした「WordPress 管理画面ログイン用ユーザー」のパスワードをコピペ
- メールアドレス:自分の受信可能なメールアドレス
- 検索エンジンでの表示:テスト用のサイトなので
以下の画面が表示されれば、インストールは無事成功です。
続けて「ログイン」をクリックすると、WordPress管理画面に移動します。
5. WordPress管理画面にログイン
ユーザー名とパスワードを入力し、「ログイン」をクリックします。
管理画面にログインできました。
6. サイトにアクセス
Webブラウザから以下にアクセスします。
http://EC2のパブリックDNS/
上記URLはEC2のメタデータから取得できます。
$ curl -s http://169.254.169.254/latest/meta-data/public-hostname | sed '1s/^/http:\/\//' | awk 1
http://ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com
無事にサイトへアクセスできました。
EC2のWordPressインストールは簡単:まとめ
EC2にWordPressをインストールする手順を紹介しました。EC2単体だと簡単ですね。
なお、本格的にEC2でWordPressを運用する場合は、少なくとも以下を考慮する必要があります。
- NginxとPHPのチューニング
- Route 53で独自ドメインを設定
- ELBとACM(AWSのSSL/TLS証明書サービス)
- EC2にEIPを設定
- DBはRDSで稼働させる
AWSの勉強のために、上記を実装したWordPressを運用してみるのも面白そうですね。
最後に、本手順はWordPressのインストール完了までを目的にしています。
作成したEC2は忘れずに削除しましょう。
当記事が参考になれば幸いです。