LoginSignup
8
9

More than 5 years have passed since last update.

Lets’ Encrypt + certbot + NginxでSSL

Last updated at Posted at 2017-03-11

概要

遺伝子の情報サイト
https://ge-no.com/
ってサービスのSSL証明書が切れたのですが、毎度毎度SSL証明書にお金掛けるのもったいないのもありLet's EncryptでSSL証明書入れてみました。
ちなみに今まではRapid SSLのワイルドカード証明書を使っていました。
*.ge-no.comで年間6万円です。GeoTrustでワイルドカード20万円とかに比べると安いけどやっぱり高い。

導入環境

OS: CentOS 6.6 64bit
https://ge-no.com
Nginx + Unicorn

Certbotインストール

CentOS7

sudo yum install epel-release
sudo yum install certbot

CentOS6

sudo yum install epel-release
cd /usr/local/src
sudo wget https://dl.eff.org/certbot-auto
sudo chmod a+x certbot-auto

証明書取得

あらかじめファイヤウォール等の80番と443番ポートを開けておく必要がある。
ステージングサーバなどが認証サーバからつながらない事があるので注意
standaloneプラグインにより実施するのでこいつが80/443ポートを使用するためnginxを殺しておく

sudo ./certbot-auto certonly --standalone

Creating virtual environment...
Installing Python packages...
Installation succeeded.
/root/.local/share/letsencrypt/lib/python2.6/site-packages/cryptography/__init__.py:26: DeprecationWarning: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of cryptography will drop support for Python 2.6
  DeprecationWarning
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):hoge@genesis-healthcare.jp

-------------------------------------------------------------------------------
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: 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 EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: N
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel):ge-no.com
Obtaining a new certificate
Performing the following challenges:
tls-sni-01 challenge for ge-no.com

-------------------------------------------------------------------------------
Could not bind TCP port 443 because it is already in use by another process on
this system (such as a web server). Please stop the program in question and then
try again.
-------------------------------------------------------------------------------
(R)etry/(C)ancel: R
Waiting for verification...
Cleaning up challenges
Generating key (2048 bits): /etc/letsencrypt/keys/0000_key-certbot.pem
Creating CSR: /etc/letsencrypt/csr/0000_csr-certbot.pem

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/ge-no.com/fullchain.pem. Your cert will
   expire on 2017-06-09. 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"
 - 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

edit nginx config:

nginxのSSL証明書を指している部分を以下のように変更してやる。

ssl on;
ssl_certificate /etc/letsencrypt/live/ge-no.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ge-no.com/privkey.pem;

ちなみに/etc/letsencrypt/live配下にある.pemはcertbotで生成される証明書へのシンボリックリンクになっています。
このリンクを指定しておけば次回更新時に設定ファイルの書き換えが不要になります。
fullchain.pemはサーバ証明書と中間証明書が結合されています。
nginxは結合された証明書である必要がありますが、apacheの場合は分離できるのでcert.pemとchain.pemをそれぞれ指定すればいいと思います。

うまくnginxが立ち上がればSSL証明書が更新されてます。
スクリーンショット 2017-03-11 23.53.39.png

次回更新どうする??

Let's Encryptの証明書の有効期限って3ヶ月しかないです。
めんどくさいので基本的に自動化した方がいいのですが諸事情によりwebroot使うのは難しそう&とりあえず次は手動でもいいので以下で多分対応できる

sudo /usr/local/src/certbot-auto renew --pre-hook "sudo service nginx stop" --post-hook "sudo service nginx start"

https://letsencrypt.jp/docs/using.html#renew-subcommand
"上記のコマンドを実行すると、過去に取得した証明書のうち、有効期間の残りが30日未満の証明書が更新されます。"
ということで上記コマンドのテストはできないけど大丈夫そう。

運用負荷とセキュリティ的なメリット

今までSSL証明書を買い付けて、メールで来た証明書とかを加工してサーバに放り込む運用をしていました。
Let's Encryptというかcertbot入れるのには精神的障壁が大きかったが、今後の運用負荷がだいぶ軽減するというか自動化すれば0になるはず。

証明書発行費用0円+自動化により運用負荷が低くなることで、頻繁にSSL証明書を更新できるようになればセキュリティ上のメリットが生まれます。
証明書の有効期間が90日間な理由

参考

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9