Using mkcert for secure localhost development on WSL 2
For upcoming features I need to use HTTPS for local development and I came across mkcert which is a tool to create locally trusted certificates. However, I do most of my development through WSL and so I thought I'd be out of luck with using mkcert
.
I then came across a useful description in a Github Issues thread for this exact scenario.
Following alanaasmaa's steps:
- Install
mkcert
on Windows - I used Chocolatey for this - Ran
mkcert -install
- Install
mkcert
on Ubuntu WSL - for fun I decided to clone the repository and build it from source using Go - Run
mkcert -install
on Ubuntu - Back in a Windows PowerShell terminal I ran
mkcert -CAROOT
to find out where the certificates were created on Windows. I copied these. - On Ubuntu run
mkcert -CAROOT
and thenexplorer.exe
to open File Explorer on Windows at the Ubuntu directory. - Paste the certificates from step 5
- Run
mkcert -CAROOT
again on Ubuntu (I'm not sure if this step is neccessary but it works!) - Create certificates on WSL e.g.
mkcert example.com "*.example.com" localhost
- Configure nginx or Apache to use the generated certificates
The reason for installing mkcert on both Windows and Ubuntu is so that the browsers on Windows trust the certificates supplied by the nginx server on Ubuntu.
The final step I had to do was edit the hosts
file on Windows at C:\Windows\System32\drivers\etc\hosts
with Notepad in Administrator mode.
I can now view https://bowie.test
locally.