HTTPS/SSL in your local Rails 4.1 development environment

David Santoro
Carwow Product, Design & Engineering
2 min readApr 24, 2015

This week we worked on enabling HTTPS across all carwow.co.uk domains.
Setting up certificates, dns and heroku has been fairly easy thanks to ThoughtBot instructions.

Setting up the development environment has been painful!

This is the steps I’ve followed:

1 — Generate self-signed certificate

openssl req -new -newkey rsa:2048 -sha1 -days 365 -nodes -x509 -keyout localhost.key -out localhost.crt

Make sure to use localhost as common name for the certificate.

2 — Trust certificate in Ubuntu (Vagrant)

sudo cp localhost.crt /etc/ssl/cert sudo cp localhost.key /etc/ssl/private sudo c_rehash

3 — Trust certificate in OSX

In the Keychain Access application drag the localhost.crt file in the system certificates.

Then right click on the certificate, select Get info and select Always Trust.

4 — Configure WebBrick to use SSL

I’ve created the file config/ssl.rb in our Rails 4.1 app.

if ENV['SSL'] == 'true' require 'rubygems' require 'rails/commands/server' require 'rack' require 'webrick' require 'webrick/https' module Rails class Server < ::Rack::Server def default_options super.merge({ SSLEnable: true, SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE, SSLPrivateKey: OpenSSL::PKey::RSA.new(File.open("/path/to/localhost.key").read), SSLCertificate: OpenSSL::X509::Certificate.new(File.open("/path/to/localhost.crt").read), SSLCertName: [["CN", WEBrick::Utils::getservername]], }) end end end end

And I’ve required it in bin/rails at the before any other Ruby code

#!/usr/bin/env ruby #------- added by carwow -------- require_relative '../config/ssl' #-------------------------------- begin load File.expand_path("../spring", __FILE__) rescue LoadError end APP_PATH = File.expand_path('../../config/application', __FILE__) require_relative '../config/boot' require 'rails/commands'

That’s it. Run your server with SSL=true rails s and you’ll have your app running in secure mode.

Originally published at underthehood.carwow.co.uk on April 24, 2015.

Interested in making an Impact? Join the carwow-team!
Feeling social? Connect with us on Twitter and LinkedIn :-)

--

--