WordPressをAWSLightSailへ切り替えて、無料SSL証明書の設定し、月5ドルで運用

このブログは、AWSLightSailを使って毎月5ドルのプランでWordPressを動かしています。
今回は、古いAWSLightSailで動いていたインスタンスを削除し、新しいAWSLightSailのインスタンスへ切り替える手順を記載します。

WordPressの格安サーバーって結構いろいろありますね。XserverやConoHaなど私もいろいろ使ってきました。その中で今回は、AWSという選択を提案したいと思います。
プランは毎月5ドルで「1 GB RAM、2 vCPU、40 GB の SSD」です。
加えてSSL証明書は無料で利用できますので、結構コストパフォーマンスが良いのではないでしょうか?かなりの低コストでいけるので、当ブログでも数年間愛用しています。

他のサービスからAWSLightSailへ切り替え(引っ越し)たい人も参考になる記事になると思います。
AWSLightSailはroot権限もあるので、SSHで接続して、サーバーの再起動なども自由にできますね。
Amazon Web Services (AWS)が提供するシンプルで手軽なクラウドプラットフォームです。これは、小規模ビジネス、開発者、学生などのユーザーに向けて、アプリケーションやウェブサイトを簡単にデプロイし、管理するための仮想プライベートサーバー(VPS)を提供します。
主な特徴には以下のようなものがあります:
- 簡単なセットアップ: ユーザーは数クリックでサーバーをセットアップし、アプリケーションのデプロイを開始できます。
- 固定料金: 月額固定料金で、予測可能なコスト管理が可能です。
- スケーラビリティ: ビジネスやアプリケーションの成長に応じて、リソースを簡単にスケールアップ・ダウンできます。
- 管理とメンテナンス: サーバーのメンテナンスや管理がAWSによって簡素化されています。
- AWSのインフラストラクチャ: LightsailはAWSの広範なインフラストラクチャと統合されており、AWSの他のサービスと連携して使用することができます。
- セキュリティと信頼性: AWSのセキュリティと信頼性の高いインフラストラクチャを利用することで、データの保護が図られます。
Amazon Lightsailは、複雑な構成やコストの心配を減らし、特に小規模なプロジェクトや予算に限りがあるプロジェクトに適した選択肢となっています。
今回は月5ドルのプランでいきますが、相当のスペックまで利用できます。

2023 年 6 月 29 日以前に作成された仮想サーバーは、同じプランでも1 vCPU が搭載されています。新しいプランでは、2 vCPU ですので、パフォーマンスも良くなりました。

まずは、こちらを参考に進めます。

今回は主に4つの手順があります。新環境を構築したあと、記事データ移行などを行ったあとに、ドメイン付与とHTTPS化します。
- AmazonLightsailでWordPressの構築
- データ移行
- 独自ドメイン切り替え
- SSL証明書の対応(HTTPS化)
ですね。
インスタンスの新規作成
a.[インスタンスの作成]をクリックします。

b.インスタンスロケーションを選択します。

※変更する場合は[AWS リージョンとアベイラビリティーゾーンの変更]を選択しますが、今回は変更しませんので、そのままで進めます。
c.インスタンスイメージを選択します。
- プラットフォームでは[Linux/Unix]を選択する。
- 設計図の選択では[WordPress]を選択する。

d.インスタンスプランを選択します。
今回は5 USDのプランを選択します。

e.インスタンス名を入力し、[インスタンスの作成]をクリックします。

作成後は一覧に遷移し、「保留中」となります。

「実行中」にステータスが変更になるので、[管理]をクリックします。

接続情報が表示されるので、[デフォルトキーのダウンロード]をクリックしてSSH接続のためのKEYをダウンロードしておきます。

ダウンロードしたKEYは後ほど使います。
ブラウザでSSH接続し、WordPressのログインPWを入手
作成したWordPressの環境の管理画面にログインする情報(パスワード)を入手するためにSSH接続する必要があります。
a.[SSHを使用して接続]をクリックします。

b.SSH クライアントウィンドウが開いたら、次のコマンドを入力してデフォルトのWordPressログイン用パスワードを取得します。
$ cat $HOME/bitnami_application_password

このコマンド表示されるパスワードは保存しておき、WordPressのログインで利用します。
ブラウザでWordPressの管理画面にログイン
ドメインはまだ割り当てられていないのでパブリックIPアドレスでアクセスします。
a.ブラウザでパブリックIPアドレス(XXX.XXX.XXX.XXX)にアクセスします。
http://XXX.XXX.XXX.XXX/wp-login.php
パブリックIPアドレスは、インスタンスのホーム画面で取得できます。

