- Create a directory at
~/Certs/
:
$ mkdir ~/Certs
- Change into the
~/Certs
directory and generate a new key and certificate:
$ cd ~/Certs
$ openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365
Since this is for local development purposes, you can choose something simple the PEM pass phrase. Common Name can be 127.0.0.1
.
- Generate a decrypted key:
$ openssl rsa -in keytmp.pem -out key.pem
You'll be asked for the PEM pass phrase that you set in the previous step.
- Test with
http-server
by navigating to a directory with a web project. (Runnpm install -g http-server
if you haven't already):
$ http-server -S -C ~/Certs/cert.pem -K ~/Certs/key.pem -p 8443
That sets both the certificate and key files, and serves https
over port 8443. Test it in your browser at https://localhost:8443/
(be sure to type the https://
portion of the URL, too).
Those same certificate and key files can be used to serve https
elsewhere, including Express.js web apps
- Create an
https-server
alias by adding these lines to your shell's startup script (e.g.,.bashrc
,.bash_profile
,.zshrc
):
# Alias for http-server with SSL
alias https-server="http-server -S -C ~/Certs/cert.pem -K ~/Certs/key.pem -p 8443"
Either close your existing terminal window or source
your startup script to make the alias available, e.g., source ~/.bashrc
.