MENU

Let’s encrypt で ssl 通信してみた話

記事作成から1年以上経過しています。
内容が古い可能性があります。

How to Let’s encrypt

目次

TL;DR

  1. Let’s encryptを使って
  2. Pumaで動くRailsアプリを
  3. ssl対応した話

手順

  1. Certbot クライアントの導入
  2. 証明書の発行
  3. アプリへ設定
  4. 動作確認

Certbot クライアントの導入

証明書を発行するために使う Certbot のクライアントをインストールする。

今回使用するドメインはこちら

myapp.sample.biz

今回使用した環境はこちら

CentOS Linux release 7.6.1810 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

CentOS 7 / Red Hat Enterprise Linux 7 用の Certbot パッケージは、EPEL (Extra Packages for Enterprise Linux) リポジトリからインストールすることができます。

だそうなので、epelの確認

$ yum repolist | grep epel
 * epel: d2lzkl7pfhq30w.cloudfront.net
epel/x86_64             Extra Packages for Enterprise Linux 7 - x86_64    12,843

入ってるのでリポジトリの追加はスキップして certbot のインストール。

$ sudo yum install certbot # install
$ type certbot   # pathの確認
$ certbot --help # installの確認

ヘルプが確認できればインストールが完了です。

証明書の発行

証明書を発行してもらう。一番大切なところ。

standaloneプラグインを使って発行する場合は、ポート 80, 443 を使うために root 権限が必要らしい。
いくつかのファイル作成のためにも root 権限が必要みたい。

なので、ポート 80 or 443 で何かが起動している場合は停止しておく。
root ユーザで certbot コマンドを実行する。

# certbot certonly --standalone -d myapp.sample.biz
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for ses-api.miracleave.biz
Waiting for verification...
Cleaning up challenges
Resetting dropped connection: acme-v02.api.letsencrypt.org

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/myapp.sample.biz/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/myapp.sample.biz/privkey.pem
   Your cert will expire on 2019-04-02. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. 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

# ll /etc/letsencrypt/live/myapp.sample.biz/
-rw-r--r--. 1 root root 692  1月  2 09:42 README
lrwxrwxrwx. 1 root root  46  1月  2 09:42 cert.pem -> ../../archive/myapp.sample.biz/cert1.pem
lrwxrwxrwx. 1 root root  47  1月  2 09:42 chain.pem -> ../../archive/myapp.sample.biz/chain1.pem
lrwxrwxrwx. 1 root root  51  1月  2 09:42 fullchain.pem -> ../../archive/myapp.sample.biz/fullchain1.pem
lrwxrwxrwx. 1 root root  49  1月  2 09:42 privkey.pem -> ../../archive/myapp.sample.biz/privkey1.pem

今回の設定では privkey.pemfullchain.pem を使用する。
なお、これら(*.pem)のファイルは移動すると certbot が正常に動作しなくなるそうなので移動しないことをおすすめします。

アプリへ設定

puma の設定ファイルにsslの設定を追加する。

$ cd $PRJ_ROOT
$ echo 'ssl_bind "0.0.0.0", 443, {
  key: "/etc/letsencrypt/live/myapp.sample.biz/privkey.pem",
  cert: "/etc/letsencrypt/live/myapp.sample.biz/fullchain.pem"
}' >> ./config/puma.rb

これで 3000番portでhttp通信を行い、443番port でhttps通信を行う。

3000番port の http通信をしたくない場合は ./config/puma.rb

port ENV.fetch("PORT") { 3000 }

を削除 or コメントアウト しておくと3000番で LISTEN しなくなる。

動作確認

httpsでリクエストを投げて帰ってくるか確認する。

$ curl -D - -s -o /dev/null https://myapp.sample.biz:443/
HTTP/1.1 200 OK

HTTPS通信ができました!
めでたしめでたし。
※ ドメインで証明書を作成しているため、https://localhost:443 では認証エラーになるので注意する。

証明書の更新方法などはそのうち追加予定です。

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

ブライダル業界 → 2021年9月 miracleave に転職
未熟者ですが、よろしくお願いいたします!
甘いもの大好きすぎる。。

目次