b.WordPressの管理画面にログインします。
- [ユーザー名] または [メールアドレス] ボックスに「user」と入力する。
- [パスワード] ボックスに、このチュートリアルの前半で取得したWordPressログイン用パスワードを入力する。
- [ログイン]をクリックする。
c.WordPressの管理画面にログインが成功します。

静的IP(Static IP)アドレスを付与する
a.インスタンス名をクリックして、ネットワーキング情報を表示します。
[静的IPをアタッチする]をクリックします。

b.LightSailのリソース名を指定します。
- LightSailのリソース名を入力する。
- [作成およびアタッチ]をクリックする。

c.静的IPの作成が完了しました。
無事作成されるので、[続行する]をクリックします。

データの移行を実施する
今回は、All-in-One WP Migrationを使ってみます。とても優秀なツールでした。

a.インポート側の最大サイズを確認します。
インポートの最大サイズが80MBになっています。

b.インポート側の最大サイズを520MB(適当)に変更します。
今回私がエクスポートしたデータは全部で320MBでした。(※エクスポートは同様のツールを使ってエクスポートします)
それをインポートするために80MBから520MBに変更しました。
- SSH接続する。
- PHPの設定ファイルを編集する。
$ sudo vi /opt/bitnami/php/etc/php.ini
変更する項目は、下記に記載します。
upload_max_filesize = 520M
post_max_size = 520M
memory_limit = 520M
max_execution_time = 300
max_input_time = 300
上記に変更して保存します。
- WEBサーバー再起動する。
$ sudo /opt/bitnami/ctlscript.sh restart
Restarting services..
画面にアクセスすると、520MBに変わっていることを確認できました。

c.インポートを実施します。
エクスポートしたファイルをドラッグアンドドロップします。

データをエクスポートしたインスタンスのPHPが古く、今回新規作成したインスタンスはPHP8のため怒られている。構わず、[PROCEED>]をクリックする。

インポートが処理されていきます。

インポートが成功しました。

LightSailでDNSゾーンを作成する
a.サイドメニューの「ドメインとDNS」を表示します。

b.[DNSゾーンの作成]をクリックします。
- ドメインのソース:別のレジストラからドメインを使用
- 私の場合は、お名前.comなので別のレジストラです。
- ドメイン名:owani.net(私の場合)

c.DNSゾーンの作成が完了しました。

d.お名前.comでネームサーバー変更します。

設定完了です。

独自ドメインの切り替えとSSL証明書を発行
新しいインスタンスのAレコードを登録します。
a.DNSレコードのAレコードの設定を行います。

b.Let’sEncryptでSSL証明書の発行をします。
ロードバランサーを使用したかったですが、コストかかりますので、定番の無料SSL証明書「Let’sEncrypt」を設定します。
$ sudo /opt/bitnami/bncert-tool
----------------------------------------------------------------------------
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 []: owani.net
The following domains were not included: www.owani.net. Do you want to add them? [Y/n]: n
Warning: No www domains (e.g. www.example.com) or non-www domains (e.g.
www.example.com) have been provided, so the following redirections will be
disabled: non-www to www, www to non-www.
Press [Enter] to continue:
----------------------------------------------------------------------------
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
----------------------------------------------------------------------------
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:
owani.net
3. Configure a cron job to automatically renew the certificate each month
4. Configure web server name to: owani.net
5. Enable HTTP to HTTPS redirection (example: redirect http://owani.net to
https://owani.net)
6. 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: owani.net
Server name: owani.net
E-mail address []: info@owani.net
The Let's Encrypt Subscriber Agreement can be found at:
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.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.202311271526
* /opt/bitnami/apache/conf/bitnami/bitnami.conf.back.202311271526
* /opt/bitnami/apache/conf/bitnami/bitnami-ssl.conf.back.202311271526
* /opt/bitnami/apache/conf/vhosts/wordpress-https-vhost.conf.back.202311271526
* /opt/bitnami/apache/conf/vhosts/wordpress-vhost.conf.back.202311271526
Find more details in the log file:
/tmp/bncert-202311271526.log
If you find any issues, please check Bitnami Support forums at:
https://github.com/bitnami/vms
Press [Enter] to continue:

完了ですね?

はい!
いつかロードバランサーを使用したパターンも試したいです。