CentOS 7でLet’s EncryptのSSL証明書を使用する

CentOS 7のサーバでオレオレ証明書でWebやメールサーバを運用していましたが、最近面倒くさくなってきました。

PCやMacは警告を二度と表示しないようにできますが、AndroidのChromeは証明書の警告をスキップする方法が見当たらず、アプリを終了すると毎回警告されます。

最近は非常に安い証明書もあるので、ちゃんとした証明書を買おうかとも思いましたが、VPSの使用料とドメイン代以外にあまりお金をかけたくありません。

前から名前を知ってはいたLet’s Encryptを使用してみることにします。
3ヶ月くらいで更新処理が必要という話を聞いていたので、面倒くさそうなので導入を見送ったような記憶がありますが、今は設定も更新も簡単にできるようです。

さくらインターネットのさくらのナレッジにあるnginx用の手順を参考に進めます。
Let’s EncryptのSSL証明書で、安全なウェブサイトを公開
nginx用ですが、Apacheでも基本的には同じですね。

すでにオレオレ証明書で、Apache、Postfix、Dovecotを設定済みなので、これらの設定については割愛します。
既存環境にLet’s EncryptのSSL証明書を使用する手順です。

certbot-autoをダウンロード

curl https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto
chmod 700 /usr/bin/certbot-auto

ertbot-autoで証明書を作成

certbot-auto certonly \
--webroot \
-w /var/www/html \
-d 証明書のドメイン \
--email メールアドレス

“-w /var/www/html”の部分はドキュメント・ルートのパスなので、VirtualHostの場合はVirtualHostのドキュメントルートにします。

利用規約(Terms of Service)が表示されるので、”A”を入力します。

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

Electronic Frontier Foundation にアドレスを共有するか聞かれるので、とりあえず”N”を入力します。

-------------------------------------------------------------------------------
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 EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o:

Apacheの設定を変更します。
/etc/httpd/conf/httpd.conf、SNIの場合はVirtualHostのSSLの証明書の設定を変更します。

SSLCertificateFile /etc/letsencrypt/live/証明書のドメイン/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/証明書のドメイン/privkey.pem

Apacheを再起動します。

systemctl restart httpd

Postfixの設定も変更します。
/etc/postfix/main.cf

smtpd_tls_cert_file = /etc/letsencrypt/live/証明書のドメイン/cert.pem
smtpd_tls_key_file = /etc/letsencrypt/live/証明書のドメイン/privkey.pem

Postfixを再起動します。

systemctl restart postfix

Dovecotの設定も変更します。
/etc/dovecot/conf.d/10-ssl.conf

ssl_cert = </etc/letsencrypt/live/証明書のドメイン/cert.pem
ssl_key = </etc/letsencrypt/live/証明書のドメイン/privkey.pem

Dovecotを再起動します。

systemctl restart dovecot

放っておくと3ヶ月で有効期限が切れてしまうので、証明書の自動更新設定を行います。
cron.dに下記の内容を登録します。viでやりました。
vi /etc/cron.d/certupdate

50 3 * * 0 root /usr/bin/certbot-auto renew --post-hook "systemctl restart httpd;systemctl restart postfix;systemctl restart dovecot" >/dev/null 2>&1

–post-hookを指定して証明書の更新時にApacheとPostfixとDovecotを再起動し、証明書がアップデートされるように設定しています。