How-to guide · Create keys and CSRs

How to use HTTPS on localhost with mkcert

In one sentence

Create a locally trusted certificate, run an HTTPS development server, and verify which CA each browser, WSL session or container actually trusts.

Task
Create keys and CSRs
Platforms
  • Windows
  • macOS
  • Linux
  • Node.js
  • Docker
Who it is for
Level
Practitioner · 5 steps
On this page
  1. Before you start
  2. Steps
  3. 1. Install mkcert and its browser-store support
  4. 2. Create and trust your development CA
  5. 3. Issue the exact local names
  6. 4. Point the development server at the leaf files
  7. 5. Keep WSL and Docker trust boundaries explicit
  8. Check it worked
  9. If something goes wrong
  10. Why this works
  11. Go deeper

Before you start

  • A development machine you control, and permission to add a development CA to its trust store.
  • mkcert, a maintained Node.js installation for the example server, and a browser.
  • The names you will actually type: localhost, a loopback IP, or a chosen local development hostname.
  • A project directory where generated keys will stay outside version control.

mkcert creates a local certificate authority and issues certificates from it. Its certificates become trusted on machines where you explicitly install that CA. This gives you a real TLS connection for development; it does not create a public certificate or configure your web server automatically.

For a public IP or internet-facing service, use the Let’s Encrypt IP certificate guide or Certbot guide instead. Do not distribute a development CA as your production trust model.

Steps

1. Install mkcert and its browser-store support

Use an installation method from the project’s README. On macOS with Homebrew:

brew install mkcert
brew install nss

nss supports the Firefox certificate store. On Windows with Chocolatey:

choco install mkcert
mkcert -version

On Debian or Ubuntu, install the certificate-store helper before using an official mkcert binary for your architecture:

sudo apt install libnss3-tools
mkcert -version

Obtain the binary from the project’s releases, not a similarly named download site. The helper package alone does not install mkcert. Follow your normal package-management policy if your organisation supplies an approved build.

2. Create and trust your development CA

Run as your normal development user; allow only the specific elevation needed to install the CA:

mkcert -install
mkcert -CAROOT

Restart the browser after changing its trust store. -CAROOT prints the directory containing rootCA.pem, the public CA certificate, and rootCA-key.pem, its private signing key. The key can issue certificates trusted by this machine, so keep the directory private.

3. Issue the exact local names

In your project, create a certificate directory. Add .certs/ to the existing .gitignore, preserving its other entries. Then run:

mkdir -p .certs
mkcert -cert-file .certs/localhost.pem \
  -key-file .certs/localhost-key.pem localhost 127.0.0.1 ::1

In PowerShell:

New-Item -ItemType Directory -Force .certs | Out-Null
mkcert -cert-file .certs/localhost.pem -key-file .certs/localhost-key.pem localhost 127.0.0.1 ::1

Flags must precede the names. Include a name such as app.test only if you will also configure DNS or a hosts entry for it. mkcert issues names; it does not make those names resolve. A port is not part of a certificate name: the same localhost certificate can serve different local HTTPS ports.

Inspect the SAN extension:

openssl x509 -in .certs/localhost.pem -noout -ext subjectAltName

The locally tested command produced:

X509v3 Subject Alternative Name:
    DNS:localhost, IP Address:127.0.0.1, IP Address:0:0:0:0:0:0:0:1

4. Point the development server at the leaf files

Frameworks have their own HTTPS configuration. The following small server.mjs shows the complete Node.js setup without a framework dependency:

import { readFileSync } from 'node:fs';
import { createServer } from 'node:https';

const server = createServer({
  cert: readFileSync(new URL('./.certs/localhost.pem', import.meta.url)),
  key: readFileSync(new URL('./.certs/localhost-key.pem', import.meta.url)),
}, (_request, response) => {
  response.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' });
  response.end('Local HTTPS works.\n');
});

server.listen(8443, '127.0.0.1');

Start it from your project:

node server.mjs

Visit https://localhost:8443/. This demonstration binds IPv4 loopback only. The certificate includes IPv6, but a certificate does not make the application listen on IPv6; bind an IPv6 listener if you need to test https://[::1]:8443/.

5. Keep WSL and Docker trust boundaries explicit

What makes the HTTPS connection? What needs the CA?
Windows browser to a WSL server Windows browser/OS trust store
curl or uv inside WSL That Linux environment or the tool’s explicit CA bundle
Browser to a containerised server Browser trust; the server receives the leaf and its key
Application inside a container calling HTTPS Container/runtime trust store

Generate the leaf on the machine that owns the CA key, then provide only the leaf certificate and leaf key to your development server. Mount those files read-only into a container. If the container is accepting a published port, its application generally needs to listen on 0.0.0.0 internally; restrict the host-side port publication to loopback, for example 127.0.0.1:8443:8443.

For a WSL or container client, copy the public rootCA.pem through your approved local setup and use the Linux trust instructions. Creating a second mkcert CA inside WSL produces a different root: installing the first one on Windows will not trust certificates from the second.

Node.js clients can explicitly add this root before the process starts:

export NODE_EXTRA_CA_CERTS="$(mkcert -CAROOT)/rootCA.pem"

For PowerShell, set $env:NODE_EXTRA_CA_CERTS to the full path of rootCA.pem. This configures outbound Node HTTPS trust; it does not replace the server’s cert and key settings. For uv, use its certificate settings rather than assuming Python or Node settings apply.

Check it worked

Verify the chain independently on the issuing machine:

openssl verify -CAfile "$(mkcert -CAROOT)/rootCA.pem" \
  -verify_hostname localhost .certs/localhost.pem
curl --cacert "$(mkcert -CAROOT)/rootCA.pem" https://localhost:8443/

The expected certificate result is .certs/localhost.pem: OK, followed by your server’s response. Then test the browser without an exception. Explicit --cacert proves the supplied root works; the browser test proves its own trust configuration works.

If something goes wrong

  • Unknown issuer: the client trusts a different CA or was not restarted. Follow browser trust diagnostics.
  • Name mismatch: issue the actual hostname or IP as a SAN; changing the URL or hosts file alone does not change the certificate.
  • Connection refused: the listener, WSL forwarding or container port mapping is missing. Certificate trust cannot repair networking.
  • Node client fails while the browser works: configure the root before starting Node; an application-supplied ca option can override its usual CA sources.
  • A colleague cannot open your site: their machine does not automatically trust your local CA. Keep development trust deliberate, and never send them the CA key.

Why this works

The browser validates a certificate chain to the local root you installed. The SANs identify the names the certificate covers. The root is trusted because you added it to the trust store, not because it signed itself. mkcert simplifies development issuance while leaving server configuration and client trust visible.

Go deeper

Guided mission · free · no account · progress saves hereChoose the root your client trusts — 6 minutesTry different trust stores and see why a self-signed root does not choose its own authority.

Early edition. Written with AI assistance and checked against primary sources; an independent expert review is planned. Found a mistake? Tell us.

Last reviewed Edition 0.9