LoginSignup
145
150

More than 5 years have passed since last update.

Herokuの本番環境にRailsでSSL(https)を導入する方法

Last updated at Posted at 2012-12-26

環境は、rails 3.2.6、ruby 1.9.3、heroku-toolbelt/2.33.0 です。


秘密鍵を作る

(参考)https://devcenter.heroku.com/articles/csr

$ openssl genrsa -des3 -out server.orig.key 2048
...
Enter pass phrase for server.key: 適当なパスワードを。
Verifying - Enter pass phrase for server.key: もう一度同じパスワードを。

WEBサーバーに自動でアップロードするために、パスワードを外しておきます。

$ openssl rsa -in server.orig.key -out server.key

署名リクエストを作ります

$ openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP ※国名
State or Province Name (full name) [Some-State]:Hokkaido ※都道府県
Locality Name (eg, city) []:Hakodate ※市区町村
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Helply LLC. ※会社名
Organizational Unit Name (eg, section) []: ※部署(入力せず)
Common Name (eg, YOUR name) []:www.helply.net ※証明書に表示される名前
Email Address []:gen.tamura@helply.net ※メールアドレス

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:※入力せず
An optional company name []:※入力せず

私は上記のように入力しました。お好みでいいと思います。
※ちなみにこの一連の作業はRailsのappやconfigがある階層で保存してます。


証明書を購入する

(参考)https://devcenter.heroku.com/articles/ssl-certificate

  1. 証明証を購入しましょう。私はHerokuおすすめのDNSimpleから購入しました。
    https://dnsimple.com/

  2. アカウントをつくったら、DomainsAdd a Domainから希望のドメインを追加しましょう。(私はhelply.netを追加。)

  3. 追加したドメインの右端にあるManageをクリックします。

  4. 画面中央のSSL CertificatesにあるBuy an SSL certificateをクリックします。

  5. 購入にあたっての情報を入力します。
    ※1点注意があって、サブドメインを指定する場合は$20/年で、*(ワイルドカード)だと$100/年となるのでご注意下さい。

  6. 最後に「Please generate a private key and certificate signing request for me. Uncheck this box to provide the CSR yourself. If you generate your own CSR it must be valid.」とありますが、チェックはつけたままで登録しましょう。
    (外すとcsrを自分で作ってね。とありますので、外すとハマる気がします)

  7. 次のページに、「admin@yourdomain.comwebmaster@yourdomain.comroot@yourdomain.com」などの管理者用メールアドレスを選択する画面になります。これは後ほど、証明機関から承認URLを受信するために必要です。表記された該当のメールアドレスを持っていない場合は新たに該当のメールアドレスを取得する必要があるので、ご注意下さい。

  8. 完了すると、DNSimpleから「8時間以内に証明機関から、証明書を承認するためのリンクが届きます。」という内容のメールが届きます。少し待ちましょう。(私は10分ぐらいで届きました。)

  9. しばらくすると、GeoTrust(証明機関)から「Approver Emailが届くからよろしくね。」なるメールが届きます。そののち、管理者用メールアドレスに届く「RapidSSL Certificate Request Confirmation
    」を確認し、「あなたのお名前 requests that you come to the URL below to review and approve this certificate request:」の文章の下のリンクをクリックします。

  10. すると日本語で書かれた「RapidSSL® 注文の確認および承認」という書式が表示されますので、一番下の「承認します」をクリックします。

  11. これで購入が完了しました。GeoTrustから完了メールが届きます。

証明書をダウンロードする

(参考)https://devcenter.heroku.com/articles/ssl-certificate の Download certificate files以下

  1. DNSimpleを開き、DomainsSSL CertificatesDetailsをクリック。

  2. Statusが「Certificate Issued」となっていれば、「Private Key:」と「Certificate:」が表示されているはずです。※購入完了してから、少し時間が必要かもしれません。

  3. 「Private Key:」をserver.keyに追加します。
    ※保存は最初の「-」から最後の改行までです。
    ※エディタで開いて、一番下に追加します。

  4. 「Certificate:」をserver.orig.crtを新規に作成し、保存します。
    ※保存は最初の「-」から最後の改行までです。

  5. Herokuではサイト証明書と中間証明書を一つのファイルにまとめる必要があります。
    そこで以下のコマンドを実行します。

curl https://knowledge.rapidssl.com/library/VERISIGN/ALL_OTHER/RapidSSL%20Intermediate/RapidSSL_CA_bundle.pem > rapidssl_bundle.pem
 cat server.orig.crt rapidssl_bundle.pem > server.crt

HerokuのSSL導入

\1. アドオンを追加する

$ heroku addons:add ssl
Adding ssl on young-planet-6921... done, v1 ($20/mo)
Next add your certificate with `heroku certs:add PEM KEY`.
Use `heroku addons:docs ssl` to view documentation.

\2. ドメインを設定する(すでに独自ドメインを設定している場合は不要)

$ heroku domains:add www.mydomain.com
Added www.mydomain.com to myapp... done

\3. プラグインを追加する

$ heroku plugins:install https://github.com/heroku/heroku-ssl-doctor.git

※実は4でどうにも進まなくなり、Herokuのサポートへメールをしたら、このプラグインを教えてもらいました。

\4. 証明書を追加する

$ heroku certs:add server.crt server.key
Adding SSL Endpoint to myapp... done
myapp now served by tokyo-2121.herokussl.com.
Certificate details:
Expires At: 2012-10-31 21:53:18 GMT
Issuer: C=US; ST=CA; L=SF; O=Heroku; CN=www.mydomain.com
Starts At: 2011-11-01 21:53:18 GMT
Subject: C=US; ST=CA; L=SF; O=Heroku; CN=www.mydomain.com
SSL certificate is self signed.

DNSを変更する

\1. HerokuのIPアドレスを確認

さっきの4で表示されたEndpointを確認。
ここでは、tokyo-2121.herokussl.com.

\2. ご利用のネームサーバーにレコードを追加する。

「www.mydomain.com」などの独自ドメインに対して、CNAMEで「host tokyo-2121.herokussl.com」を追加します。

Railsの設定を変更する

最後にRailsの設定変更です。
デフォルトでは、コメントアウトになっていると思うので、
コメントインします。falseならtrueへ。

config/environments/production.rb
config.force_ssl = true

以上で、httpsでのアクセスが可能になります。


※番外編 SSLを更新する場合

\1. SSL証明書が切れる2ヶ月ぐらい前に、DNSimpleから「Expiration Notification」のメールが届きます。

\2. 本文中の「Renew Now」をクリック。

\3. 「Generate a private key and certificate signing request for me」のチェックを入れたまま、「Renew SSL Certificate」のボタンをクリック。

\4. 「証明書を購入する」の7〜11と「証明書をダウンロードする」を実施する。

\5. 「HerokuのSSL導入」の4.のみを以下の通り実施する。

$ heroku certs:update server.crt server.key

以上で、更新が完了です。

145
150
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
145
150