<!-- https://pkizerotohero.com/guides/localhost-https-mkcert/ | Markdown mirror of the HTML page | PKI Zero to Hero, by Duyutech BV -->

> Set up localhost HTTPS with mkcert on Windows, macOS and Linux. Verify SANs, configure a dev server and handle WSL, Docker and Node trust correctly.

Source: https://pkizerotohero.com/guides/localhost-https-mkcert/ · Last reviewed: 2026-10-25

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](https://pkizerotohero.com/guides/#task-create)

Platforms

- Windows
- macOS
- Linux
- Node.js
- Docker

Who it is for

- [Developer](https://pkizerotohero.com/start/#developer)
- [IT ops](https://pkizerotohero.com/start/#itops)

Level

Practitioner · 5 steps

## Before you start

- A development machine you control, and permission to add a development CA to its trust store.
- [mkcert](https://github.com/FiloSottile/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](https://pkizerotohero.com/handbook/glossary/#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](https://pkizerotohero.com/guides/lets-encrypt-ip-address-certificate/) or [Certbot guide](https://pkizerotohero.com/guides/certbot-lets-encrypt/) 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](https://github.com/FiloSottile/mkcert#installation). On macOS with Homebrew:

```bash
brew install mkcert
brew install nss
```

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

```powershell
choco install mkcert
mkcert -version
```

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

```bash
sudo apt install libnss3-tools
mkcert -version
```

Obtain the binary from [the project’s releases](https://github.com/FiloSottile/mkcert/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:

```bash
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.

Warning

The CA key stays on its issuing machine

Never commit, upload or share `rootCA-key.pem`. Other machines need only `rootCA.pem` to trust your certificates. A server needs its own leaf certificate and leaf private key, not the CA private key.

### 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:

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

In PowerShell:

```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](https://pkizerotohero.com/handbook/glossary/#subject-alternative-name) extension:

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

The locally tested command produced:

```text
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:

```javascript
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:

```bash
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](https://pkizerotohero.com/guides/add-root-ca-linux/). 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:

```bash
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](https://pkizerotohero.com/errors/uv-unknown-issuer/) rather than assuming Python or Node settings apply.

## Check it worked

Verify the chain independently on the issuing machine:

```bash
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](https://pkizerotohero.com/errors/chrome-err-cert-authority-invalid/).
- **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](https://pkizerotohero.com/handbook/certificate-chains/) to the local root you installed. The [SANs](https://pkizerotohero.com/handbook/names-and-key-usage/) identify the names the certificate covers. The root is trusted because you added it to the [trust store](https://pkizerotohero.com/handbook/trust-stores/), not because it signed itself. mkcert simplifies development issuance while leaving server configuration and client trust visible.

## Go deeper

- [mkcert README](https://github.com/FiloSottile/mkcert): supported stores, flags, CA ownership and installing the public root elsewhere.
- [Node.js HTTPS](https://nodejs.org/api/https.html): server certificate and key options.
- [Node.js extra CA certificates](https://nodejs.org/api/cli.html#node_extra_ca_certsfile): client trust and process-start behaviour.

**Early edition.** Written with AI assistance and checked against primary sources; an independent expert review is planned. Found a mistake? [Tell us](https://pkizerotohero.com/contact/?topic=content-error&page=/guides/localhost-https-mkcert/).

Last reviewed 25 October 2026Edition 0.9

## Keep going

### Learn why it works

- [Chapter 12: Who do we trust? CAs, trust stores and self-signed certificates](https://pkizerotohero.com/handbook/trust-stores/)
- [Chapter 9: Names and jobs: SANs, wildcards, key usage and the CA flag](https://pkizerotohero.com/handbook/names-and-key-usage/)
- [Chapter 22: A CA of your own: private two-tier PKI](https://pkizerotohero.com/handbook/private-ca/)

### Error pages

- [NET::ERR\_CERT\_AUTHORITY\_INVALID](https://pkizerotohero.com/errors/chrome-err-cert-authority-invalid/)
- [UNABLE\_TO\_VERIFY\_LEAF\_SIGNATURE](https://pkizerotohero.com/errors/node-unable-to-verify-leaf-signature/)
- [uv: invalid peer certificate: UnknownIssuer](https://pkizerotohero.com/errors/uv-unknown-issuer/)

### Tools

- [Certificate decoder](https://pkizerotohero.com/tools/decoder/): Paste a PEM certificate and read every field in plain words.

### Topics

- [Chains and trust stores](https://pkizerotohero.com/topics/chains-and-trust/)
- [TLS and HTTPS](https://pkizerotohero.com/topics/tls/)
- [Private PKI](https://pkizerotohero.com/topics/private-pki/)

## More guides: Create keys and CSRs

- [How to create a key and a CSR with OpenSSL (with SANs)](https://pkizerotohero.com/guides/create-csr-openssl/)
- [How to create a CSR on Windows (certreq and IIS)](https://pkizerotohero.com/guides/create-csr-windows/)
- [How to create a CSR with Java keytool](https://pkizerotohero.com/guides/create-csr-java-keytool/)
