CentOS+ApacheにLet's Encryptを導入


Overview

Linux CentOS + ApacheLet's Encrypt を導入する方法を説明します。

Installation

Let's Encrypt は、手動でインストールすることもできますが、Cerbot という自動化サービスがありますのでこちらを利用します。

  • 書き込み可能な任意の場所に github から Cerbot をダウンロードします。
cd /usr/local
git clone https://github.com/certbot/certbot.git

Obtain a certificate

Cerbot を利用して、SSL証明書を取得します。

  • ダウンロードした、Cerbot のディレクトリに入り、SSL証明書を取得するコマンドを叩きます。
cd certbot
./certbot-auto certonly --webroot -w /var/www/vhosts/www.mydomain.com/html -d www.mydomain.com -m [email protected] -n --agree-tos
Option Description Value
-w キュメントルート /var/www/vhosts/www.mydomain.com/html
-d ドメイン名 www.mydomain.com
-m 連絡先のメールアドレス [email protected]
-n 対話入力をスキップする
--agree-tos 利用規約に同意する

※ キュメントルート、ドメイン名、連絡先のメールアドレスは、環境に合わせた任意の値を設定してください。

正常に証明書を取得できると、/etc/letsencrypt/live/www.mydomain.com/ 以下に次のファイルを確認できます。

cert.pem privkey.pem chain.pem fullchain.pem

Configure Apache

取得したSSL証明書をApacheサーバーに設定します。

  • 次のように /etc/httpd/conf/httpd.conf を編集します。
<VirtualHost *:443>
  ServerName www.mydomain.com
  DocumentRoot /var/www/html
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/www.mydomain.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/www.mydomain.com/privkey.pem
  SSLCACertificateFile /etc/letsencrypt/live/www.mydomain.com/chain.pem

  <Directory "/var/www/vhosts/www.mydomain.com/html">
    ... 省略 ...
  </Directory>
</VirtualHost>
  • Apache を再起動します。
service httpd restart

Update certificate

Let's Encrypt 証明書は、90日の有効期限しかない。

  • 次のコマンドで更新をします。
/usr/local/certbot/certbot-auto renew --post-hook "service httpd restart"

Configure Cron

手動で更新をするのは、手間なので、Cron を使って自動化します。

  • Cron の設定ファイルを作成します。
vim /etc/cron.d/letsencrypt
  • ファイルに以下を記述します。
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/developer/bin
00 01 * * 01 root /usr/local/certbot/certbot-auto renew --post-hook "service httpd restart"

PATH は、環境によって異なります。 echo $PATH で確認して置き換えてください。
Cron 設定の 00 01 * * 01 は、毎週月曜日の朝1時に実行するを意味します。

(参考に https://crontab.guru/)

  • Cron を再起動します。
service crond restart

以上になります。