AWS Lightsail に WordPress を構築して HTTPS 化する手順

AWS Lightsail に WordPress を構築して HTTPS 化する手順

December 15, 2021

久しぶりに Lightsail に WordPress を構築する機会があったものの、すっかり手順を忘れてしまっていたので。(本記事で説明不足な点は、途中途中で記載する参考リンクをご参照ください。こんな記事よりもずっと役に立ちます。)

WordPress のインスタンスを作成 #

ご参考:Amazon Lightsail を使って 15 分で WordPress を構築してみた | DevelopersIO

インスタンスを生成 #

特に書くことはないですね。WordPress のインスタンスイメージを選択して作成するだけです。SSH キーペアはデフォルトのものを適用することもできますが、インスタンスごとに作っておいた方が管理しやすいかと思います。

Staitc IP を割り当て #

そのままだとインスタンスをリブートするたびに IP アドレスが変わってしまします。忘れずに Static IP を割り当てておきましょう。

WordPress ユーザとパスワードを確認 #

インスタンスが作成されたら SSH 接続しましょう。

Linux ip-172-99-99-999 4.19.0-17-cloud-amd64 #1 SMP Debian 4.19.194-3 (2021-07-18) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
       ___ _ _                   _
      | _ |_) |_ _ _  __ _ _ __ (_)
      | _ \ |  _| ' \/ _` | '  \| |
      |___/_|\__|_|_|\__,_|_|_|_|_|

  *** Welcome to the WordPress packaged by Bitnami 5.8.1-18        ***
  *** Documentation:  https://docs.bitnami.com/aws/apps/wordpress/ ***
  ***                 https://docs.bitnami.com/aws/                ***
  *** Bitnami Forums: https://community.bitnami.com/               ***
Last login: Thu Dec  9 30:45:15 2021 from 55.250.200.55

その後、

cat bitnami_credentials

を実行すると、WordPress のユーザとパスワードが表示されます。

bitnami@ip-172-99-99-999:~$ cat bitnami_credentials

Welcome to the Bitnami WordPress Stack

******************************************************************************
The default username and password is 'user' and 'abcd1234WXYZ'.
******************************************************************************

You can also use this password to access the databases and any other component the stack in
cludes.

Please refer to https://docs.bitnami.com/ for more details.

ユーザは user で固定、パスワードはランダムです。これで WordPress にログインできます。

HTTPS 化する #

では次に HTTPS 化しましょう。HTTPS 化のさせ方は大きく3つあります。

  1. bncert-tool で HTTPS 化する。
  2. Cloud Front を挟んで HTTPS 化する。
  3. Load Balancer を挟んで HTTPS 化する。

下にいくほど大掛かりになりお金がかかります。そうする必要性があれば実施すべきですが、とりあえず HTTPS 化させたいという場合は bncert-tool の方法でシンプルに対応しましょう。本記事では bncert-tool を使った方法を記載します。

Cloud Front を使った方法

この方法については以下の記事が参考になります。

Amazon Lightsail インスタンスと AWS CloudFront を使って HTTPS 化してみた | DevelopersIO

なお Cloud Front を挟む場合、そのままだと WordPress の表示崩れが発生したりといった問題が起きるため、Cloud Front のキャッシュ設定を変えたり、WordPress にプラグインを追加したりといったことが必要になります。詳細は以下の記事が参考になります。

CloudFront + WordPress 構成の必須設定 | Oji-Cloud

Load Balancer を使った方法

Lightsail のインスタンスを複数台立てるほどの大規模なサイトであればこの方法になるかもしれませんが、大抵の人にとっては必要ないでしょう。(実施してみたい方は調べれば参考記事が見つかると思います。)

DNS の設定をする #

サイトに適用したいドメインを設定しましょう。Route 53 だったりお使いの DNS で、Lightsail の Static IP に向けて A レコードを登録しましょう。

なお、この後の bncert-tool の設定フロー例では www 付きのドメインから www 無しのドメインにリダイレクトするように設定しています。そちらと合わせるならば以下のように設定しておきましょう。(例としてドメインは example.com だとします。)

