LoginSignup
2
3

More than 5 years have passed since last update.

LaravelでHerokuにSSLを設定した話。

Posted at

以外にやり方面倒くさいので、メモ代わりに。
デプロイ用のマシンはmacです。

参考にしたサイト
http://blog.kakeragames.com/2016/09/14/heroku-ssl.html
http://qiita.com/kon_yu/items/302d8bc0924f292d378b

インストール

Let's Encryptを利用します。
昔はgitからインストールする必要がありましたが、最近はhomebrewで簡単にインストールすることが出来ます。

$ brew install certbot

Laravelのコード修正

証明する際に、特定の場所にファイルを置く必要あります。

ドメイン名/.well-known/acme-challenge/xxxx

で、定期的に手動で更新するする必要があるので、環境変数で簡単に更新できるようにします。

route.php

Route::get('/.well-known/acme-challenge/{id}', function ($id) {
    if ($id == env('LETSENCRYPT_REQUEST')) {
        return env('LETSENCRYPT_RESPONSE');
    } else {
        abort(404);
    }
});

publicにファイルを追加します。
emptyは空で良いです。

public/.well-known/acme-challeng/empty

ディレクトリを準備しないと、何故か403エラーになり上手く表示されませんでした。回避方法があれば誰か教えてください。

証明書を発行

証明書発行のコマンドを実行します。

$ certbot certonly --manual -d www.example.com --logs-dir certbot --config-dir certbot --work-dir certbot

sudo付けて実行しても良いですが、今回はworkingdirを変更して対応します。

色々、受け答えしますが、以下のメッセージが出たら作業を止めます。

Make sure your web server displays the following content at
http://www.example.com/.well-known/acme-challenge/xxxxx before continuing:

xxxxx.yyyyy

herokのconfigに設定を追加します。

$ heroku config:set LETSENCRYPT_REQUEST=xxxxx
$ heroku config:set LETSENCRYPT_RESPONSE=xxxxx.yyyyy

終わったらEnterして完了です。
成功したら以下のようなメッセージが表示されます。

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
...

herokuに証明書を設定する。

workingdirを変更しているので、そのパスを指定します。

$ heroku certs:add ./certbot/live/www.example.com/cert.pem ./certbot/live/www.example.com/privkey.pem

正しく登録出来てるか $ heroku certs:info で確認します。

まとめ

いままでSSL利用するのに、20ドル取られていましたが、Hobby Dynos (7ドル) にするだけでSSLを簡単に設定できるようになりました。
10分もかからないので、herokuで運用しているサービスはSSLにしても良いかもしれません。
※ 自動更新できないので、定期的に更新する必要があります。

2
3
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
2
3