LoginSignup
2
4

「さくらのレンタルサーバ」Aレコード変更前に無料SSLを導入したい

Posted at

さくらのレンタルサーバでは、ドメインのAレコードがさくらのレンタルサーバに向いていないと、Let's Encrypt (無料SSL)を利用することができません。
新規サイト立ち上げの時には問題ないのですが、既存サイトのサーバ移転をする時に、Aレコードを変更して反映されるまで Let's Encryptの利用設定すらできず、SSLが効かない時間帯が発生してしまいます。

そこで、Certbotクライアントを使って、ローカル環境でLet's EncryptのSSL証明書を発行し、「さくらのレンタルサーバ」に独自SSLとしてインストールして、一時しのぎする方法をご紹介します。

実行環境 : macOS Ventura です。

Homebrewをインストール

CertbotクライアントのインストールにHomebrewを使います。
Homebrewがインストールされているかどうかを確認して、インストールされていない場合はインストールしてください。

Homebrewがインストールされているか確認

brew --version

バージョンが表示される場合は、インストール済みです。

Homebrew 4.1.13

Homebrewをインストールする

正確なインストールコマンドはHomebrewの公式サイトで確認できます。

インストールコマンド (2023/10/13現在)

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Certbotクライアントをインストール

Let's EncryptからSSL証明書を発行するためのCertbotクライアントツールをインストールします。

Certbot インストールコマンド

brew install certbot

インストールできたか確認

certbot --version

インストールできていれば、バージョンが表示されます。

certbot 2.6.0

SSL証明書を発行する

Certbotを使うと、コマンドラインでSSL証明書を発行できます。
example.comとwww.example.comの証明書を発行する場合、下記のコマンドを実行します。

sudo certbot certonly --manual -d 'example.com' -d 'www.example.com' -m 'mail@example.com' --agree-tos

--manual オプション

手動でドメイン名の認証を行い、SSL証明書を取得します。

--d オプション

SSL証明書を取得するドメイン名を指定します。複数指定することもできます。

-m オプション

メールアドレスを入力しておきます(公開はされません)。証明書を発行するドメインとは違うメールアドレスでも大丈夫です。

--agree-tos オプション

利用規約に同意します。このオプションを指定しておくと、後で聞かれません。

画面に表示される内容に沿って進めていく

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: 

Let's Encryptのプロジェクトパートナーにメールアドレスを共有して、DMを送っても良いか聞かれます。
不要なら No を選択します。

ドメインの所有確認を行う

Account registered.
Requesting a certificate for example.com and www.example.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create a file containing just this data:

HoDUtcD73I7zlqQzM-TL1-TOO2WiREEDJB4FmcpIx78.8QUqvDci5i_2tbCTaIT7f0uJopebyORwbU4UyfBbLlw

And make it available on your web server at this URL:

http://www.example.com/.well-known/acme-challenge/HoDUtcD73I7zlqQzM-TL1-TOO2WiREEDJB4FmcpIx78

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

画面に記載されている”内容”、”ファイル名”の通りにファイルを作成し、サーバ上の指定されている場所にファイルを置いて、準備ができたらEnterを押します。
複数のドメイン名(www.example.com と example.com など)を指定した場合は、ドメインごとに所有確認が必要です。

ドメインの所有確認が終わると、証明書が発行されます。
ここまで来たら、サーバにアップした所有確認用のファイルはもう不要なので、削除して大丈夫です。

発行された証明書を確認

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/example.com/privkey.pem
This certificate expires on 2023-12-26.
These files will be updated when the certificate renews.

NEXT STEPS:
- This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an authentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same certbot command before the certificate's expiry date.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/etc/letsencrypt/live/ 内に保存したと書いてありますが、こちらはシンボリックリンクで、実態は/etc/letsencrypt/archive/ 内にあります。
初期状態だと、ディレクトリの閲覧権限がないので、パーミッションを変更しておきます。

sudo chmod 755 /etc/letsencrypt/live
sudo chmod 755 /etc/letsencrypt/archive

発行された証明書

サーバ証明書
/etc/letsencrypt/archive/example.com/cert1.pem

中間CA証明書
/etc/letsencrypt/archive/example.com/chain1.pem

サーバ証明書と中間CA証明書が結合された証明書
/etc/letsencrypt/archive/example.com/fullchain1.pem

秘密鍵
/etc/letsencrypt/archive/example.com/privkey1.pem

秘密鍵の中身は、パーミッションを変更しないと確認できないので要注意です。

sudo chmod 644 /etc/letsencrypt/archive/example.com/privkey1.pem

さくらのレンタルサーバに独自SSLをインストールする

スクリーンショット 2023-09-27 14.54.09.png
コントロールパネルの「ドメイン/SSL」から「SSL証明書登録」画面を開き、「登録設定を始める SSL証明書の種類を選択」へ進みます。

スクリーンショット 2023-09-27 14.54.45.png
「SSL証明書の利用種類を選択」で「独自SSL」を選択します。

秘密鍵の登録

「秘密鍵の登録」から、Certbotで取得した下記のファイルをアップロードして登録します。
(※パーミッションに注意が必要です。)
/etc/letsencrypt/archive/example.com/privkey1.pem

証明書インストール

「証明書インストール」から、Certbotで取得した下記のファイルの中身をコピペして登録します。
/etc/letsencrypt/archive/example.com/cert1.pem

中間証明書インストール

「中間証明書インストール」で、Certbotで取得した下記のファイルの中身をコピペして登録します。
/etc/letsencrypt/archive/example.com/chain1.pem

確認

「SSL証明書の表示」の枠に証明書の内容が表示されていれば完了です。
ローカルのhostsファイルで名前解決して、サイトを確認してみましょう。

サーバ移転完了後、さくらの無料SSLに切り替える

Let's EncryptのSSL証明書の有効期限は3ヶ月です。
さくらの無料SSLを利用すると3ヶ月ごとに自動更新されますが、手動でLet's EncryptのSSL証明書をインストールした場合は、自動更新されないので、注意が必要です。
サーバ移転が完了したら、有効期限が切れる前に、さくらの無料SSLに切り替えておきましょう。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4