Record name     | Type  | Value
----------------|-------|----------------------------------------
example.com     | A     | 55.123.456.70 (Lightsail の Static IP)
www.example.com | CNAME | example.com

bncert-tool を実行する #

インスタンスに SSH 接続しましょう。以下を実行します。

sudo /opt/bitnami/bncert-tool

アップデートがあると言われたら y してあげましょう。

An updated version is available. Would you like to download it? You would need to run it ma
nually later. [Y/n]: y

The tool will exit now. To run the updated version run the following command:

/opt/bitnami/bncert-tool

その後同コマンドを再度実行します。

sudo /opt/bitnami/bncert-tool

ドメインを聞かれるので答えます。(ここではドメインを example.com としています。)

----------------------------------------------------------------------------
Welcome to the Bitnami HTTPS Configuration tool.

----------------------------------------------------------------------------
Domains

Please provide a valid space-separated list of domains for which you wish to
configure your web server.

Domain list []: example.com

親切にも www 付きのドメインも含めることを提案してくれますので y します。

The following domains were not included: www.example.com. Do you want to add them? [Y/n]
: y

リダイレクトの設定を聞かれますので答えましょう。ここでは以下の通りに回答しています。

  • HTTP から HTTPS へリダイレクトする。
  • www 付きのアドレスから www 無しのアドレスへとリダイレクトする。
----------------------------------------------------------------------------
Enable/disable redirections

Please select the redirections you wish to enable or disable on your Bitnami
installation.


Enable HTTP to HTTPS redirection [Y/n]: y
Enable non-www to www redirection [Y/n]: n
Enable www to non-www redirection [y/N]: y

設定変更内容の確認を促されますので y します。証明書の更新を行うクーロンも設定してくれます。

----------------------------------------------------------------------------
Changes to perform

The following changes will be performed to your Bitnami installation:

1. Stop web server
2. Configure web server to use a free Let's Encrypt certificate for the domains:
example.com www.example.com
3. Configure a cron job to automatically renew the certificate each month
4. Configure web server name to: example.com
5. Enable HTTP to HTTPS redirection (example: redirect http://example.com to
https://example.com)
6. Enable www to non-www redirection (example: redirect www.example.com to
example.com)
7. Start web server once all changes have been performed


Do you agree to these changes? [Y/n]: y

証明書に登録するメールアドレスと規約への同意を求められます。回答しましょう。

----------------------------------------------------------------------------
Create a free HTTPS certificate with Let's Encrypt

Please provide a valid e-mail address for which to associate your Let's Encrypt
certificate.

Domain list: example.com www.example.com

Server name: example.com

E-mail address []: hello.world@gmail.com

The Let's Encrypt Subscriber Agreement can be found at:

https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf

Do you agree to the Let's Encrypt Subscriber Agreement? [Y/n]: y

設定変更が実行されます。すぐに終わるはずです。

----------------------------------------------------------------------------
Performing changes to your installation

The Bitnami HTTPS Configuration Tool will perform any necessary actions to your
Bitnami installation. This may take some time, please be patient.

----------------------------------------------------------------------------
Success

The Bitnami HTTPS Configuration Tool succeeded in modifying your installation.

The configuration report is shown below.

Backup files:
* /opt/bitnami/apache/conf/httpd.conf.back.202112091545
* /opt/bitnami/apache/conf/bitnami/bitnami.conf.back.202112091545
* /opt/bitnami/apache/conf/bitnami/bitnami-ssl.conf.back.202112091545
* /opt/bitnami/apache/conf/vhosts/wordpress-https-vhost.conf.back.202112091545
* /opt/bitnami/apache/conf/vhosts/wordpress-vhost.conf.back.202112091545

Find more details in the log file:

/tmp/bncert-202112091545.log

If you find any issues, please check Bitnami Support forums at:

https://community.bitnami.com

Press [Enter] to continue:

これで設定は全て終わりです。サイトに HTTPS でアクセスできることを確認してみましょう。