Skip to content

Instantly share code, notes, and snippets.

@kus
Last active October 21, 2023 01:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kus/7d82442ab69b811b2b93a93a91fdc703 to your computer and use it in GitHub Desktop.
Save kus/7d82442ab69b811b2b93a93a91fdc703 to your computer and use it in GitHub Desktop.
Local HTTPS server, generate SSL certificate

Chrome has decided that what they deem powerful web platform features such as Geolocation, Device motion / orientation, getUserMedia etc can no longer run on "Insecure Origins", HTTP (non-HTTPS) being one of them. Read more

So if you want to use these features in development and you are testing on a mobile via the IP of your computer, you now need to be serving the content over HTTPS for it to work.

Generate local SSL certificate

First check if you need to install openssl with which openssl. If nothing comes up run brew install openssl to install openssl with Brew.

openssl genrsa -des3 -passout pass:x -out localhost.pass.key 2048
openssl rsa -passin pass:x -in localhost.pass.key -out localhost.key
openssl req -new -key localhost.key -out localhost.csr
openssl x509 -req -sha256 -days 365 -in localhost.csr -signkey localhost.key -out localhost.crt
rm localhost.pass.key
rm localhost.csr
mv localhost.crt ~/.ssh/localhost.crt
mv localhost.key ~/.ssh/localhost.key

Start local HTTPS server

npm install -g http-server
http-server --ssl --cert ~/.ssh/localhost.crt --key ~/.ssh/localhost.key -o

To make things quicker, you can setup an alias in your ~/.bash_profile, so next time you can type https-server in any directory to spin up a HTTPS server.

alias https-server='http-server --ssl --cert ~/.ssh/localhost.crt --key ~/.ssh/localhost.key -o'

@odahcam
Copy link

odahcam commented Apr 13, 2018

I could not get this to work. :(
image

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