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.
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
Terminal packages
openssl
(should be pre-installed already)http-server
(npm package - node.js package)
- Install packages
$ npm install -g http-server
- Make directory under your home
$ mkdir .localhost-ssl
- Create self signed key
$ sudo openssl genrsa -out ~/.localhost-ssl/localhost.key 2048
- Create certificate
$ sudo openssl req -new -x509 -key ~/.localhost-ssl/localhost.key -out ~/.localhost-ssl/localhost.crt -days 3650 -subj /CN=localhost
- Add this into your Keychain
$ sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ~/.localhost-ssl/localhost.crt
- Trust the newly added certificate -
Go to Keychain app and select
Always Tust
- 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:
Image for running https server in Terminal:
References: