Let’s Encrypt設定
ドメイン名を取得してから、Let’s Encryptの自動設定を行うまでを書いてみようかと思います。
一度ドメインとSSL証明書、取ってみたかったんです。
環境
- OS: Centos7
- Webサーバ: nginx + php + wordpress
DNSレコード設定
DNS関連の設定は以下の通りです。
ドメイン: tech-law-pyscho.info Aレコード: totokun CNAMEレコード: www -> totokun.tech-law-pyscho.info
以下のような設定にしてあります。
以下を参考にしました。
https://www.kagoya.jp/howto/webhomepage/lets-encrypt/
iptablesは割愛
port 443を通す設定は既に行っているので、割愛しました。
Nginx (Webサーバー)設定
server_nameをドメイン名に変更しておきます。
$ cat /etc/nginx/conf.d/wp.conf | grep server_name server_name www.tech-law-pyscho.info; $ systemctl reload nginx
Certbotクライアントソフトウェアのインストール
以下でCertbotプログラムをインストールします。
$ sudo yum install --enablerepo=epel certbot-nginx
以下のコマンドでSSL証明書を取得します。
$ sudo certbot –-nginx -d www.tech-law-pyscho.info ### メールアドレス登録 Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator nginx, Installer nginx Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): ke.tanino@gmail.com ### 規約 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-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A
とやっていたら、以下で失敗しました。nginx.confの中で日本語を使用すると以下のエラーが出るらしいです。
確かにpython2で日本語文字列の読み込みをすると、よく出るエラーメッセージですね。。
An unexpected error occurred: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 15: ordinal not in range(128) Please see the logfiles in /var/log/letsencrypt for more details.
nginxの設定を変更しようとしたところで落ちているようです。python2で作ってるからなんじゃ。。python3にしようよ。。。
気を取り直して、設定ファイルから日本語を削除して、再度実行。以下続きです。
### 自動的にリダイレクトするかどうかの設定 ### ここではリダイレクトすることにした。 Deploying Certificate to VirtualHost /etc/nginx/conf.d/wp.conf Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel):2 Congratulations! You have successfully enabled https://www.tech-law-pyscho.info You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=www.tech-law-pyscho.info - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/www.tech-law-pyscho.info/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/www.tech-law-pyscho.info/privkey.pem Your cert will expire on 2019-08-28. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - 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
ここまで行くと成功です。
https://www.tech-law-pyscho.info
にアクセスできたことを確認しました。
証明書は以下になります。
cronで回す
このままだと有効期限が3ヶ月で切れるので、cron設定をしておきます。
crontab -eで以下の設定を行います。renewとすると、30日以内で切れる場合は自動的に更新してくれるらしいです。
30 2 * * * /usr/bin/certbot renew --quiet
一応、dry-runして問題なさそうなのは確認しました。
# /usr/bin/certbot renew --dry-run (略) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates below have not been saved.) Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/www.tech-law-pyscho.info/fullchain.pem (success) ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates above have not been saved.) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ここまでで、記録しながら、2時間ぐらいなので、簡単でした。