Skip to content

Instantly share code, notes, and snippets.

@karlstolley

karlstolley/self-signed-certs.md Secret

Last active Sep 9, 2020
Embed
What would you like to do?
Serve https locally

Self-Signed Certs for Local https

  1. Create a directory at ~/Certs/:
$ mkdir ~/Certs
  1. Change into the ~/Certs directory and generate a new key and certificate:
$ cd ~/Certs
$ openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365

Since this is for local development purposes, you can choose something simple the PEM pass phrase. Common Name can be 127.0.0.1.

  1. Generate a decrypted key:
$ openssl rsa -in keytmp.pem -out key.pem

You'll be asked for the PEM pass phrase that you set in the previous step.

  1. Test with http-server by navigating to a directory with a web project. (Run npm install -g http-server if you haven't already):
$ http-server -S -C ~/Certs/cert.pem -K ~/Certs/key.pem -p 8443

That sets both the certificate and key files, and serves https over port 8443. Test it in your browser at https://localhost:8443/ (be sure to type the https:// portion of the URL, too).

Those same certificate and key files can be used to serve https elsewhere, including Express.js web apps

  1. Create an https-server alias by adding these lines to your shell's startup script (e.g., .bashrc, .bash_profile, .zshrc):
# Alias for http-server with SSL
alias https-server="http-server -S -C ~/Certs/cert.pem -K ~/Certs/key.pem -p 8443"

Either close your existing terminal window or source your startup script to make the alias available, e.g., source ~/.bashrc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.