Setup https for Rails localhost development

Looks like we can easily setup https locally while doing rails development using mkcert.

Here are the steps:

  • Install mkcert using brew: brew install mkcert. This might ask you to install something called nss, and while installing it (via brew again), if you use firefox master password, it would ask you that password too.
  • Add to /etc/hosts file the ‘.test’ version of your app’s intended domain. Eg: 127.0.0.1 MyBillionDollarApp.test
  • Create the key and certificate files for your domain like so: mkcert MyBillionDollarApp.test. This will generate the 2 files in the same folder you ran the command in.
  • Move these files to your rails app, into the folder config/ssl (make sure you gitignore this!)
  • Start your rails server like so: bundle exec rails server -p 3000 -b 'ssl://MyBillionDollarApp.test:3000?key=config/ssl/MyBillionDollarApp.test-key.pem&cert=config/ssl/MyBillionDollarApp.test.pem' (You can setup foreman gem and move this into the Procfile if you don’t like seeing this long command)
  • Now rails will serve the development environment version of your app at https://MyBillionDollarApp.test.

Great! This is one step closer in making your local development as much similar to your production setup as possible. This will bring to your attention a lot of ssl related errors before even deploying your app.

I wanted to set this up for my hugo site too (this site) locally. But the dev server that comes bundled with hugo doesn’t support ssl. So yeah, no https while testing the site locally.


{{%footnotes%}}

  • Thanks to Pieter Levels for the tweet informing this.
  • Thanks to https://rossta.net/blog/local-ssl-for-rails-5.html for telling how to bind ssl in rails server

{{%/footnotes%}}