アプリケーション開発ポータルサイト
ServerNote.NET
Amazon.co.jpでPC関連商品タイムセール開催中!
カテゴリー【ApachePostfixDebian
【Let's Encrypt】無料SSLサーバー証明書および外部からの確認方法【Debian】
POSTED BY
2023-10-31

昔はVeriSignやGlobalSignといった大手の発行するお高い証明書じゃないとエラー扱いになったものだが、今ではこの無料の証明書が現役のPC/モバイルブラウザほぼすべてで問題なく使えるようになった。

インストール

# Debian Stretch の場合

/etc/apt/sources.list を編集

deb http://ftp.debian.org/debian stretch-backports main

を加えて、

apt update
apt -t stretch-backports install certbot

# Debian Buster の場合

aptをいじる必要なし、そのまま

apt install certbot

証明書を取得するには、ドメイン(サーバー名)認証が必要になる。
certbotを起動すると、まずそのマシンの80番ポートでリッスンを開始する。
よって、80番ポートをリッスンしてるApache等は落としておかなくてはならない。

/etc/init.d/apache2 stop

Let's Encryptの中央サーバーがそのサーバーのIPアドレスの80番ポートを叩いてくるので、
ルーターの設定でそのサーバーに到達するよう80番を開けておかなくてはいけない。
そしてその80番ポートを待ち受けているのは上記certbotでなくてはいけない。

この条件を満たせば、晴れてそのサーバー名の証明書が発行される。

certbot certonly --standalone -d www.example1155.jp

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): webmaster@example1155.jp

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A

-------------------------------------------------------------------------------
Would you be willing 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
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.example1155.jp
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.example1155.jp/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.example1155.jp/privkey.pem
   Your cert will expire on 2018-12-16. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - 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/www.example1155.jp

以下、

cert.pem # 証明書本体
chain.pem # 中間証明書
fullchain,pem # cert.pem + chain.pem
privkey.pem # 秘密鍵

となっているので、それぞれSSL通信させたいサーバソフトに設定する。

Apacheの場合(2.4.7以前のバージョン)

SSLCertificateFile "/etc/letsencrypt/live/www.example1155.jp/cert.pem"
SSLCertificateChainFile "/etc/letsencrypt/live/www.example1155.jp/chain.pem"
SSLCertificateKeyFile "/etc/letsencrypt/live/www.example1155.jp/privkey.pem"

Apacheの場合(2.4.8以降のバージョン)

SSLCertificateFile "/etc/letsencrypt/live/www.example1155.jp/fullchain.pem"
SSLCertificateKeyFile "/etc/letsencrypt/live/www.example1155.jp/privkey.pem"

ちゃんとSSL通信できるか、外のサーバーからopensslコマンドで確認する。
もちろんルーター、Apacheともに443番を開けてリッスンし待ち受けておく。
外のサーバーからこのように打つ。

openssl s_client -showcerts -connect www.example1155.jp:443 | openssl x509 -text

上記のcert.pem,chain.pem証明書がずらずら表示されれば正当にSSL通信されている。

Validity
Not Before: Oct 11 12:13:32 2019 GMT
Not After : Jan  9 12:13:32 2020 GMT

などと表示されており、このNot Afterが、証明書の有効期限である。

Postfixの場合

/etc/postfix/main.cf

smtpd_tls_cert_file = /etc/letsencrypt/live/www.example1155.jp/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/www.example1155.jp/privkey.pem

SMTPSのポートは465番である。ルーターをそのように開放しPostfixは465を待ち受ける。
外のサーバーからポートを変えてopensslで確認する。

openssl s_client -showcerts -connect www.example1155.jp:465 | openssl x509 -text

Dovecotの場合

/etc/dovecot/conf.d/10-ssl.conf
ssl_cert = </etc/letsencrypt/live/www.example1155.jp/fullchain.pem
ssl_key = </etc/letsencrypt/live/www.example1155.jp/privkey.pem

Let's Encryptの証明書は有効期限が短いので自動更新がかかせず、certbotを走らせると自動的に

/etc/cron.d/certbot

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

が設定される。よく他のサイトではこれを編集してApacheやPostfixを一時停止・再開するコマンドを付与している例が見られるが、
当サイトではこのデフォルトcronのまま何もしなくてもApacheとPostfixは問題なく更新された証明書を常にロードしてくれていることが確認できている。

※本記事は当サイト管理人の個人的な備忘録です。本記事の参照又は付随ソースコード利用後にいかなる損害が発生しても当サイト及び管理人は一切責任を負いません。
※本記事内容の無断転載を禁じます。
【WEBMASTER/管理人】
自営業プログラマーです。お仕事ください!
ご連絡は以下アドレスまでお願いします★

☆ServerNote.NETショッピング↓
ShoppingNote / Amazon.co.jp
☆お仲間ブログ↓
一人社長の不動産業務日誌
【キーワード検索】