Skip to content

Instantly share code, notes, and snippets.

@jitheshkt
Last active December 6, 2023 16:16
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jitheshkt/7f578e3f450af9d0e8a248545d2662d7 to your computer and use it in GitHub Desktop.
Save jitheshkt/7f578e3f450af9d0e8a248545d2662d7 to your computer and use it in GitHub Desktop.
Installing SSL on WSL2 Apache
# The idea is, we don't install certificates on WSL, instead we install them on
# Windows and point the path at WSL Apache/Ngnix configuration file.
# We use mkcert to generate certificates.
# https://github.com/FiloSottile/mkcert
# Install mkcert on Linux. Even though we don't generate certificate here,
# I am installing this only to check the constant is properly set or not.
# If you're Linux Ninja like my friend Bombay, you could do it without
# This package for sure.
# On WSL2 as root
wget https://github.com/FiloSottile/mkcert/releases/download/v1.4.2/mkcert-v1.4.2-linux-amd64
mv mkcert-v1.4.2-linux-amd64 mkcert
chmod +x mkcert
cp mkcert /usr/local/bin/
# On Windows
# Install Chocolatey on Windows: https://chocolatey.org/install
# Open a PowerShell windows with administrator rights and run choco install mkcert
mkcert -install
setx CAROOT "$(mkcert -CAROOT)"; If ($Env:WSLENV -notlike "*CAROOT*") { setx WSLENV "CAROOT/up:$Env:WSLENV" }
# This will set the CAROOT environment variable on the WSL2 side to point to the Windows CAROOT,
# so your Windows browser can trust sites running in WSL2.
# Back on WSL2, you can verify the constant by typing:
mkcert -CAROOT
# You will see a result something like this:
`/mnt/c/Users/Jithesh/AppData/Local/mkcert`
# On Windows type
mkcert localhost 127.0.0.1 ::1 0.0.0.0
# So, now the certificates will be in the CAROOT directory.
# Simply update the apache2 config and point certificate files:
# Type vim /etc/apache2/sites-available/default-ssl.conf
SSLCertificateFile /mnt/c/Users/Jithesh/AppData/Local/mkcert/localhost+3.pem
SSLCertificateKeyFile /mnt/c/Users/Jithesh/AppData/Local/mkcert/localhost+3-key.pem
# Enable SSL if you've not
a2enmod ssl
a2ensite default-ssl.conf
# Restart Apache
service apache2 reload
service apache2 restart
@howarddavis
Copy link

Thank you for your help.

I followed this guide, however, I am stuck here:

I installed mkcert on windows, and ran the following,
setx CAROOT "$(mkcert -CAROOT)"; If ($Env:WSLENV -notlike "*CAROOT*") { setx WSLENV "CAROOT/up:$Env:WSLENV" }
however,
When I type in the following in WSL Ubuntu,
mkcert -CAROOT
I get the following output
/root/.local/share/mkcert

What am I doing wrong?

@ShawnCrigger-SOLS
Copy link

When I run

setx CAROOT "$(mkcert -CAROOT)"; If ($Env:WSLENV -notlike "*CAROOT*") { setx WSLENV "CAROOT/up:$Env:WSLENV" }

I get the following error every time

ERROR: Invalid syntax.
Type "SETX /?" for usage.

@jitheshkt
Copy link
Author

@ShawnCrigger-SOLS are you on powershell? Please try it on Powershell

@howarddavis Try the same command without any if condition. Like:

setx WSLENV "CAROOT/up:$Env:WSLENV"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment