https://localhost

A set of really simple commands to enable https over localhost for Mac


cd; mkdir .ssl

openssl req -newkey rsa:2048 -x509 -nodes -keyout .ssl/localhost.key -new -out .ssl/localhost.crt -subj /CN=localhost -reqexts SAN -extensions SAN -config <(cat /System/Library/OpenSSL/openssl.cnf <(printf '[SAN]\nsubjectAltName=DNS:localhost')) -sha256 -days 3650

sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain .ssl/localhost.crt
        

Now that you have a certificate for you loacalhost, you can spin up a server. An easy option would be to use http-server, which comes with options to serve over ssl.


npm install http-server -g
        

Now to spin-up the server with ssl you'll need to pass a few options to the http-server command.


http-server --ssl --cert ~/.ssl/localhost.crt --key ~/.ssl/localhost.key -a localhost -o
        

This command is quite long to type in everytime you want to spin-up a server with ssl, so you might want to consider adding an alias like


alias https-server='http-server --ssl --cert ~/.ssl/localhost.crt --key ~/.ssl/localhost.key -a localhost -o'
        

And add it to your .bash_profile