auston

A simple way to run HTTPS on localhost

TL;DRIf you want to run https on localhost, you can use this handy dandy shell script.

curl https://raw.githubusercontent.com/userpath/stunnel_setup/master/setup_stunnel.sh -o setup_stunnel.sh && chmod +x setup_stunnel.sh && ./setup_stunnel.sh dev_port=<your-port-here>

After being stumped on how to get https running for my django app – I ended up figuring out that stunnel is the perfect solution for faking an ssl certificate on localhost (aka self-signed certs).

I’m pretty sure you could use this for any server that you are starting via the command line, so you could get local https / ssl for rails, sinatra, flask, bottle.py, node or even PHP on your local machine.

First, install stunnel:

brew install stunnel # for mac

apt-get install stunnel # for debian/ubuntu

Now once it’s installed, let’s generate our necessary SSL keys and certificates:

cd ~/ &&
openssl genrsa 1024 > stunnel.key &&
openssl req -new -x509 -nodes -sha1 -days 365 -key ~/stunnel.key > ~/stunnel.cert &&
cat ~/stunnel.key ~/stunnel.cert > ~/stunnel.pem

Finally, let’s make a config file:


pid=

cert = /path/to/stunnel.pem
sslVersion = all
options = NO_SSLv2
foreground = yes
output = /path/to/stunnel.log # a path that exists

[https]
accept=8443
connect=3333 # or 8000 or whatever port you're running on
TIMEOUTclose=1

Now once that stunnel.conf is somewhere accessible, let’s start stunnel:

sudo stunnel stunnel.conf

And finally, we start the server 🙂

python manage.py runserver 3333
rails s -p 3333
node app.js
php -S 127.0.0.1:3333

Any voila. We have HTTPS running on localhost. Click here to confirm! If that’s too annoying, I made a little shell script to download & configure stunnel for port 8443 and forward to port 8000 that should work for Mac OS X (if you have homebrew installed), Ubuntu or Debian. This is what it looks like:

stunnel-screencast

UserPath

Ready to try UserPath for yourself?

Sign Up For Free!
auston

Auston

Co-founder at UserPath. Often blogging about the stuff we're doing to make your apps better and users satisfied. Sometimes writing about experiments or research. Learner. Thinker. Programmer.