Skip to content

Instantly share code, notes, and snippets.

@jonsamp
Created June 11, 2017 21:16
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save jonsamp/587b78b7698be7c7fd570164a586e6b7 to your computer and use it in GitHub Desktop.
Save jonsamp/587b78b7698be7c7fd570164a586e6b7 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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
Copy link

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

@lano-vargas
Copy link

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?

@quintendewilde
Copy link

How can I put a folder in this https: server?
I can not locate the local host directory
Or where do I put my files to run the website/webapp

@atrovatto
Copy link

Same doubt here, How can I put a folder in this https: server?

usualy when i start a http server i do this on the terminal:

  • cd <insert here the folder_path without the "<>">
  • php -S 0.0.0.0:8080
  • Then i type my ip in the place of "0.0.0.0" + "8080"

How can i create the project in the https after running the script?

@hashmil
Copy link

hashmil commented Jul 11, 2020

@quintendewilde

How can I put a folder in this https: server?
I can not locate the local host directory
Or where do I put my files to run the website/webapp

The local host directory is hidden. In terminal do a cd ~/ then do ls -a and you should see the folder created.
Also you dont need to put the folders anywhere. just cd to the folder and run https-server

@ollyde
Copy link

ollyde commented Oct 28, 2021

The script doesn't work. I get the following: /Users/junkbox/.bash_profile: line 9: `https-server': not a valid identifier

@TanjinAlam
Copy link

after executing the script run
sudo http-server --ssl --cert ~/.localhost-ssl/localhost.crt --key ~/.localhost-ssl/localhost.key
it will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment