Nginx + Let’s Encrypt のインストール

<景品表示法に基づく表記> 本サイトのコンテンツには、商品プロモーションが含まれている場合があります。

事前準備

インストールは自動化されていますので、動かすための必要パッケージをインストールをします。

# yum -y install git

Let’s Encryptのインストール

# cd /etc
# git clone https://github.com/letsencrypt/letsencrypt.git

証明書取得

既にWebサーバ環境が整っているのでオプション「webroot」にて実行します。ドキュメントルート直下に「.well-known/」というディレクトリが作成され必要な処理が実行されます。

# cd /etc/letsencrypt
# ./certbot-auto certonly --webroot \
 -w /usr/share/nginx/wordpress/ -d rin-ka.net \
 -m [email protected] \
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/rin-ka.net/fullchain.pem. Your cert will
   expire on 2017-01-19. To obtain a new or tweaked version of this
   certificate in the future, simply run certbot-auto again. To
   non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - If you lose your account credentials, you can recover through
   e-mails sent to [email protected].
 - 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

「Congratulations!」が表示されると完了です。

SSL証明書の適用

作成したSSL証明書をnginxに組み込みます。

# mkdir /etc/nginx/ssl
# cd /etc/nginx/ssl
# cp /etc/letsencrypt/live/rin-ka.net/fullchain.pem .
# cp /etc/letsencrypt/live/rin-ka.net/privkey.pem .

設定ファイル変更を変更します。

# cd /etc/nginx/conf.d/
# vi rin-ka.net.conf
server {
        listen  443 ssl  ;
        server_name     rin-ka.net;

        ssl_certificate /etc/nginx/ssl/fullchain.pem;
        ssl_certificate_key /etc/nginx/ssl/privkey.pem;
}

設定を反映します。

# systemctl reload nginx

SSL証明書の更新

Let’s Encrypt の証明書の有効期限は、90日間と短いため定期的に更新する必要があります。また、証明書が更新された場合はnginxに再読込を実施する必要があります。

SSL証明書の更新と、Nginxの設定再読込を実行するスクリプトを作成し、Cronで定期的にスクリプトを実行します。

# cd /etc/cron.monthly
# vi letsencrypt

#!/bin/sh
/etc/letsencrypt/letsencrypt-auto renew > /dev/null
systemctl reload nginx

# chmod +x letsencrypt

このSSL証明書更新コマンドは常に全ドメインが対象となるため、個々に更新したいケースには対応できません。個別更新をしたい場合は新規発行したときと同じオプションでコマンドを実行します。確認ダイアログが表示されるので「–non-interactive」オプションを与えて実行します。

更新処理がスキップされた時

有効期限に余裕がある際にコマンドを実行すると更新処理はスキップされます。

# /etc/letsencrypt/letsencrypt-auto renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/rin-ka.net.conf
-------------------------------------------------------------------------------
Cert not yet due for renewal

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/rin-ka.net/fullchain.pem (skipped)
No renewals were attempted.

更新処理が実行された時

有効期間の残りが30日未満の際にコマンドを実行すると更新処理が実行されます。

# /etc/letsencrypt/letsencrypt-auto renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Cert is due for renewal, auto-renewing...
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org Renewing an existing certificate Performing the following challenges:
http-01 challenge for rin-ka.net Waiting for verification...
Cleaning up challenges
Generating key (2048 bits): /etc/letsencrypt/keys/0007_key-certbot.pem
Creating CSR: /etc/letsencrypt/csr/0007_csr-certbot.pem

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/rin-ka.net.conf
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
new certificate deployed without reload, fullchain is /etc/letsencrypt/live/rin-ka.net/fullchain.pem
-------------------------------------------------------------------------------

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/rin-ka.net/fullchain.pem (success)