ヘルプ:Let's Encrypt から SSL/TLS サーバー証明書を取得(AlmaLinux)
メインページ > Help:目次 > Help:自宅サーバー構築(AlmaLinux) > Let's Encrypt から SSL/TLS サーバー証明書を取得(AlmaLinux)
- バーチャルホスト構築を前提にしてます。
- https化かつwwwなしに統一する。
- 偶にしか行わないので、忘れないように書いておく。
Let's Encrypt から 発行料 無料の SSL/TLS サーバー証明書を取得します。
Let's Encrypt は Linux Foundation の協業プロジェクトで、Web 全体の安全性を改善することをミッションに掲げているとのことです。
発行料 無料 とはいえ、あやしいものではありません。
Let's Encrypt の詳細は公式サイトを参照ください。
Let's Encrypt では 一般的な ドメイン認証 (DV) の証明書を無料で発行しています。
無料ですが Let's Encrypt の中間証明書は、大手認証局 (CA) のルート証明書によってクロス署名されているため、多くの主要ブラウザ等々で信頼済みとして扱われます。
なお、一回の作業で得られる証明書の有効期限は 90日です。よって、90日以内に更新作業を再度実施する必要があります。
メインホスト設定
メインホスト用ドキュメントルートディレクトリ作成
- ドメイン名で作成。
[root@host4 ~]# mkdir /var/www/html/awnet.jp
ディレクトリの所有者変更
- ドメイン名の所有者変更。
[root@host4 ~]# chown -R sysop. /var/www/html/awanet.jp/
メインホスト設定ファイル作成
[root@host4 ~]# vi /etc/httpd/conf.d/virtualhost-awanet.jp.conf <VirtualHost *:80> ServerName awanet.jp ServerAlias www.awanet.jp DocumentRoot /var/www/html/awanet.jp </VirtualHost>
Apache設定反映
[root@host4 ~]# systemctl reload httpd
Certbotクライアントインストール
証明書を取得するためのツール Certbot クライアントをインストールします。
[root@host4 ~]# snap install certbot --classic certbot 1.15.0 from Certbot Project (certbot-eff✓) installed
/snap/bin にインストールされたので、使いやすいように /usr/bin/certbot としてシンボリックリンクを作成。
[root@host4 ~]# ln -s /snap/bin/certbot /usr/bin/certbot
バージョンを確認します。
[root@host4 ~]# certbot --version certbot 1.15.0
ドメイン名に対するサーバー証明書取得
Certbotではサーバー名の認証をWeb経由で行うため、サーバー証明書に指定するサーバー名で外部からWebアクセスできるようにしておく必要がある。
ここでは、www 有る無し両方で使える SSL/TLS サーバ証明書を取得します。
テスト用のサーバ証明書取得
Let's Encrypt 認証局では、ドメイン名あたりの SSL/TLS サーバ証明書の発行枚数について、制限を設けています。そのため、試験段階においては --test-cert オプションを付けて、テスト用のサーバで使ってみることをお勧めします。
※テスト用のサーバで発行したテスト用の証明書は、ブラウザにおいてセキュリティ警告が表示され、信頼された証明書として扱われません。
[root@host4 ~]# certbot certonly --webroot -w /var/www/html/awanet.jp -d awanet.jp -d www.awanet.jp -m *****@sudachi.jp --test-cert
サーバー証明書取得
[root@host4 ~]# certbot certonly --webroot -w /var/www/html/awanet.jp -d awanet.jp -d www.awanet.jp -m *****@sudachi.jp --agree-tos ・ ・ ・ What would you like to do? ------------------------------------------------------------------------------- 1: Keep the existing certificate for now 2: Renew & replace the cert (limit ~5 per 7 days) ------------------------------------------------------------------------------- Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 ← 2を選択(証明書を更新し、交換する)
- [Congratulations] と表示されれば成功
- メッセージ中に記載の通り [/etc/letsencrypt/live/(FQDN)/] 配下に証明書が取得されている
- cert.pem ⇒ SSLサーバー証明書(公開鍵含む)
- chain.pem ⇒ 中間証明書
- fullchain.pem ⇒ cert.pem と chain.pem が結合されたファイル
- privkey.pem ⇒ 公開鍵に対する秘密鍵
ポート番号を指定
HTTPやHTTPSの待ち受けポートを変更している場合は、以下のオプションでポート番号を指定します。
- HTTPのポート番号指定
--http-01-port 8080
- HTTPSのポート番号指定
--tls-sni-01-port 4443
証明書をまとめて更新
取得済みの証明書をまとめて更新する場合は、[renew] サブコマンドを使用します。
有効期限が 30日未満の証明書が全て更新されます。
有効期限の残り日数に関わらず更新したい場合は [--force-renew] を合わせて指定します。
ただし、当例のように Snapd からインストールした [certbot] クライアントであれば、更新の手動実行は必要ありません。更新は自動で実行されます。
- ただし、この機能では、証明書を使用するWebサーバーソフトウェアの再起動は行われません。
Snapd の Certbot パッケージはタイマーが付属
[root@host4 ~]# systemctl status snap.certbot.renew.timer ● snap.certbot.renew.timer - Timer renew for snap application certbot.renew Loaded: loaded (/etc/systemd/system/snap.certbot.renew.timer; enabled; vendor preset: disabled) Active: active (waiting) since Wed 2021-05-19 10:21:15 JST; 10min ago Trigger: Wed 2021-05-19 13:50:00 JST; 3h 18min left 5月 19 10:21:15 host3.awanet.jp systemd[1]: Started Timer renew for snap application certbot.renew.
デフォルトでは以下のように毎日 2回 [renew] 実行
[root@host4 ~]# systemctl cat snap.certbot.renew.timer # /etc/systemd/system/snap.certbot.renew.timer [Unit] # Auto-generated, DO NOT EDIT Description=Timer renew for snap application certbot.renew Requires=var-lib-snapd-snap-certbot-1150.mount After=var-lib-snapd-snap-certbot-1150.mount X-Snappy=yes [Timer] Unit=snap.certbot.renew.service OnCalendar=*-*-* 08:49 OnCalendar=*-*-* 13:50 [Install] WantedBy=timers.target
手動で更新する場合
[root@host4 ~]# certbot renew
Webサーバーの自動的再起動
- 日数不足で動作の確認が出来てません。
[root@host4 ~]# vi /etc/letsencrypt/renewal-hooks/deploy/web_restart.sh #!/bin/bash systemctl restart httpd
[root@host4 ~]# chmod 755 /etc/letsencrypt/renewal-hooks/deploy/web_restart.sh
最近のLet's Encrypt(certbot)では証明書更新時の追加動作に関する仕組みが拡張され、/etc/letsencrypt/renewal-hooks/以下のディレクトリ(pre, post, deploy)以下に実行ファイルを置くことで、証明書更新の処理を行う際にこれを実行してくれる様になりました。
ディレクトリ | 処理のトリガー |
---|---|
pre | certbotが全部の証明書更新の処理を始める前に証明書の更新の有無に関わらず必ず実行されます |
post | certbotが全部の証明書更新の処理を終わった後で証明書の更新の有無に関わらず必ず実行されます |
deploy | certbotが個々の証明書更新の処理を終わった後で証明書の更新された場合にだけ実行されます |
ファイルの場所
設定ファイルの場所
[root@host4 ~]# ll /etc/letsencrypt/renewal/ 合計 4 -rw-r--r-- 1 root root 617 5月 1 04:01 awajp.com.conf -rw-r--r-- 1 root root 541 5月 18 10:32 awanet.jp.conf
証明書の場所(シンボリックリンク)
[root@host4 ~]# ll /etc/letsencrypt/live/ 合計 0 drwxr-xr-x 2 root root 93 5月 1 04:01 awajp.com drwxr-xr-x 2 root root 93 5月 18 10:32 awanet.jp [root@host4 ~]# ll /etc/letsencrypt/live/awanet.jp/ 合計 4 -rw-r--r-- 1 root root 692 5月 18 10:32 README lrwxrwxrwx 1 root root 33 5月 18 10:32 cert.pem -> ../../archive/awanet.jp/cert1.pem lrwxrwxrwx 1 root root 34 5月 18 10:32 chain.pem -> ../../archive/awanet.jp/chain1.pem lrwxrwxrwx 1 root root 38 5月 18 10:32 fullchain.pem -> ../../archive/awanet.jp/fullchain1.pem
l rwxrwxrwx 1 root root 36 5月 18 10:32 privkey.pem -> ../../archive/awanet.jp/privkey1.pem
証明書の場所
[root@host4 ~]# ll /etc/letsencrypt/archive/ 合計 8 drwxr-xr-x 2 root root 4096 5月 1 04:01 awajp.com drwxr-xr-x 2 root root 83 5月 18 10:32 awanet.jp
[root@host4 ~]# ll /etc/letsencrypt/archive/awanet.jp 合計 20 -rw-r--r-- 1 root root 1830 5月 18 10:32 cert1.pem -rw-r--r-- 1 root root 3749 5月 18 10:32 chain1.pem -rw-r--r-- 1 root root 5579 5月 18 10:32 fullchain1.pem -rw------- 1 root root 1708 5月 18 10:32 privkey1.pem
設定ファイル
[root@host4 ~]# ll /etc/letsencrypt/ 合計 8 drwx------ 4 root root 86 2月 3 19:28 accounts drwx------ 5 root root 62 5月 18 10:32 archive drwxr-xr-x 2 root root 4096 5月 18 10:32 csr drwx------ 2 root root 4096 5月 18 10:32 keys drwx------ 5 root root 76 5月 18 10:32 live drwxr-xr-x 2 root root 77 5月 18 10:32 renewal drwxr-xr-x 5 root root 43 12月 10 07:58 renewal-hooks
webrootの設定
webrootのディレクトリ名を記録のために書いておく。
.well-known
証明書を失効させる
最新の certbot コマンドには delete オプションが追加され、まとめてファイルを消せるようになりました。
[root@host4 ~]# certbot delete Saving debug log to /var/log/letsencrypt/letsencrypt.log Which certificate(s) would you like to delete? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: eeljp.net 2: sudachi.xyz 3: sudachinet.jp - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel): 2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Deleted all files relating to certificate sudachi.xyz. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -