2024年06月02日作成
certbotを使ってLet'sEncryptでSSL証明を取得する
1.はじめに
2.certbotのインストール
3.証明書の取得
4.certbotの自動更新
5.ワイルドカードでの指定や複数ドメインでの指定
6.取得した証明書の確認と削除
7.証明書取得時の実行結果例
1.はじめに
今やどこのサイトでもSSL証明による暗号化が当たり前になり、メールやゲームサーバまで暗号化を求められます。
そんな中、無償でドメイン認証でのSSL証明書を取得できるLet'sEncryptは多くの人々に利用されており、
このHPでも利用しています。
私自身、必要に迫られてcertbotを知った口ですが、改めて勉強がてらいろいろ調べてみると、
間違ったまま使ってしまっていたこともあったので備忘録がてら記事にします。
2.certbotのインストール
certbotのインストール方法ですが、公式ページを参照すると
意外と出回っているインストール方法が間違っているかのような書き方をしています。
Ubuntuでcertbotをインストールするといえばaptコマンドでインストールするのが一般的なようですが、
公式ページではsnapでインストールせよとしているようです。
では具体的なインストール手順です。
#snapのインストール(Ubuntuであればインストール済みの可能性が高いが念のため)
$ sudo apt install snapd
#cerbotのインストール
$ sudo snap install --classic certbot
#certbotの事前確認
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
最後のコマンドでエラーが出なければインストール完了です。
3.証明書の取得
SSL証明書の取得時には以下の項目を質問されます。
・メールアドレス
・利用規約への同意
・DMの要否
・ドメイン名
これらの質問事項については、DMの要否以外はSSL証明取得コマンドにオプションを付けることで、
あらかじめ指定しておくことが可能です。
#メールアドレスの指定
-m example@mail.com
#利用規約への同意
--agree-tos
#ドメイン名
-d examlpe.com
実際のSSL証明取得時には、HTTPの80番ポートを利用します。
その際にどのHTTPサーバ機能を使うかによってSSL証明取得コマンドが以下のように異なります。
3.1.Apacheを利用する場合
すでに稼働中のApacheサーバがある場合はこちらを選択します。
また、SSL証明の取得と同時にApacheの設定もいい感じ変えてくれるコマンドがあるのですが、
今回は勝手に変わってしまうと嫌なのでSSLの証明の取得のみとします。
なおApacheを用いる場合は、SSL証明取得後にHTTPSになっているか自動的に確認するため80番ポートと443番ポートを開けておきます。
実行コマンドは以下の通りとなります。
#対話型で必要事項を入力する場合
$ sudo certbot certonly --apache
#オプションで質問事項を事前に指定する場合
$ sudo certbot certonly --apache -m example@mail.com --agree-tos -d example.com
3.2.nginxを利用する場合
すでに稼働中のNginxサーバがある場合はこちらを選択します。
基本的にはApacheと同様の設定方法です。
そのため事前に80番ポートと443番ポートを開けておく点も同様です。
実行コマンドは以下の通りとなります。
#対話型で必要事項を入力する場合
$ sudo certbot certonly --nginx
#オプションで質問事項を事前に指定する場合
$ sudo certbot certonly --nginx -m example@mail.com --agree-tos -d example.com
3.3.その他の場合
メールサーバなどで、稼働中HTTP/HTTPSサーバがない場合はこちらを選択します。
前述のパターンと異なる点としては、HTTPSのチェックが入らないので443番ぽーとは開けておく必要がなく、
80番ポートのみ開けておけば問題ないです。
実行コマンドは以下の通りとなります。
#対話型で必要事項を入力する場合
$ sudo certbot certonly --standalone
#オプションで質問事項を事前に指定する場合
$ sudo certbot certonly --standalone -m example@mail.com --agree-tos -d example.com
取得しした証明書ファイルは以下のように、実行結果表示の箇所に保存されます。
各種サーバにて証明書ファイルとしてこれらを指定してあげれば完了です。
~中略~
Successfully received certificare.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem
~中略~
4.certbotの自動更新
上記手順で証明書を取得した場合、証明書ファイルの自動更新が可能です。
※Certbotでは証明書の有効期限は90日です。
公式で案内されている自動更新のテストは以下のコマンドで可能です。
$ sudo certbot renew --dry-run
--dry-roundオプションがテスト実行のオプションなので、実際に更新する際には以下のコマンドになります。
$ sudo certbot renew
ようはこのコマンドを1か月に1回とかの頻度で実行すれば、更新することが可能です。
Cronでスケジューリングすればいいので以下のように設定します。
なお、80番ポートを使うサービスが動いている場合は事前にサービスを止めてから更新させる必要があります。
更新後には新しい証明書を読み込む必要があるため、利用サービスを再起動させる必要があります。
$ sudo crontab -e //このコマンドでコマンドのスケジューラを編集
//初回は利用するエディタを選択
//以下の内容をページの最後に追記
00 04 1 * * certbot renew //毎月1日朝4時に実行
00 04 1 * * systemctl stop nginx.service && certbot renew && systemctl restart nginx.service
//更新前にhttpサービスを停止する例
00 04 1 * * certbot renew && systemctl restart postfix.service && systemctl restart dovecot.service
//証明書更新後にメールサービスを再起動する例
$ sudo systemctl restart cron //cronサービスを再起動させて設定を有効化
5.ワイルドカードでの指定や複数ドメインでの指定
一つの証明書ファイルで複数ドメインのドメイン認証を行う方法として、
"*.misumi-net.com"のようにワイルドカードですべてのサブドメインをまとめて指定する方法や、
登録時に複数ドメインを並べて指定する方法などがあります。
なお、この場合自動的に証明書を更新するにはスクリプトを組むなどの必要があるため、ハードルが高くなります。
5.1.ワイルドカードでのドメイン指定
ワイルドカードでのドメイン指定をする場合は以下の手順で証明書を取得可能です。
まずは以下のコマンドを実行します。
$ sudo certbot certonly --manual
対話型で各必要事項を入力していきます。ドメイン名の指定の指示があった場合には、"*.misumi-net.com"のように指定します。
以下のような文面が出てきたら、対象ドメインのDNSレコード追加します。
test@test:~$ sudo certbot certonly --manual
[sudo] password for test:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Please enter the domain name(s) you would like on your certificate (comma and/or
space separated) (Enter 'c' to cancel): *.misumi-net.com
Requesting a certificate for *.misumi-net.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:
_acme-challenge.misumi-net.com.
with the following value:
iqMMmEArO8xtpwLfnjSoO7GctzuSAP2************
Before continuing, verify the TXT record has been deployed. Depending on the DNS
provider, this may take some time, from a few seconds to multiple minutes. You can
check if it has finished deploying with aid of online tools, such as the Google
Admin Toolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.misumi-net.com.
Look for one or more bolded line(s) below the line ';ANSWER'. It should show the
value(s) you've just added.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
上記の場合は以下の内容でDNSレコードを追加します。
ホスト名:_acme-challenge.misumi-net.com.
タイプ:TXT
TTL:任意の値(デフォルトなら3600?)
値:iqMMmEArO8xtpwLfnjSoO7GctzuSAP2************
DNSレコードを設定後、5分程度以上待って(DNSの浸透待ち)からEnterを押します。
”Successfully received certificate.”と表示されれば、認証の完了および証明書の生成が成功となります。
また、これらの操作は更新のたびに実行する必要があるので結構手間です。
5.2.複数ドメインでのドメイン指定
複数ドメインでのドメイン指定をする場合は、ワイルドカードの時と同様に以下の手順で証明書を取得可能です。
まずは以下のコマンドを実行します。
$ sudo certbot certonly --manual
対話型で各必要事項を入力していきます。ドメイン名の指定の指示があった場合には、
"test.misumi-net.com test2.misumi-net.com"のように指定します。
この時"test.misumi-net.com test2.misumi-net.com"の両ドメインが、certbotを実行しているマシンと同一であればそのまま証明書取得が進みます。
なお、異なるマシンのドメインを取得する場合には以下のような文面が表示されますので、テキストファイルの設置が必要となります。
test@test:~$ sudo certbot certonly --manual
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Please enter the domain name(s) you would like on your certificate (comma and/or
space separated) (Enter 'c' to cancel): test.misumi-net.com test2.misumi-net.com
Requesting a certificate for test.misumi-net.com and test2.misumi-net.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create a file containing just this data:
weP5-kJlvu1AVuy9-GJZqEFBnAv4MDG81YgHvU4aszI.ScwyX4ZSiuP_8Fdkwy4mP5dTsQ7luXDaHQHL
wTisz50
And make it available on your web server at this URL:
http://test.misumi-net.com/.well-known/acme-challenge/weP5-kJlvu1AVuy9-GJZqEFBnA
v4MDG81YgHvU4aszI
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
上記の場合、以下の場所に以下の内容のテキストファイルを設置後Enterを押します。
設置場所URL:http://test.misumi-net.com/.well-known/acme-challenge/
テキストファイル名(拡張子はつけない):weP5-kJlvu1AVuy9-GJZqEFBnAv4MDG81YgHvU4aszI
テキストファイルの中身(値):weP5-kJlvu1AVuy9-GJZqEFBnAv4MDG81YgHvU4aszI.ScwyX4ZSiuP_8Fdkwy4mP5dTsQ7luXDaHQHLwTisz50
”Successfully received certificate.”と表示されれば、認証の完了および証明書の生成が成功となります。
また、同一ホスト以外のドメインが含まれる場合、テキストファイルの設置作業は更新のたびに実行する必要があります。
6.取得した証明書の確認と削除
certbotで取得した証明書ファイルについては以下のコマンドで確認が可能です。$ sudo certbot certificates
"test.misumi-net.com"と"test2.misumi-net.com"の2つのドメイン証明書がある場合の実行例
test@test:~$ sudo certbot certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: test.misumi-net.com
Serial Number: 47740fb7bbd7507193149e00944********
Key Type: ECDSA
Domains: test.misumi-net.com
Expiry Date: 2024-08-31 03:58:54+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/test.misumi-net.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/test.misumi-net.com/privkey.pem
Certificate Name: test2.misumi-net.com
Serial Number: 41557e539fa668a8f512e266884********
Key Type: ECDSA
Domains: test2.misumi-net.com
Expiry Date: 2024-08-31 03:59:15+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/test2.misumi-net.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/test2.misumi-net.com/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
test@test:~$
取得済みの証明書ファイルを削除する場合は以下のコマンドで削除可能です。
$ sudo certbot delete --cert-name test.misumi-net.com
"test.misumi-net.com"の証明書を削除した場合の実行例
test@test:~$ sudo certbot delete --cert-name test.misumi-net.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certificate(s) are selected for deletion:
* test.misumi-net.com
WARNING: Before continuing, ensure that the listed certificates are not being
used by any installed server software (e.g. Apache, nginx, mail servers).
Deleting a certificate that is still being used will cause the server software
to stop working. See https://certbot.org/deleting-certs for information on
deleting certificates safely.
Are you sure you want to delete the above certificate(s)?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Deleted all files relating to certificate test.misumi-net.com.
test@test:~$
7.証明書取得時の実行結果例
参考に証明書取得時のログを載せておきます。※メールアドレスは適当、ドメインはテスト用を利用
test@misumi-net:~$
test@misumi-net:~$ sudo certbot certonly --apache
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): example@mail.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: n
Account registered.
Please enter the domain name(s) you would like on your certificate (comma and/or
space separated) (Enter 'c' to cancel): test.misumi-net.com
Requesting a certificate for test.misumi-net.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/test.misumi-net.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/test.misumi-net.com/privkey.pem
This certificate expires on 2024-08-31.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
test@misumi-net:~$
test@misumi-net:~$
test@misumi-net:~$ sudo certbot certonly --apache -m example@testmail.com --agree-tos -d test2.misumi-net.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for test2.misumi-net.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/test2.misumi-net.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/test2.misumi-net.com/privkey.pem
This certificate expires on 2024-08-31.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
test@misumi-net:~$
<前へ戻る