AWSのEC2にLEMP環境(Linux、Nginx、MariaDB、PHP)を作成し、WordPressをインストールする

AWSのEC2にLEMP環境(Linux、Nginx、MariaDB、PHP)を作成し、WordPressをインストールする AWS

AWSのEC2にWordPress環境を作成したので、手順をまとめたいと思います。

AWSのEC2でWordPressだと、Bitnamiを利用するAMIが用意されているので、それを使うと簡単です。
以下の記事ではBitnamiのAMIを利用しています。

また、Lightsailを使用すると、さらに楽で、たぶん5分くらいで作れると思います。
純粋にWordPress環境を作りたいという感じなら、Lightsailを使用するのが一番楽なんですかね。
※Lightsailを使用する場合も、Bitnamiのパッケージを利用することになります。

ただ、今回はBitnamiを利用したくなかったのと、ApacheではなくNginxを使用したかったので、LEMP環境(Linux、Nginx、MariaDB、PHP)で作っています。

作成する環境

以下の環境を作成します。
2021年6月末に作成した時点の情報になります。

  • Linux:Amazon Linux 2
  • Nginx:1.20.0
  • MariaDB:10.5.10
  • PHP:7.4.19
  • WordPress:5.7.2

Nginx、PHP、MariaDBのインストール

Nginx、PHP、MariaDBのインストールについては、以下の記事をご覧ください。

WordPressのインストール

続いて、WordPressをインストールします。
まず、WordPressに必要なパッケージをインストールします。

$ sudo yum install -y php-gd php-mbstring php-xml

WordPressをダウンロードして展開します。

$ cd /usr/share/nginx/html
$ sudo wget https://ja.wordpress.org/latest-ja.tar.gz
$ sudo tar zxvf latest-ja.tar.gz
$ sudo rm -f latest-ja.tar.gz

フォルダの所有者をnginxユーザーに変更します。

$ ll
合計 20
-rw-r--r-- 1 ec2-user nginx 3665  5月 25 19:50 404.html
-rw-r--r-- 1 ec2-user nginx 3708  5月 25 19:50 50x.html
drwxr-xr-x 2 ec2-user nginx   27  6月 29 09:01 icons
-rw-r--r-- 1 ec2-user nginx 3520  5月 25 19:50 index.html
-rw-r--r-- 1 ec2-user nginx  368  5月 25 19:50 nginx-logo.png
lrwxrwxrwx 1 ec2-user nginx   14  6月 29 09:01 poweredby.png -> nginx-logo.png
drwxr-xr-x 5 ec2-user nginx 4096  6月 30 07:06 wordpress
$ sudo chown -R nginx:nginx wordpress
$ ll
合計 20
-rw-r--r-- 1 ec2-user nginx 3665  5月 25 19:50 404.html
-rw-r--r-- 1 ec2-user nginx 3708  5月 25 19:50 50x.html
drwxr-xr-x 2 ec2-user nginx   27  6月 29 09:01 icons
-rw-r--r-- 1 ec2-user nginx 3520  5月 25 19:50 index.html
-rw-r--r-- 1 ec2-user nginx  368  5月 25 19:50 nginx-logo.png
lrwxrwxrwx 1 ec2-user nginx   14  6月 29 09:01 poweredby.png -> nginx-logo.png
drwxr-xr-x 5 nginx    nginx 4096  6月 30 07:06 wordpress

設定ファイルを編集します。

$ cd wordpress
$ cp -p wp-config-sample.php wp-config.php
$ vi wp-config.php

以下の内容を編集します。

/** WordPress のためのデータベース名 */
define( 'DB_NAME', '[作成したデータベース名]' );

/** MySQL データベースのユーザー名 */
define( 'DB_USER', '[作成したユーザー名]' );

/** MySQL データベースのパスワード */
define( 'DB_PASSWORD', '[作成したデータベースのパスワード]' );

認証ユニークキーを編集します。
認証ユニークキーは以下のURLにアクセスすると生成できるので、そのままコピって貼り付けます。

https://api.wordpress.org/secret-key/1.1/salt/
define('AUTH_KEY',         'hoge');
define('SECURE_AUTH_KEY',  'hoge');
define('LOGGED_IN_KEY',    'hoge');
define('NONCE_KEY',        'hoge');
define('AUTH_SALT',        'hoge');
define('SECURE_AUTH_SALT', 'hoge');
define('LOGGED_IN_SALT',   'hoge');
define('NONCE_SALT',       'hoge');

nginxのドキュメントルートをインストールしたWordPressに変更します。

$ sudo vi /etc/nginx/nginx.conf

以下のようにドキュメントルートを変更します。

server {
    listen       80;
    listen       [::]:80;
    server_name  _;
    root         /usr/share/nginx/html/wordpress;

WordPressのパーマリンク設定をデフォルトから変更するために、index.phpに飛ばす設定を追記します。
上記のドキュメントルート設定の下に追記します。

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

Nginxを再起動します。

$ sudo systemctl restart nginx.service

SSLの設定

続いて、SSLの設定を行います。
本記事では「Let’s Encrypt」を利用して、SSLの設定を行います。

以前、Let’s Encryptで設定した時には、certbot-auto コマンドで設定を行っていたのですが、Amazon Linux 2に限らず、Let’s Encryptはバージョン1.10.0のリリースをもってcertbot-auto のサポートを止めたようです。
https://community.letsencrypt.org/t/certbot-auto-no-longer-works-on-debian-based-systems/139702/7

AWS公式サイトで対処方法が記載されていたので、以下の内容で対応しました。
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/SSL-on-amazon-linux-2.html#letsencrypt

EPELパッケージをインストールします。

$ sudo amazon-linux-extras install epel

Certbot と python2-certbot-nginx をインストールします。

$ sudo yum install -y certbot python2-certbot-nginx

Certbot を実行します。

$ sudo certbot
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): [メールアドレスを入力]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at

404 Page not found - Let's Encrypt
Let's Encrypt is a free, automated, and open certificate authority brought to you by the nonprofit Internet Security Res...
You must agree in order to register with the ACME server. Do you agree? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing, once your first certificate is successfully issued, to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: N Account registered. No names were found in your configuration files. Please enter in your domain name(s) (comma and/or space separated) (Enter 'c' to cancel): [対象のドメインを入力] Requesting a certificate for [対象のドメイン] Performing the following challenges: http-01 challenge for [対象のドメイン] Using default addresses 80 and [::]:80 ipv6only=on for authentication. Waiting for verification... Cleaning up challenges Could not automatically find a matching server block for [対象のドメイン名]. Set the `server_name` directive to use the Nginx installer. IMPORTANT NOTES: - Unable to install the certificate - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/[対象のドメイン名]/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/[対象のドメイン名]/privkey.pem Your certificate will expire on 2021-09-30. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew"

NginxのSSL設定

Nginxの設定ファイルに、SSLの設定を追記します。
httpの設定内容を流用して、SSL用のserverディレクティブを作成します。

SSL証明書については、「/etc/letsencrypt/live/[ドメイン名]」配下に作成されているので、パスを指定します。

server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name  _;
    root         /usr/share/nginx/html/wordpress;

    ssl_certificate "/etc/letsencrypt/live/[ドメイン名]/fullchain.pem";
    ssl_certificate_key "/etc/letsencrypt/live/[ドメイン名]/privkey.pem";
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_prefer_server_ciphers on;
    (以下略)
}

SSL証明書の更新を自動化する

作成したSSL証明書は有効期限が90日間となります。
更新するには、certbot コマンドを手動で実行する必要があるので、cronで自動で実行するようにします。

$ sudo crontab -e

毎月1日、16日の1時に実行されるようにしています。
有効期限の90日に合わせて、3ヶ月に1度実行すれば良いのですが、ちょうど有効期限の切れ目に当たってしまう可能性もあるので、月2回実行するようにしました。

00 01 1,16 * * sudo certbot renew
$ sudo systemctl restart crond

タイトルとURLをコピーしました