See <a href="https://shellcheck.net" rel="nofollow">https://shellcheck.net</a> to fix your script and follow good guidelines. Top of my head:<p>* /bin/bash -> /usr/bin/env bash<p>* You probably don't need bash anyway, so switch to /bin/sh<p>* errors go to stderr (>&2), not stdout<p>* exits because of errors should return non-zero codes. (e.g.: `exit 1`)<p>* Full caps variables are bad practise (might conflict with real, global env variables)<p>* rather than running everything as root (using sudo), I'd call sudo for the only few commands that actually require root privileges (I found none, so I suppose "security" is the only command that needs root perms).
Although these will "work" today in popular browsers and with most tools, this is NOT the right way to scribble a DNS name into a certificate this century.<p>Write SANs. Subject Alternative Names. These aren't aliases, the "alternative" means in the sense that this is an "alternative" to writing human readable X.500 series Common Names. Unlike those human names, SANs are defined in a machine readable way, e.g. the dnsNAme SAN spells exactly DNS A-labels, the ipAddress SAN is just an IPv4 or IPv6 address written out as raw bytes, not a dotted decimal or whatever else someone thought might be fun today.<p>You should also write one of the SANs you choose as the Common Name in some plausible text format, but by having SANs all vaguely modern tools can just match those rather than trying to make sense of the Common Name.<p>In a very new OpenSSL you can actually do this from the command line sort-of sensibly. In most installs you will need to modify that configuration file instead, you're already using a configuration file so that's no big deal.
Good ideas are rarely unique, as they usually solve a common problem. I recently built something similar:<p><a href="https://github.com/FiloSottile/mkcert" rel="nofollow">https://github.com/FiloSottile/mkcert</a><p>It's in pure Go instead of using OpenSSL, and it works with Windows, macOS and Firefox, too.
Also see "Certificates for localhost" from Certbot/Let's Encrypt documentation:<p><a href="https://letsencrypt.org/docs/certificates-for-localhost/" rel="nofollow">https://letsencrypt.org/docs/certificates-for-localhost/</a>
Your solution generates a certificate and leaves it up to the user to setup https.<p>There are other steps involved, like adding the cert to the trust store (so you don't get invalid SSL warnings). And also changing your application code to use these certificates.<p>Even if you do that, you are still exposed to a serious security threat: if a bad actor gets hold of your certificate file, they can pose as a legitimate website and steal sensitive data. This security flaw is present with all other script solutions mentioned in this thread.<p>To overcome these issues, I have built a mac application called HTTPSLocalhost (<a href="https://httpslocalhost.com" rel="nofollow">https://httpslocalhost.com</a>).<p>- It offers a user interface to add remove local https domains<p>- Has an inbuilt proxy so you don't need to change your application code<p>- Is much safer because it deletes the certificate and private keys as soon as the proxy server starts<p>- It creates a new certificate each time you start the app, to enhance security.<p>- And of course, like all good things, is free (there is a video demo on the website, the app will be ready soon).<p>Wanted to do a proper Show HN next week, but I guess it's the right time to bring it up :)<p>Thanks
<i>"Generating the certs is a complicated hassle."</i><p>Not really... I will grant that the openssl commands are a bit non-obvious.<p>Step 1: Generate private key<p>openssl ecparam -genkey -name secp384r1 -out key.pem<p>Step 2: Create and sign cert<p>openssl req -x509 -sha512 -nodes -days 365 -key key.pem -subj "/CN=example.com" -reqexts SAN -extensions SAN -config <(cat /etc/ssl/openssl.cnf <(printf '[SAN]\nsubjectAltName=DNS:example.com,DNS:*.example.com')) -out cert.pem
I built something similar (though probably a lot less sophisticated) as an alpine based docker image. I had some issues with openssl on a Mac in the past, and this approach circumvents those.<p><a href="https://hub.docker.com/r/qubyte/cert-creator/" rel="nofollow">https://hub.docker.com/r/qubyte/cert-creator/</a>
I would use it if I could do so with PHP's internal webserver.<p>I often hack together quick experiments using PHP's internal webserver. It only serves via http though, not https. Is there a way to make it serve over https?
If you prefer using macOS itself, making a trusted self signed cert only requires a few clicks and one command <a href="https://certsimple.com/blog/localhost-ssl-fix" rel="nofollow">https://certsimple.com/blog/localhost-ssl-fix</a>