Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create https key and cert on localhost
cd ~/
mkdir .localhost-ssl
sudo openssl genrsa -out ~/.localhost-ssl/localhost.key 2048
sudo openssl req -new -x509 -key ~/.localhost-ssl/localhost.key -out ~/.localhost-ssl/localhost.crt -days 3650 -subj /CN=localhost
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ~/.localhost-ssl/localhost.crt
npm install -g http-server
echo "
function https-server() {
http-server --ssl --cert ~/.localhost-ssl/localhost.crt --key ~/.localhost-ssl/localhost.key
}
" >> ~/.bash_profile
source ~/.bash_profile
echo "You're ready to use https on localhost 💅"
echo "Navigate to a project directory and run:"
echo ""
echo "https-server"
@heymartinadams

This comment has been minimized.

Copy link

heymartinadams commented Jul 14, 2019

Sweet! <3 And for those of us who use yarn:

cd ~/
mkdir .localhost-ssl

sudo openssl genrsa -out ~/.localhost-ssl/localhost.key 2048
sudo openssl req -new -x509 -key ~/.localhost-ssl/localhost.key -out ~/.localhost-ssl/localhost.crt -days 3650 -subj /CN=localhost
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ~/.localhost-ssl/localhost.crt

yarn add global http-server
echo " 
function https-server() {
  http-server --ssl --cert ~/.localhost-ssl/localhost.crt --key ~/.localhost-ssl/localhost.key
}
" >> ~/.bash_profile
source ~/.bash_profile

echo "You're ready to use https on localhost 💅"
echo "Navigate to a project directory and run:"
echo ""
echo "https-server"
@heymartinadams

This comment has been minimized.

Copy link

heymartinadams commented Jul 14, 2019

I found with Chrome, got the following error response, despite using your gist above:

NET::ERR_CERT_AUTHORITY_INVALID

So implemented this solution to allow insecure localhost: https://stackoverflow.com/a/53985755

@heymartinadams

This comment has been minimized.

Copy link

heymartinadams commented Jul 14, 2019

For React, needed to set HTTPS=true: HTTPS=true react-scripts start

@lano-vargas

This comment has been minimized.

Copy link

lano-vargas commented Dec 6, 2019

Hi running `sudo openssl req -new -x509 -key ~/.localhost-ssl/localhost.key -out ~/.localhost-ssl/localhost.crt -days 3650 -subj /CN=localhost' I get Can't load /Users/lano-vargas/.rnd into RNG
4489983424:error:2406F079:random number generator:RAND_load_file:Cannot open file:crypto/rand/randfile.c:88:Filename=/Users/lano-vargas/.rnd

I am missing this .rnd hwo can I fix it?

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.