Skip to content

Instantly share code, notes, and snippets.

@SheldonWangRJT
Last active March 8, 2019 21:53
Show Gist options
  • Save SheldonWangRJT/0f4a9c66b0c820a20a9428216ac7a758 to your computer and use it in GitHub Desktop.
Save SheldonWangRJT/0f4a9c66b0c820a20a9428216ac7a758 to your computer and use it in GitHub Desktop.
Create local HTTPS server

Create local HTTPS Server

I was required to test some files that have to be hosted under a HTTPS server. Although at the end of the day, I used Azure to deploy a simple webservice, but the process to create a local HTTPS server is still interesting enough to let me write it down.

Notice before you start

There are two things you need to keep in mind before u keep doing this:

  • Although we will be using openssl to create a certificate, it is not a trusted certificate, I don't know if this will fulfil your need of HTTPS or not.
  • It will require sudo, make sure you are admin of your machine

Tools

Terminal packages

  • openssl (should be pre-installed already)
  • http-server (npm package - node.js package)

Steps

  1. Install packages $ npm install -g http-server
  2. Make directory under your home $ mkdir .localhost-ssl
  3. Create self signed key $ sudo openssl genrsa -out ~/.localhost-ssl/localhost.key 2048
  4. Create certificate $ sudo openssl req -new -x509 -key ~/.localhost-ssl/localhost.key -out ~/.localhost-ssl/localhost.crt -days 3650 -subj /CN=localhost
  5. Add this into your Keychain $ sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ~/.localhost-ssl/localhost.crt
  6. Trust the newly added certificate - Go to Keychain app and select Always Tust
  7. Start the Server (You can move the key file to any other location if you dont have access of the hidden folder)
    $ http-server --ssl --cert ~/.localhost-ssl/localhost.crt --key ~/.localhost-ssl/localhost.key

Image for always-trust in Keychain: img1

Image for running https server in Terminal: img2

References:

  1. https://medium.com/@jonsamp/how-to-set-up-https-on-localhost-for-macos-b597bcf935ee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment