[サーバーノウハウ2]レッツエンクリプトでhttps化する手順(nginx)

実行環境

今回私が使用する実行環境は以下の通りです

os : ubuntu 20.04

サーバーソフト : nginx

snapdをインストール

snapedをインストールするコマンドは以下の通りです。

sudo apt install snapd

snapdでcertbotをインストール

snapdでcertbotをインストールするコマンドは以下の通りです。

sudo snap install --classic certbot

正しくインストールされていれば、以下のコマンドでバージョンが表示されるはずです。

certbot --version

certbotでhttps化

まずは以下のコマンドを実行しましょう

sudo certbot --nginx

このコマンドは、nginxの設定に基づいてhttps化の設定を行うコマンドです。

上記のコードを実行すると、下記のようにメッセージが出ます。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel):

意味は「/var/log/letsencrypt/letsencrypt.logにログを出力しました。管理者のメールアドレスを入力してください」です。

ですので、自身のメールアドレスを入力しましょう。

メールアドレスを入力したら下記のような表示がされると思います。

Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf. 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.

これはレッツエンクリプトパートナーにメールアドレスを公開していいか聞かれています。

拒否しても問題ないので、お好きな方をお選びください。

入力したら以下の表示がされると思います。

Account registered.

Which names would you like to activate HTTPS for?
We recommend selecting either all domains, or all domains in a VirtualHost/server block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: hoge@fuga.piyo
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):

これはnginxに設定されているドメインのうち、https化したいドメインを数字で指定してくださいと言われています。

今回の例では「1」を入力すればいいということです。

入力したら以下の表示がされると思います。

Requesting a certificate for hoge@fuga.piyo

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/hoge@fuga.piyo/fullchain.pem
Key is saved at: /etc/letsencrypt/live/hoge@fuga.piyo/privkey.pem
This certificate expires on 2024-08-07.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for hoge@fuga.piyo to /etc/nginx/sites-enabled/default
Congratulations! You have successfully enabled HTTPS on https://hoge@fuga.piyo

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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/hoge@fuga.piyo/fullchain.pemに証明書を保存しました。秘密鍵は/etc/letsencrypt/live/hoge@fuga.piyo/privkey.pemに保存しました。これらのファイルは証明書を更新する時に、バックグラウンドで自動的に更新されるよう、スケジュールタスクを設定しました。https://hoge@fuga.piyoで認証したサイトにアクセスできます。」です。

ちなみに、httpで接続してもhttpsにリダイレクトされるようになってました。

便利ですね。

本来は自分の手で、リダイレクトされるようにnginxの設定ファイルを書き換えなくてはいけませんからね。

まとめ

いかがだったでしょうか?。

nginxを使う場合は簡単にhttps化出来てびっくりしました。

本来は公開鍵や秘密鍵の話が絡んでくるのですが、今はかなり手順が簡略化されているんですね。

皆さんがhttps化する際の参考になれば嬉しいです。

ではまた!

次の記事

準備中